]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOCommandGate.cpp
xnu-6153.81.5.tar.gz
[apple/xnu.git] / iokit / Kernel / IOCommandGate.cpp
1 /*
2 * Copyright (c) 1998-2000, 2009-2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #include <libkern/OSDebug.h>
29
30 #include <IOKit/IOCommandGate.h>
31 #include <IOKit/IOWorkLoop.h>
32 #include <IOKit/IOReturn.h>
33 #include <IOKit/IOTimeStamp.h>
34 #include <IOKit/IOKitDebug.h>
35
36 #define super IOEventSource
37
38 OSDefineMetaClassAndStructors(IOCommandGate, IOEventSource)
39 #if __LP64__
40 OSMetaClassDefineReservedUnused(IOCommandGate, 0);
41 #else
42 OSMetaClassDefineReservedUsed(IOCommandGate, 0);
43 #endif
44 OSMetaClassDefineReservedUnused(IOCommandGate, 1);
45 OSMetaClassDefineReservedUnused(IOCommandGate, 2);
46 OSMetaClassDefineReservedUnused(IOCommandGate, 3);
47 OSMetaClassDefineReservedUnused(IOCommandGate, 4);
48 OSMetaClassDefineReservedUnused(IOCommandGate, 5);
49 OSMetaClassDefineReservedUnused(IOCommandGate, 6);
50 OSMetaClassDefineReservedUnused(IOCommandGate, 7);
51
52 #if IOKITSTATS
53
54 #define IOStatisticsInitializeCounter() \
55 do { \
56 IOStatistics::setCounterType(IOEventSource::reserved->counter, kIOStatisticsCommandGateCounter); \
57 } while (0)
58
59 #define IOStatisticsActionCall() \
60 do { \
61 IOStatistics::countCommandGateActionCall(IOEventSource::reserved->counter); \
62 } while (0)
63
64 #else
65
66 #define IOStatisticsInitializeCounter()
67 #define IOStatisticsActionCall()
68
69 #endif /* IOKITSTATS */
70
71 bool
72 IOCommandGate::init(OSObject *inOwner, Action inAction)
73 {
74 bool res = super::init(inOwner, (IOEventSource::Action) inAction);
75 if (res) {
76 IOStatisticsInitializeCounter();
77 }
78
79 return res;
80 }
81
82 IOCommandGate *
83 IOCommandGate::commandGate(OSObject *inOwner, Action inAction)
84 {
85 IOCommandGate *me = new IOCommandGate;
86
87 if (me && !me->init(inOwner, inAction)) {
88 me->release();
89 return NULL;
90 }
91
92 return me;
93 }
94
95 /* virtual */ void
96 IOCommandGate::disable()
97 {
98 if (workLoop && !workLoop->inGate()) {
99 OSReportWithBacktrace("IOCommandGate::disable() called when not gated");
100 }
101
102 super::disable();
103 }
104
105 /* virtual */ void
106 IOCommandGate::enable()
107 {
108 if (workLoop) {
109 closeGate();
110 super::enable();
111 wakeupGate(&enabled, /* oneThread */ false); // Unblock sleeping threads
112 openGate();
113 }
114 }
115
116 /* virtual */ void
117 IOCommandGate::free()
118 {
119 if (workLoop) {
120 setWorkLoop(NULL);
121 }
122 super::free();
123 }
124
125 enum{
126 kSleepersRemoved = 0x00000001,
127 kSleepersWaitEnabled = 0x00000002,
128 kSleepersActions = 0x00000100,
129 kSleepersActionsMask = 0xffffff00,
130 };
131
132 /* virtual */ void
133 IOCommandGate::setWorkLoop(IOWorkLoop *inWorkLoop)
134 {
135 IOWorkLoop * wl;
136 uintptr_t * sleepersP = (uintptr_t *) &reserved;
137 bool defer;
138
139 if (!inWorkLoop && (wl = workLoop)) { // tearing down
140 wl->closeGate();
141 *sleepersP |= kSleepersRemoved;
142 while (*sleepersP & kSleepersWaitEnabled) {
143 thread_wakeup_with_result(&enabled, THREAD_INTERRUPTED);
144 sleepGate(sleepersP, THREAD_UNINT);
145 }
146 *sleepersP &= ~kSleepersWaitEnabled;
147 defer = (0 != (kSleepersActionsMask & *sleepersP));
148 if (!defer) {
149 super::setWorkLoop(NULL);
150 *sleepersP &= ~kSleepersRemoved;
151 }
152 wl->openGate();
153 return;
154 }
155
156 super::setWorkLoop(inWorkLoop);
157 }
158
159 IOReturn
160 IOCommandGate::runCommand(void *arg0, void *arg1,
161 void *arg2, void *arg3)
162 {
163 return runAction((Action) action, arg0, arg1, arg2, arg3);
164 }
165
166 IOReturn
167 IOCommandGate::attemptCommand(void *arg0, void *arg1,
168 void *arg2, void *arg3)
169 {
170 return attemptAction((Action) action, arg0, arg1, arg2, arg3);
171 }
172
173
174 static IOReturn
175 IOCommandGateActionToBlock(OSObject *owner,
176 void *arg0, void *arg1,
177 void *arg2, void *arg3)
178 {
179 return ((IOEventSource::ActionBlock) arg0)();
180 }
181
182 IOReturn
183 IOCommandGate::runActionBlock(ActionBlock _action)
184 {
185 return runAction(&IOCommandGateActionToBlock, _action);
186 }
187
188 IOReturn
189 IOCommandGate::runAction(Action inAction,
190 void *arg0, void *arg1,
191 void *arg2, void *arg3)
192 {
193 IOWorkLoop * wl;
194 uintptr_t * sleepersP;
195
196 if (!inAction) {
197 return kIOReturnBadArgument;
198 }
199 if (!(wl = workLoop)) {
200 return kIOReturnNotReady;
201 }
202
203 // closeGate is recursive needn't worry if we already hold the lock.
204 wl->closeGate();
205 sleepersP = (uintptr_t *) &reserved;
206
207 // If the command gate is disabled and we aren't on the workloop thread
208 // itself then sleep until we get enabled.
209 IOReturn res;
210 if (!wl->onThread()) {
211 while (!enabled) {
212 IOReturn sleepResult = kIOReturnSuccess;
213 if (workLoop) {
214 *sleepersP |= kSleepersWaitEnabled;
215 sleepResult = wl->sleepGate(&enabled, THREAD_INTERRUPTIBLE);
216 *sleepersP &= ~kSleepersWaitEnabled;
217 }
218 bool wakeupTearDown = (!workLoop || (0 != (*sleepersP & kSleepersRemoved)));
219 if ((kIOReturnSuccess != sleepResult) || wakeupTearDown) {
220 wl->openGate();
221
222 if (wakeupTearDown) {
223 wl->wakeupGate(sleepersP, false); // No further resources used
224 }
225 return kIOReturnAborted;
226 }
227 }
228 }
229
230 bool trace = (gIOKitTrace & kIOTraceCommandGates) ? true : false;
231
232 if (trace) {
233 IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION),
234 VM_KERNEL_ADDRHIDE(inAction), VM_KERNEL_ADDRHIDE(owner));
235 }
236
237 IOStatisticsActionCall();
238
239 // Must be gated and on the work loop or enabled
240
241 *sleepersP += kSleepersActions;
242 res = (*inAction)(owner, arg0, arg1, arg2, arg3);
243 *sleepersP -= kSleepersActions;
244
245 if (trace) {
246 IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION),
247 VM_KERNEL_ADDRHIDE(inAction), VM_KERNEL_ADDRHIDE(owner));
248 }
249
250 if (kSleepersRemoved == ((kSleepersActionsMask | kSleepersRemoved) & *sleepersP)) {
251 // no actions outstanding
252 *sleepersP &= ~kSleepersRemoved;
253 super::setWorkLoop(NULL);
254 }
255
256 wl->openGate();
257
258 return res;
259 }
260
261 IOReturn
262 IOCommandGate::attemptAction(Action inAction,
263 void *arg0, void *arg1,
264 void *arg2, void *arg3)
265 {
266 IOReturn res;
267 IOWorkLoop * wl;
268
269 if (!inAction) {
270 return kIOReturnBadArgument;
271 }
272 if (!(wl = workLoop)) {
273 return kIOReturnNotReady;
274 }
275
276 // Try to close the gate if can't get return immediately.
277 if (!wl->tryCloseGate()) {
278 return kIOReturnCannotLock;
279 }
280
281 // If the command gate is disabled then sleep until we get a wakeup
282 if (!wl->onThread() && !enabled) {
283 res = kIOReturnNotPermitted;
284 } else {
285 bool trace = (gIOKitTrace & kIOTraceCommandGates) ? true : false;
286
287 if (trace) {
288 IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION),
289 VM_KERNEL_ADDRHIDE(inAction), VM_KERNEL_ADDRHIDE(owner));
290 }
291
292 IOStatisticsActionCall();
293
294 res = (*inAction)(owner, arg0, arg1, arg2, arg3);
295
296 if (trace) {
297 IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION),
298 VM_KERNEL_ADDRHIDE(inAction), VM_KERNEL_ADDRHIDE(owner));
299 }
300 }
301
302 wl->openGate();
303
304 return res;
305 }
306
307 IOReturn
308 IOCommandGate::commandSleep(void *event, UInt32 interruptible)
309 {
310 if (!workLoop->inGate()) {
311 /* The equivalent of 'msleep' while not holding the mutex is invalid */
312 panic("invalid commandSleep while not holding the gate");
313 }
314
315 return sleepGate(event, interruptible);
316 }
317
318 IOReturn
319 IOCommandGate::commandSleep(void *event, AbsoluteTime deadline, UInt32 interruptible)
320 {
321 if (!workLoop->inGate()) {
322 /* The equivalent of 'msleep' while not holding the mutex is invalid */
323 panic("invalid commandSleep while not holding the gate");
324 }
325
326 return sleepGate(event, deadline, interruptible);
327 }
328
329 void
330 IOCommandGate::commandWakeup(void *event, bool oneThread)
331 {
332 wakeupGate(event, oneThread);
333 }