]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/subr_eventhandler.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / bsd / kern / subr_eventhandler.c
CommitLineData
5ba3f43e
A
1/*
2 * Copyright (c) 2016-2017 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/*-
29 * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
30 * All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 */
53
54#include <sys/cdefs.h>
55#include <sys/param.h>
56#include <sys/kernel.h>
57#include <kern/queue.h>
58#include <kern/locks.h>
59#include <sys/malloc.h>
60#include <sys/proc.h>
61#include <sys/systm.h>
62#include <sys/mcache.h>
63#include <sys/eventhandler.h>
64#include <sys/sysctl.h>
65
66int evh_debug = 0;
67
68MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records");
69
70SYSCTL_NODE(_kern, OID_AUTO, eventhandler, CTLFLAG_RW | CTLFLAG_LOCKED,
71 0, "Eventhandler");
72SYSCTL_INT(_kern_eventhandler, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_LOCKED,
73 &evh_debug, 0, "Eventhandler debug mode");
74
a39ff7e2 75struct eventhandler_entry_arg eventhandler_entry_dummy_arg = { { 0 }, { 0 } };
5ba3f43e
A
76
77/* List of 'slow' lists */
78static struct eventhandler_lists_ctxt evthdlr_lists_ctxt_glb;
79static lck_grp_attr_t *eventhandler_mutex_grp_attr;
80static lck_grp_t *eventhandler_mutex_grp;
81static lck_attr_t *eventhandler_mutex_attr;
82
0a7de745
A
83static unsigned int eg_size; /* size of eventhandler_entry_generic */
84static struct mcache *eg_cache; /* mcache for eventhandler_entry_generic */
a39ff7e2 85
0a7de745
A
86static unsigned int el_size; /* size of eventhandler_list */
87static struct mcache *el_cache; /* mcache for eventhandler_list */
a39ff7e2 88
5ba3f43e
A
89static lck_grp_attr_t *el_lock_grp_attr;
90lck_grp_t *el_lock_grp;
91lck_attr_t *el_lock_attr;
92
0a7de745
A
93struct eventhandler_entry_generic {
94 struct eventhandler_entry ee;
95 void (* func)(void);
5ba3f43e
A
96};
97
98static struct eventhandler_list *_eventhandler_find_list(
0a7de745 99 struct eventhandler_lists_ctxt *evthdlr_lists_ctxt, const char *name);
5ba3f43e
A
100
101void
102eventhandler_lists_ctxt_init(struct eventhandler_lists_ctxt *evthdlr_lists_ctxt)
103{
104 VERIFY(evthdlr_lists_ctxt != NULL);
105
106 TAILQ_INIT(&evthdlr_lists_ctxt->eventhandler_lists);
107 evthdlr_lists_ctxt->eventhandler_lists_initted = 1;
108 lck_mtx_init(&evthdlr_lists_ctxt->eventhandler_mutex,
109 eventhandler_mutex_grp, eventhandler_mutex_attr);
110}
111
112/*
113 * Initialize the eventhandler mutex and list.
114 */
115void
116eventhandler_init(void)
117{
118 eventhandler_mutex_grp_attr = lck_grp_attr_alloc_init();
119 eventhandler_mutex_grp = lck_grp_alloc_init("eventhandler",
120 eventhandler_mutex_grp_attr);
121 eventhandler_mutex_attr = lck_attr_alloc_init();
122
123 el_lock_grp_attr = lck_grp_attr_alloc_init();
124 el_lock_grp = lck_grp_alloc_init("eventhandler list",
125 el_lock_grp_attr);
126 el_lock_attr = lck_attr_alloc_init();
127
128 eventhandler_lists_ctxt_init(&evthdlr_lists_ctxt_glb);
a39ff7e2 129
0a7de745 130 eg_size = sizeof(struct eventhandler_entry_generic);
a39ff7e2 131 eg_cache = mcache_create("eventhdlr_generic", eg_size,
0a7de745 132 sizeof(uint64_t), 0, MCR_SLEEP);
a39ff7e2 133
0a7de745 134 el_size = sizeof(struct eventhandler_list);
a39ff7e2 135 el_cache = mcache_create("eventhdlr_list", el_size,
0a7de745 136 sizeof(uint64_t), 0, MCR_SLEEP);
a39ff7e2
A
137}
138
139void
140eventhandler_reap_caches(boolean_t purge)
141{
142 mcache_reap_now(eg_cache, purge);
143 mcache_reap_now(el_cache, purge);
5ba3f43e
A
144}
145
146/*
147 * Insertion is O(n) due to the priority scan, but optimises to O(1)
148 * if all priorities are identical.
149 */
150static eventhandler_tag
151eventhandler_register_internal(
0a7de745
A
152 struct eventhandler_lists_ctxt *evthdlr_lists_ctxt,
153 struct eventhandler_list *list,
154 const char *name, eventhandler_tag epn)
5ba3f43e 155{
0a7de745
A
156 struct eventhandler_list *new_list;
157 struct eventhandler_entry *ep;
5ba3f43e 158
0a7de745 159 VERIFY(strlen(name) <= (sizeof(new_list->el_name) - 1));
a39ff7e2 160
0a7de745 161 if (evthdlr_lists_ctxt == NULL) {
5ba3f43e 162 evthdlr_lists_ctxt = &evthdlr_lists_ctxt_glb;
0a7de745 163 }
5ba3f43e
A
164
165 VERIFY(evthdlr_lists_ctxt->eventhandler_lists_initted); /* eventhandler registered too early */
166 VERIFY(epn != NULL); /* cannot register NULL event */
167
168 /* lock the eventhandler lists */
a39ff7e2 169 lck_mtx_lock_spin(&evthdlr_lists_ctxt->eventhandler_mutex);
5ba3f43e
A
170
171 /* Do we need to find/create the (slow) list? */
172 if (list == NULL) {
173 /* look for a matching, existing list */
174 list = _eventhandler_find_list(evthdlr_lists_ctxt, name);
175
176 /* Do we need to create the list? */
177 if (list == NULL) {
a39ff7e2
A
178 lck_mtx_convert_spin(&evthdlr_lists_ctxt->eventhandler_mutex);
179 new_list = mcache_alloc(el_cache, MCR_SLEEP);
180 bzero(new_list, el_size);
181 evhlog((LOG_DEBUG, "%s: creating list \"%s\"", __func__, name));
182 list = new_list;
183 list->el_flags = 0;
184 list->el_runcount = 0;
185 bzero(&list->el_lock, sizeof(list->el_lock));
0a7de745 186 (void) snprintf(list->el_name, sizeof(list->el_name), "%s", name);
a39ff7e2 187 TAILQ_INSERT_HEAD(&evthdlr_lists_ctxt->eventhandler_lists, list, el_link);
5ba3f43e
A
188 }
189 }
190 if (!(list->el_flags & EHL_INITTED)) {
191 TAILQ_INIT(&list->el_entries);
192 EHL_LOCK_INIT(list);
193 list->el_flags |= EHL_INITTED;
194 }
195 lck_mtx_unlock(&evthdlr_lists_ctxt->eventhandler_mutex);
196
197 KASSERT(epn->ee_priority != EHE_DEAD_PRIORITY,
198 ("%s: handler for %s registered with dead priority", __func__, name));
199
200 /* sort it into the list */
201 evhlog((LOG_DEBUG, "%s: adding item %p (function %p to \"%s\"", __func__, VM_KERNEL_ADDRPERM(epn),
202 VM_KERNEL_UNSLIDE(((struct eventhandler_entry_generic *)epn)->func), name));
203 EHL_LOCK(list);
204 TAILQ_FOREACH(ep, &list->el_entries, ee_link) {
205 if (ep->ee_priority != EHE_DEAD_PRIORITY &&
206 epn->ee_priority < ep->ee_priority) {
207 TAILQ_INSERT_BEFORE(ep, epn, ee_link);
208 break;
209 }
210 }
0a7de745 211 if (ep == NULL) {
5ba3f43e 212 TAILQ_INSERT_TAIL(&list->el_entries, epn, ee_link);
0a7de745 213 }
5ba3f43e 214 EHL_UNLOCK(list);
0a7de745 215 return epn;
5ba3f43e
A
216}
217
218eventhandler_tag
219eventhandler_register(struct eventhandler_lists_ctxt *evthdlr_lists_ctxt,
220 struct eventhandler_list *list, const char *name,
221 void *func, struct eventhandler_entry_arg arg, int priority)
222{
0a7de745 223 struct eventhandler_entry_generic *eg;
5ba3f43e
A
224
225 /* allocate an entry for this handler, populate it */
a39ff7e2
A
226 eg = mcache_alloc(eg_cache, MCR_SLEEP);
227 bzero(eg, eg_size);
5ba3f43e
A
228 eg->func = func;
229 eg->ee.ee_arg = arg;
230 eg->ee.ee_priority = priority;
231
0a7de745 232 return eventhandler_register_internal(evthdlr_lists_ctxt, list, name, &eg->ee);
5ba3f43e
A
233}
234
235void
236eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
237{
0a7de745 238 struct eventhandler_entry *ep = tag;
5ba3f43e
A
239
240 EHL_LOCK_ASSERT(list, LCK_MTX_ASSERT_OWNED);
241 if (ep != NULL) {
242 /* remove just this entry */
243 if (list->el_runcount == 0) {
244 evhlog((LOG_DEBUG, "%s: removing item %p from \"%s\"", __func__, VM_KERNEL_ADDRPERM(ep),
245 list->el_name));
246 /*
247 * We may have purged the list because of certain events.
248 * Make sure that is not the case when a specific entry
249 * is being removed.
250 */
0a7de745 251 if (!TAILQ_EMPTY(&list->el_entries)) {
5ba3f43e 252 TAILQ_REMOVE(&list->el_entries, ep, ee_link);
0a7de745 253 }
a39ff7e2
A
254 EHL_LOCK_CONVERT(list);
255 mcache_free(eg_cache, ep);
5ba3f43e
A
256 } else {
257 evhlog((LOG_DEBUG, "%s: marking item %p from \"%s\" as dead", __func__,
258 VM_KERNEL_ADDRPERM(ep), list->el_name));
259 ep->ee_priority = EHE_DEAD_PRIORITY;
260 }
261 } else {
262 /* remove entire list */
263 if (list->el_runcount == 0) {
264 evhlog((LOG_DEBUG, "%s: removing all items from \"%s\"", __func__,
265 list->el_name));
a39ff7e2 266 EHL_LOCK_CONVERT(list);
5ba3f43e
A
267 while (!TAILQ_EMPTY(&list->el_entries)) {
268 ep = TAILQ_FIRST(&list->el_entries);
269 TAILQ_REMOVE(&list->el_entries, ep, ee_link);
a39ff7e2 270 mcache_free(eg_cache, ep);
5ba3f43e
A
271 }
272 } else {
273 evhlog((LOG_DEBUG, "%s: marking all items from \"%s\" as dead",
274 __func__, list->el_name));
275 TAILQ_FOREACH(ep, &list->el_entries, ee_link)
0a7de745 276 ep->ee_priority = EHE_DEAD_PRIORITY;
5ba3f43e
A
277 }
278 }
0a7de745 279 while (list->el_runcount > 0) {
a39ff7e2 280 msleep((caddr_t)list, &list->el_lock, PSPIN, "evhrm", 0);
0a7de745 281 }
5ba3f43e
A
282 EHL_UNLOCK(list);
283}
284
285/*
286 * Internal version for use when eventhandler list is already locked.
287 */
288static struct eventhandler_list *
289_eventhandler_find_list(struct eventhandler_lists_ctxt *evthdlr_lists_ctxt,
290 const char *name)
291{
0a7de745 292 struct eventhandler_list *list;
5ba3f43e
A
293
294 VERIFY(evthdlr_lists_ctxt != NULL);
295
296 LCK_MTX_ASSERT(&evthdlr_lists_ctxt->eventhandler_mutex, LCK_MTX_ASSERT_OWNED);
297 TAILQ_FOREACH(list, &evthdlr_lists_ctxt->eventhandler_lists, el_link) {
0a7de745 298 if (!strcmp(name, list->el_name)) {
5ba3f43e 299 break;
0a7de745 300 }
5ba3f43e 301 }
0a7de745 302 return list;
5ba3f43e
A
303}
304
305/*
306 * Lookup a "slow" list by name. Returns with the list locked.
307 */
308struct eventhandler_list *
309eventhandler_find_list(struct eventhandler_lists_ctxt *evthdlr_lists_ctxt,
310 const char *name)
311{
0a7de745 312 struct eventhandler_list *list;
5ba3f43e 313
0a7de745 314 if (evthdlr_lists_ctxt == NULL) {
5ba3f43e 315 evthdlr_lists_ctxt = &evthdlr_lists_ctxt_glb;
0a7de745 316 }
5ba3f43e 317
0a7de745
A
318 if (!evthdlr_lists_ctxt->eventhandler_lists_initted) {
319 return NULL;
320 }
5ba3f43e
A
321
322 /* scan looking for the requested list */
a39ff7e2 323 lck_mtx_lock_spin(&evthdlr_lists_ctxt->eventhandler_mutex);
5ba3f43e 324 list = _eventhandler_find_list(evthdlr_lists_ctxt, name);
a39ff7e2
A
325 if (list != NULL) {
326 lck_mtx_convert_spin(&evthdlr_lists_ctxt->eventhandler_mutex);
327 EHL_LOCK_SPIN(list);
328 }
5ba3f43e
A
329 lck_mtx_unlock(&evthdlr_lists_ctxt->eventhandler_mutex);
330
0a7de745 331 return list;
5ba3f43e
A
332}
333
334/*
335 * Prune "dead" entries from an eventhandler list.
336 */
337void
338eventhandler_prune_list(struct eventhandler_list *list)
339{
340 struct eventhandler_entry *ep, *en;
341 int pruned = 0;
342
343 evhlog((LOG_DEBUG, "%s: pruning list \"%s\"", __func__, list->el_name));
344 EHL_LOCK_ASSERT(list, LCK_MTX_ASSERT_OWNED);
345 TAILQ_FOREACH_SAFE(ep, &list->el_entries, ee_link, en) {
346 if (ep->ee_priority == EHE_DEAD_PRIORITY) {
347 TAILQ_REMOVE(&list->el_entries, ep, ee_link);
a39ff7e2 348 mcache_free(eg_cache, ep);
5ba3f43e
A
349 pruned++;
350 }
351 }
0a7de745 352 if (pruned > 0) {
5ba3f43e 353 wakeup(list);
0a7de745 354 }
5ba3f43e
A
355}
356
357/*
358 * This should be called when last reference to an object
359 * is being released.
360 * The individual event type lists must be purged when the object
361 * becomes defunct.
362 */
363void
364eventhandler_lists_ctxt_destroy(struct eventhandler_lists_ctxt *evthdlr_lists_ctxt)
365{
366 struct eventhandler_list *list = NULL;
0a7de745 367 struct eventhandler_list *list_next = NULL;
5ba3f43e
A
368
369 lck_mtx_lock(&evthdlr_lists_ctxt->eventhandler_mutex);
370 TAILQ_FOREACH_SAFE(list, &evthdlr_lists_ctxt->eventhandler_lists,
371 el_link, list_next) {
372 VERIFY(TAILQ_EMPTY(&list->el_entries));
373 EHL_LOCK_DESTROY(list);
a39ff7e2 374 mcache_free(el_cache, list);
5ba3f43e
A
375 }
376 lck_mtx_unlock(&evthdlr_lists_ctxt->eventhandler_mutex);
377 lck_mtx_destroy(&evthdlr_lists_ctxt->eventhandler_mutex,
378 eventhandler_mutex_grp);
379 return;
380}