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 IOCommandGate::init(OSObject *inOwner, Action inAction) |
72 | { |
73 | bool res = super::init(inOwner, (IOEventSource::Action) inAction); |
74 | if (res) { |
75 | IOStatisticsInitializeCounter(); |
76 | } |
77 | |
78 | return res; |
79 | } |
80 | |
81 | IOCommandGate * |
82 | IOCommandGate::commandGate(OSObject *inOwner, Action inAction) |
83 | { |
84 | IOCommandGate *me = new IOCommandGate; |
85 | |
86 | if (me && !me->init(inOwner, inAction)) { |
87 | me->release(); |
88 | return 0; |
89 | } |
90 | |
91 | return me; |
92 | } |
93 | |
94 | /* virtual */ void IOCommandGate::disable() |
95 | { |
96 | if (workLoop && !workLoop->inGate()) |
97 | OSReportWithBacktrace("IOCommandGate::disable() called when not gated" ); |
98 | |
99 | super::disable(); |
100 | } |
101 | |
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 | } |
111 | |
112 | /* virtual */ void IOCommandGate::free() |
113 | { |
114 | if (workLoop) setWorkLoop(0); |
115 | super::free(); |
116 | } |
117 | |
118 | enum |
119 | { |
120 | kSleepersRemoved = 0x00000001, |
121 | kSleepersWaitEnabled = 0x00000002, |
122 | kSleepersActions = 0x00000100, |
123 | kSleepersActionsMask = 0xffffff00, |
124 | }; |
125 | |
126 | /* virtual */ void IOCommandGate::setWorkLoop(IOWorkLoop *inWorkLoop) |
127 | { |
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) { |
136 | thread_wakeup_with_result(&enabled, THREAD_INTERRUPTED); |
137 | sleepGate(sleepersP, THREAD_UNINT); |
138 | } |
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; |
148 | } |
149 | |
150 | super::setWorkLoop(inWorkLoop); |
151 | } |
152 | |
153 | IOReturn IOCommandGate::runCommand(void *arg0, void *arg1, |
154 | void *arg2, void *arg3) |
155 | { |
156 | return runAction((Action) action, arg0, arg1, arg2, arg3); |
157 | } |
158 | |
159 | IOReturn IOCommandGate::attemptCommand(void *arg0, void *arg1, |
160 | void *arg2, void *arg3) |
161 | { |
162 | return attemptAction((Action) action, arg0, arg1, arg2, arg3); |
163 | } |
164 | |
165 | |
166 | static IOReturn IOCommandGateActionToBlock(OSObject *owner, |
167 | void *arg0, void *arg1, |
168 | void *arg2, void *arg3) |
169 | { |
170 | return ((IOEventSource::ActionBlock) arg0)(); |
171 | } |
172 | |
173 | IOReturn IOCommandGate::runActionBlock(ActionBlock action) |
174 | { |
175 | return (runAction(&IOCommandGateActionToBlock, action)); |
176 | } |
177 | |
178 | IOReturn IOCommandGate::runAction(Action inAction, |
179 | void *arg0, void *arg1, |
180 | void *arg2, void *arg3) |
181 | { |
182 | IOWorkLoop * wl; |
183 | uintptr_t * sleepersP; |
184 | |
185 | if (!inAction) |
186 | return kIOReturnBadArgument; |
187 | if (!(wl = workLoop)) |
188 | return kIOReturnNotReady; |
189 | |
190 | // closeGate is recursive needn't worry if we already hold the lock. |
191 | wl->closeGate(); |
192 | sleepersP = (uintptr_t *) &reserved; |
193 | |
194 | // If the command gate is disabled and we aren't on the workloop thread |
195 | // itself then sleep until we get enabled. |
196 | IOReturn res; |
197 | if (!wl->onThread()) |
198 | { |
199 | while (!enabled) |
200 | { |
201 | IOReturn sleepResult = kIOReturnSuccess; |
202 | if (workLoop) |
203 | { |
204 | *sleepersP |= kSleepersWaitEnabled; |
205 | sleepResult = wl->sleepGate(&enabled, THREAD_INTERRUPTIBLE); |
206 | *sleepersP &= ~kSleepersWaitEnabled; |
207 | } |
208 | bool wakeupTearDown = (!workLoop || (0 != (*sleepersP & kSleepersRemoved))); |
209 | if ((kIOReturnSuccess != sleepResult) || wakeupTearDown) { |
210 | wl->openGate(); |
211 | |
212 | if (wakeupTearDown) |
213 | wl->wakeupGate(sleepersP, false); // No further resources used |
214 | |
215 | return kIOReturnAborted; |
216 | } |
217 | } |
218 | } |
219 | |
220 | bool trace = ( gIOKitTrace & kIOTraceCommandGates ) ? true : false; |
221 | |
222 | if (trace) IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION), |
223 | VM_KERNEL_ADDRHIDE(inAction), VM_KERNEL_ADDRHIDE(owner)); |
224 | |
225 | IOStatisticsActionCall(); |
226 | |
227 | // Must be gated and on the work loop or enabled |
228 | |
229 | *sleepersP += kSleepersActions; |
230 | res = (*inAction)(owner, arg0, arg1, arg2, arg3); |
231 | *sleepersP -= kSleepersActions; |
232 | |
233 | if (trace) IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION), |
234 | VM_KERNEL_ADDRHIDE(inAction), VM_KERNEL_ADDRHIDE(owner)); |
235 | |
236 | if (kSleepersRemoved == ((kSleepersActionsMask|kSleepersRemoved) & *sleepersP)) |
237 | { |
238 | // no actions outstanding |
239 | *sleepersP &= ~kSleepersRemoved; |
240 | super::setWorkLoop(0); |
241 | } |
242 | |
243 | wl->openGate(); |
244 | |
245 | return res; |
246 | } |
247 | |
248 | IOReturn IOCommandGate::attemptAction(Action inAction, |
249 | void *arg0, void *arg1, |
250 | void *arg2, void *arg3) |
251 | { |
252 | IOReturn res; |
253 | IOWorkLoop * wl; |
254 | |
255 | if (!inAction) |
256 | return kIOReturnBadArgument; |
257 | if (!(wl = workLoop)) |
258 | return kIOReturnNotReady; |
259 | |
260 | // Try to close the gate if can't get return immediately. |
261 | if (!wl->tryCloseGate()) |
262 | return kIOReturnCannotLock; |
263 | |
264 | // If the command gate is disabled then sleep until we get a wakeup |
265 | if (!wl->onThread() && !enabled) |
266 | res = kIOReturnNotPermitted; |
267 | else { |
268 | |
269 | bool trace = ( gIOKitTrace & kIOTraceCommandGates ) ? true : false; |
270 | |
271 | if (trace) |
272 | IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION), |
273 | VM_KERNEL_ADDRHIDE(inAction), VM_KERNEL_ADDRHIDE(owner)); |
274 | |
275 | IOStatisticsActionCall(); |
276 | |
277 | res = (*inAction)(owner, arg0, arg1, arg2, arg3); |
278 | |
279 | if (trace) |
280 | IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION), |
281 | VM_KERNEL_ADDRHIDE(inAction), VM_KERNEL_ADDRHIDE(owner)); |
282 | } |
283 | |
284 | wl->openGate(); |
285 | |
286 | return res; |
287 | } |
288 | |
289 | IOReturn IOCommandGate::commandSleep(void *event, UInt32 interruptible) |
290 | { |
291 | if (!workLoop->inGate()) { |
292 | /* The equivalent of 'msleep' while not holding the mutex is invalid */ |
293 | panic("invalid commandSleep while not holding the gate" ); |
294 | } |
295 | |
296 | return sleepGate(event, interruptible); |
297 | } |
298 | |
299 | IOReturn IOCommandGate::commandSleep(void *event, AbsoluteTime deadline, UInt32 interruptible) |
300 | { |
301 | if (!workLoop->inGate()) { |
302 | /* The equivalent of 'msleep' while not holding the mutex is invalid */ |
303 | panic("invalid commandSleep while not holding the gate" ); |
304 | } |
305 | |
306 | return sleepGate(event, deadline, interruptible); |
307 | } |
308 | |
309 | void IOCommandGate::commandWakeup(void *event, bool oneThread) |
310 | { |
311 | wakeupGate(event, oneThread); |
312 | } |
313 | |