]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOCommandGate.cpp
xnu-6153.11.26.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@
0a7de745 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.
0a7de745 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.
0a7de745 17 *
2d21ac55
A
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.
0a7de745 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
0a7de745
A
71bool
72IOCommandGate::init(OSObject *inOwner, Action inAction)
1c79356b 73{
0a7de745
A
74 bool res = super::init(inOwner, (IOEventSource::Action) inAction);
75 if (res) {
76 IOStatisticsInitializeCounter();
77 }
6d2010ae 78
0a7de745 79 return res;
1c79356b
A
80}
81
82IOCommandGate *
55e303ae 83IOCommandGate::commandGate(OSObject *inOwner, Action inAction)
1c79356b 84{
0a7de745 85 IOCommandGate *me = new IOCommandGate;
1c79356b 86
0a7de745
A
87 if (me && !me->init(inOwner, inAction)) {
88 me->release();
cb323159 89 return NULL;
0a7de745 90 }
1c79356b 91
0a7de745 92 return me;
1c79356b
A
93}
94
0a7de745
A
95/* virtual */ void
96IOCommandGate::disable()
89b3af67 97{
0a7de745
A
98 if (workLoop && !workLoop->inGate()) {
99 OSReportWithBacktrace("IOCommandGate::disable() called when not gated");
100 }
5d5c5d0d 101
0a7de745 102 super::disable();
0c530ab8 103}
5d5c5d0d 104
0a7de745
A
105/* virtual */ void
106IOCommandGate::enable()
0c530ab8 107{
0a7de745
A
108 if (workLoop) {
109 closeGate();
110 super::enable();
111 wakeupGate(&enabled, /* oneThread */ false); // Unblock sleeping threads
112 openGate();
113 }
0c530ab8 114}
89b3af67 115
0a7de745
A
116/* virtual */ void
117IOCommandGate::free()
0c530ab8 118{
0a7de745 119 if (workLoop) {
cb323159 120 setWorkLoop(NULL);
0a7de745
A
121 }
122 super::free();
0c530ab8 123}
4452a7af 124
0a7de745
A
125enum{
126 kSleepersRemoved = 0x00000001,
127 kSleepersWaitEnabled = 0x00000002,
128 kSleepersActions = 0x00000100,
129 kSleepersActionsMask = 0xffffff00,
3e170ce0
A
130};
131
0a7de745
A
132/* virtual */ void
133IOCommandGate::setWorkLoop(IOWorkLoop *inWorkLoop)
0c530ab8 134{
0a7de745
A
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) {
cb323159 149 super::setWorkLoop(NULL);
0a7de745
A
150 *sleepersP &= ~kSleepersRemoved;
151 }
152 wl->openGate();
153 return;
3e170ce0 154 }
4452a7af 155
0a7de745 156 super::setWorkLoop(inWorkLoop);
0c530ab8
A
157}
158
0a7de745
A
159IOReturn
160IOCommandGate::runCommand(void *arg0, void *arg1,
161 void *arg2, void *arg3)
0c530ab8 162{
0a7de745 163 return runAction((Action) action, arg0, arg1, arg2, arg3);
0c530ab8
A
164}
165
0a7de745
A
166IOReturn
167IOCommandGate::attemptCommand(void *arg0, void *arg1,
168 void *arg2, void *arg3)
0c530ab8 169{
0a7de745 170 return attemptAction((Action) action, arg0, arg1, arg2, arg3);
1c79356b
A
171}
172
d9a64523 173
0a7de745
A
174static IOReturn
175IOCommandGateActionToBlock(OSObject *owner,
176 void *arg0, void *arg1,
177 void *arg2, void *arg3)
d9a64523 178{
0a7de745 179 return ((IOEventSource::ActionBlock) arg0)();
d9a64523
A
180}
181
0a7de745 182IOReturn
cb323159 183IOCommandGate::runActionBlock(ActionBlock _action)
d9a64523 184{
cb323159 185 return runAction(&IOCommandGateActionToBlock, _action);
d9a64523
A
186}
187
0a7de745
A
188IOReturn
189IOCommandGate::runAction(Action inAction,
190 void *arg0, void *arg1,
191 void *arg2, void *arg3)
1c79356b 192{
0a7de745
A
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;
1c79356b 231
0a7de745
A
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;
1c79356b 244
0a7de745
A
245 if (trace) {
246 IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION),
247 VM_KERNEL_ADDRHIDE(inAction), VM_KERNEL_ADDRHIDE(owner));
0c530ab8 248 }
0a7de745
A
249
250 if (kSleepersRemoved == ((kSleepersActionsMask | kSleepersRemoved) & *sleepersP)) {
251 // no actions outstanding
252 *sleepersP &= ~kSleepersRemoved;
cb323159 253 super::setWorkLoop(NULL);
0a7de745
A
254 }
255
256 wl->openGate();
257
258 return res;
1c79356b
A
259}
260
0a7de745
A
261IOReturn
262IOCommandGate::attemptAction(Action inAction,
263 void *arg0, void *arg1,
264 void *arg2, void *arg3)
1c79356b 265{
0a7de745
A
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;
1c79356b
A
305}
306
0a7de745
A
307IOReturn
308IOCommandGate::commandSleep(void *event, UInt32 interruptible)
1c79356b 309{
0a7de745
A
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 }
1c79356b 314
0a7de745 315 return sleepGate(event, interruptible);
1c79356b
A
316}
317
0a7de745
A
318IOReturn
319IOCommandGate::commandSleep(void *event, AbsoluteTime deadline, UInt32 interruptible)
b0d623f7 320{
0a7de745
A
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 }
b0d623f7 325
0a7de745 326 return sleepGate(event, deadline, interruptible);
b0d623f7
A
327}
328
0a7de745
A
329void
330IOCommandGate::commandWakeup(void *event, bool oneThread)
1c79356b 331{
0a7de745 332 wakeupGate(event, oneThread);
1c79356b 333}