| 1 | /* |
| 2 | * Copyright (c) 2016-2018 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 | * $FreeBSD$ |
| 54 | */ |
| 55 | |
| 56 | #ifndef _SYS_EVENTHANDLER_H_ |
| 57 | #define _SYS_EVENTHANDLER_H_ |
| 58 | |
| 59 | #include <ptrauth.h> |
| 60 | |
| 61 | #include <kern/locks.h> |
| 62 | #include <sys/queue.h> |
| 63 | #include <sys/cdefs.h> |
| 64 | #include <sys/syslog.h> |
| 65 | #include <uuid/uuid.h> |
| 66 | |
| 67 | extern int evh_debug; |
| 68 | extern lck_grp_t el_lock_grp; |
| 69 | extern lck_attr_t el_lock_attr; |
| 70 | extern struct eventhandler_entry_arg eventhandler_entry_dummy_arg; |
| 71 | |
| 72 | struct eventhandler_lists_ctxt { |
| 73 | TAILQ_HEAD(, eventhandler_list) eventhandler_lists; |
| 74 | int eventhandler_lists_initted; |
| 75 | decl_lck_mtx_data(, eventhandler_mutex); |
| 76 | }; |
| 77 | |
| 78 | struct eventhandler_entry_arg { |
| 79 | uuid_t ee_fm_uuid; /* Flow manager UUID */ |
| 80 | uuid_t ee_fr_uuid; /* Flow route UUID */ |
| 81 | }; |
| 82 | |
| 83 | struct eventhandler_entry { |
| 84 | TAILQ_ENTRY(eventhandler_entry) ee_link; |
| 85 | int ee_priority; |
| 86 | #define EHE_DEAD_PRIORITY (-1) |
| 87 | struct eventhandler_entry_arg ee_arg; |
| 88 | }; |
| 89 | |
| 90 | #define EVENTHANDLER_MAX_NAME 32 |
| 91 | |
| 92 | struct eventhandler_list { |
| 93 | char el_name[EVENTHANDLER_MAX_NAME]; |
| 94 | int el_flags; |
| 95 | #define EHL_INITTED (1<<0) |
| 96 | u_int el_runcount; |
| 97 | decl_lck_mtx_data(, el_lock); |
| 98 | TAILQ_ENTRY(eventhandler_list) el_link; |
| 99 | TAILQ_HEAD(, eventhandler_entry) el_entries; |
| 100 | }; |
| 101 | |
| 102 | typedef struct eventhandler_entry *eventhandler_tag; |
| 103 | |
| 104 | #define EHL_LOCK_INIT(p) lck_mtx_init(&(p)->el_lock, &el_lock_grp, &el_lock_attr) |
| 105 | #define EHL_LOCK(p) lck_mtx_lock(&(p)->el_lock) |
| 106 | #define EHL_LOCK_SPIN(p) lck_mtx_lock_spin(&(p)->el_lock) |
| 107 | #define EHL_LOCK_CONVERT(p) lck_mtx_convert_spin(&(p)->el_lock) |
| 108 | #define EHL_UNLOCK(p) lck_mtx_unlock(&(p)->el_lock) |
| 109 | #define EHL_LOCK_ASSERT(p, x) LCK_MTX_ASSERT(&(p)->el_lock, x) |
| 110 | #define EHL_LOCK_DESTROY(p) lck_mtx_destroy(&(p)->el_lock, &el_lock_grp) |
| 111 | |
| 112 | #define evhlog(x) do { if (evh_debug >= 1) log x; } while (0) |
| 113 | |
| 114 | /* |
| 115 | * Macro to invoke the handlers for a given event. |
| 116 | */ |
| 117 | #define _EVENTHANDLER_INVOKE(name, list, ...) do { \ |
| 118 | struct eventhandler_entry * _ep __single; \ |
| 119 | struct eventhandler_entry_ ## name * _t __single; \ |
| 120 | \ |
| 121 | VERIFY((list)->el_flags & EHL_INITTED); \ |
| 122 | EHL_LOCK_ASSERT((list), LCK_MTX_ASSERT_OWNED); \ |
| 123 | (list)->el_runcount++; \ |
| 124 | VERIFY((list)->el_runcount > 0); \ |
| 125 | evhlog((LOG_DEBUG, "eventhandler_invoke(\"" __STRING(name) "\")")); \ |
| 126 | TAILQ_FOREACH(_ep, &((list)->el_entries), ee_link) { \ |
| 127 | if (_ep->ee_priority != EHE_DEAD_PRIORITY) { \ |
| 128 | EHL_UNLOCK((list)); \ |
| 129 | _t = (struct eventhandler_entry_ ## name *)_ep; \ |
| 130 | evhlog((LOG_DEBUG, "eventhandler_invoke: executing %p", \ |
| 131 | (void *)VM_KERNEL_UNSLIDE((uint64_t)(_t->eh_func)))); \ |
| 132 | _t->eh_func(_ep->ee_arg , ## __VA_ARGS__); \ |
| 133 | EHL_LOCK_SPIN((list)); \ |
| 134 | } \ |
| 135 | } \ |
| 136 | VERIFY((list)->el_runcount > 0); \ |
| 137 | (list)->el_runcount--; \ |
| 138 | if ((list)->el_runcount == 0) { \ |
| 139 | EHL_LOCK_CONVERT((list)); \ |
| 140 | eventhandler_prune_list(list); \ |
| 141 | } \ |
| 142 | EHL_UNLOCK((list)); \ |
| 143 | } while (0) |
| 144 | |
| 145 | /* |
| 146 | * Slow handlers are entirely dynamic; lists are created |
| 147 | * when entries are added to them, and thus have no concept of "owner", |
| 148 | * |
| 149 | * Slow handlers need to be declared, but do not need to be defined. The |
| 150 | * declaration must be in scope wherever the handler is to be invoked. |
| 151 | */ |
| 152 | #define EVENTHANDLER_DECLARE(name, type) \ |
| 153 | struct eventhandler_entry_ ## name \ |
| 154 | { \ |
| 155 | struct eventhandler_entry ee; \ |
| 156 | type eh_func; \ |
| 157 | }; \ |
| 158 | struct __hack |
| 159 | |
| 160 | /* |
| 161 | * XXX EVENTHANDLER_DEFINE by itself doesn't do much on XNU |
| 162 | * All it does is that it declares the static eventhandler_tag |
| 163 | * and defines an init routine that still needs to called to put the |
| 164 | * event and callback on the list. |
| 165 | */ |
| 166 | #define EVENTHANDLER_DEFINE(evthdlr_ref, name, func, arg, priority) \ |
| 167 | static eventhandler_tag name ## _tag; \ |
| 168 | static void name ## _evh_init(void *ctx) \ |
| 169 | { \ |
| 170 | name ## _tag = EVENTHANDLER_REGISTER(evthdlr_ref, name, func, ctx, \ |
| 171 | priority); \ |
| 172 | } \ |
| 173 | SYSINIT(name ## _evh_init, SI_SUB_CONFIGURE, SI_ORDER_ANY, \ |
| 174 | name ## _evh_init, arg); \ |
| 175 | struct __hack |
| 176 | |
| 177 | #define EVENTHANDLER_INVOKE(evthdlr_ref, name, ...) \ |
| 178 | do { \ |
| 179 | struct eventhandler_list *__single _el; \ |
| 180 | \ |
| 181 | if ((_el = eventhandler_find_list(evthdlr_ref, #name)) != NULL) \ |
| 182 | _EVENTHANDLER_INVOKE(name, _el , ## __VA_ARGS__); \ |
| 183 | } while (0) |
| 184 | |
| 185 | #define EVENTHANDLER_REGISTER(evthdlr_ref, name, func, arg, priority) \ |
| 186 | eventhandler_register(evthdlr_ref, NULL, #name, ptrauth_nop_cast(void * __single, &func), arg, priority) |
| 187 | |
| 188 | #define EVENTHANDLER_DEREGISTER(evthdlr_ref, name, tag) \ |
| 189 | do { \ |
| 190 | struct eventhandler_list *__single _el; \ |
| 191 | \ |
| 192 | if ((_el = eventhandler_find_list(evthdlr_ref, #name)) != NULL) \ |
| 193 | eventhandler_deregister(_el, tag); \ |
| 194 | } while(0) |
| 195 | |
| 196 | void eventhandler_init(void); |
| 197 | eventhandler_tag eventhandler_register(struct eventhandler_lists_ctxt *evthdlr_lists_ctxt, |
| 198 | struct eventhandler_list *list, const char *name, void *func, struct eventhandler_entry_arg arg, int priority); |
| 199 | void eventhandler_deregister(struct eventhandler_list *list, |
| 200 | eventhandler_tag tag); |
| 201 | struct eventhandler_list *eventhandler_find_list( |
| 202 | struct eventhandler_lists_ctxt *evthdlr_lists_ctxt, const char *name); |
| 203 | void eventhandler_prune_list(struct eventhandler_list *list); |
| 204 | void eventhandler_lists_ctxt_init(struct eventhandler_lists_ctxt *evthdlr_lists_ctxt); |
| 205 | void eventhandler_lists_ctxt_destroy(struct eventhandler_lists_ctxt *evthdlr_lists_ctxt); |
| 206 | |
| 207 | /* Generic priority levels */ |
| 208 | #define EVENTHANDLER_PRI_FIRST 0 |
| 209 | #define EVENTHANDLER_PRI_ANY 10000 |
| 210 | #define EVENTHANDLER_PRI_LAST 20000 |
| 211 | |
| 212 | #endif /* _SYS_EVENTHANDLER_H_ */ |
| 213 | |