1*fdd8201dSApple OSS Distributions /* 2*fdd8201dSApple OSS Distributions * Copyright (c) 2016-2018 Apple Inc. All rights reserved. 3*fdd8201dSApple OSS Distributions * 4*fdd8201dSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*fdd8201dSApple OSS Distributions * 6*fdd8201dSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*fdd8201dSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*fdd8201dSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*fdd8201dSApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*fdd8201dSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*fdd8201dSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*fdd8201dSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*fdd8201dSApple OSS Distributions * terms of an Apple operating system software license agreement. 14*fdd8201dSApple OSS Distributions * 15*fdd8201dSApple OSS Distributions * Please obtain a copy of the License at 16*fdd8201dSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*fdd8201dSApple OSS Distributions * 18*fdd8201dSApple OSS Distributions * The Original Code and all software distributed under the License are 19*fdd8201dSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*fdd8201dSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*fdd8201dSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*fdd8201dSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*fdd8201dSApple OSS Distributions * Please see the License for the specific language governing rights and 24*fdd8201dSApple OSS Distributions * limitations under the License. 25*fdd8201dSApple OSS Distributions * 26*fdd8201dSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*fdd8201dSApple OSS Distributions */ 28*fdd8201dSApple OSS Distributions /*- 29*fdd8201dSApple OSS Distributions * Copyright (c) 1999 Michael Smith <[email protected]> 30*fdd8201dSApple OSS Distributions * All rights reserved. 31*fdd8201dSApple OSS Distributions * 32*fdd8201dSApple OSS Distributions * Redistribution and use in source and binary forms, with or without 33*fdd8201dSApple OSS Distributions * modification, are permitted provided that the following conditions 34*fdd8201dSApple OSS Distributions * are met: 35*fdd8201dSApple OSS Distributions * 1. Redistributions of source code must retain the above copyright 36*fdd8201dSApple OSS Distributions * notice, this list of conditions and the following disclaimer. 37*fdd8201dSApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright 38*fdd8201dSApple OSS Distributions * notice, this list of conditions and the following disclaimer in the 39*fdd8201dSApple OSS Distributions * documentation and/or other materials provided with the distribution. 40*fdd8201dSApple OSS Distributions * 41*fdd8201dSApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 42*fdd8201dSApple OSS Distributions * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43*fdd8201dSApple OSS Distributions * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44*fdd8201dSApple OSS Distributions * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45*fdd8201dSApple OSS Distributions * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46*fdd8201dSApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47*fdd8201dSApple OSS Distributions * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48*fdd8201dSApple OSS Distributions * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49*fdd8201dSApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50*fdd8201dSApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51*fdd8201dSApple OSS Distributions * SUCH DAMAGE. 52*fdd8201dSApple OSS Distributions * 53*fdd8201dSApple OSS Distributions * $FreeBSD$ 54*fdd8201dSApple OSS Distributions */ 55*fdd8201dSApple OSS Distributions 56*fdd8201dSApple OSS Distributions #ifndef _SYS_EVENTHANDLER_H_ 57*fdd8201dSApple OSS Distributions #define _SYS_EVENTHANDLER_H_ 58*fdd8201dSApple OSS Distributions 59*fdd8201dSApple OSS Distributions #include <ptrauth.h> 60*fdd8201dSApple OSS Distributions 61*fdd8201dSApple OSS Distributions #include <kern/locks.h> 62*fdd8201dSApple OSS Distributions #include <sys/queue.h> 63*fdd8201dSApple OSS Distributions #include <sys/cdefs.h> 64*fdd8201dSApple OSS Distributions #include <sys/syslog.h> 65*fdd8201dSApple OSS Distributions #include <uuid/uuid.h> 66*fdd8201dSApple OSS Distributions 67*fdd8201dSApple OSS Distributions extern int evh_debug; 68*fdd8201dSApple OSS Distributions extern lck_grp_t el_lock_grp; 69*fdd8201dSApple OSS Distributions extern lck_attr_t el_lock_attr; 70*fdd8201dSApple OSS Distributions extern struct eventhandler_entry_arg eventhandler_entry_dummy_arg; 71*fdd8201dSApple OSS Distributions 72*fdd8201dSApple OSS Distributions struct eventhandler_lists_ctxt { 73*fdd8201dSApple OSS Distributions TAILQ_HEAD(, eventhandler_list) eventhandler_lists; 74*fdd8201dSApple OSS Distributions int eventhandler_lists_initted; 75*fdd8201dSApple OSS Distributions decl_lck_mtx_data(, eventhandler_mutex); 76*fdd8201dSApple OSS Distributions }; 77*fdd8201dSApple OSS Distributions 78*fdd8201dSApple OSS Distributions struct eventhandler_entry_arg { 79*fdd8201dSApple OSS Distributions uuid_t ee_fm_uuid; /* Flow manager UUID */ 80*fdd8201dSApple OSS Distributions uuid_t ee_fr_uuid; /* Flow route UUID */ 81*fdd8201dSApple OSS Distributions }; 82*fdd8201dSApple OSS Distributions 83*fdd8201dSApple OSS Distributions struct eventhandler_entry { 84*fdd8201dSApple OSS Distributions TAILQ_ENTRY(eventhandler_entry) ee_link; 85*fdd8201dSApple OSS Distributions int ee_priority; 86*fdd8201dSApple OSS Distributions #define EHE_DEAD_PRIORITY (-1) 87*fdd8201dSApple OSS Distributions struct eventhandler_entry_arg ee_arg; 88*fdd8201dSApple OSS Distributions }; 89*fdd8201dSApple OSS Distributions 90*fdd8201dSApple OSS Distributions #define EVENTHANDLER_MAX_NAME 32 91*fdd8201dSApple OSS Distributions 92*fdd8201dSApple OSS Distributions struct eventhandler_list { 93*fdd8201dSApple OSS Distributions char el_name[EVENTHANDLER_MAX_NAME]; 94*fdd8201dSApple OSS Distributions int el_flags; 95*fdd8201dSApple OSS Distributions #define EHL_INITTED (1<<0) 96*fdd8201dSApple OSS Distributions u_int el_runcount; 97*fdd8201dSApple OSS Distributions decl_lck_mtx_data(, el_lock); 98*fdd8201dSApple OSS Distributions TAILQ_ENTRY(eventhandler_list) el_link; 99*fdd8201dSApple OSS Distributions TAILQ_HEAD(, eventhandler_entry) el_entries; 100*fdd8201dSApple OSS Distributions }; 101*fdd8201dSApple OSS Distributions 102*fdd8201dSApple OSS Distributions typedef struct eventhandler_entry *eventhandler_tag; 103*fdd8201dSApple OSS Distributions 104*fdd8201dSApple OSS Distributions #define EHL_LOCK_INIT(p) lck_mtx_init(&(p)->el_lock, &el_lock_grp, &el_lock_attr) 105*fdd8201dSApple OSS Distributions #define EHL_LOCK(p) lck_mtx_lock(&(p)->el_lock) 106*fdd8201dSApple OSS Distributions #define EHL_LOCK_SPIN(p) lck_mtx_lock_spin(&(p)->el_lock) 107*fdd8201dSApple OSS Distributions #define EHL_LOCK_CONVERT(p) lck_mtx_convert_spin(&(p)->el_lock) 108*fdd8201dSApple OSS Distributions #define EHL_UNLOCK(p) lck_mtx_unlock(&(p)->el_lock) 109*fdd8201dSApple OSS Distributions #define EHL_LOCK_ASSERT(p, x) LCK_MTX_ASSERT(&(p)->el_lock, x) 110*fdd8201dSApple OSS Distributions #define EHL_LOCK_DESTROY(p) lck_mtx_destroy(&(p)->el_lock, &el_lock_grp) 111*fdd8201dSApple OSS Distributions 112*fdd8201dSApple OSS Distributions #define evhlog(x) do { if (evh_debug >= 1) log x; } while (0) 113*fdd8201dSApple OSS Distributions 114*fdd8201dSApple OSS Distributions /* 115*fdd8201dSApple OSS Distributions * Macro to invoke the handlers for a given event. 116*fdd8201dSApple OSS Distributions */ 117*fdd8201dSApple OSS Distributions #define _EVENTHANDLER_INVOKE(name, list, ...) do { \ 118*fdd8201dSApple OSS Distributions struct eventhandler_entry *_ep; \ 119*fdd8201dSApple OSS Distributions struct eventhandler_entry_ ## name *_t; \ 120*fdd8201dSApple OSS Distributions \ 121*fdd8201dSApple OSS Distributions VERIFY((list)->el_flags & EHL_INITTED); \ 122*fdd8201dSApple OSS Distributions EHL_LOCK_ASSERT((list), LCK_MTX_ASSERT_OWNED); \ 123*fdd8201dSApple OSS Distributions (list)->el_runcount++; \ 124*fdd8201dSApple OSS Distributions VERIFY((list)->el_runcount > 0); \ 125*fdd8201dSApple OSS Distributions evhlog((LOG_DEBUG, "eventhandler_invoke(\"" __STRING(name) "\")")); \ 126*fdd8201dSApple OSS Distributions TAILQ_FOREACH(_ep, &((list)->el_entries), ee_link) { \ 127*fdd8201dSApple OSS Distributions if (_ep->ee_priority != EHE_DEAD_PRIORITY) { \ 128*fdd8201dSApple OSS Distributions EHL_UNLOCK((list)); \ 129*fdd8201dSApple OSS Distributions _t = (struct eventhandler_entry_ ## name *)_ep; \ 130*fdd8201dSApple OSS Distributions evhlog((LOG_DEBUG, "eventhandler_invoke: executing %p", \ 131*fdd8201dSApple OSS Distributions (void *)VM_KERNEL_UNSLIDE((void *)_t->eh_func))); \ 132*fdd8201dSApple OSS Distributions _t->eh_func(_ep->ee_arg , ## __VA_ARGS__); \ 133*fdd8201dSApple OSS Distributions EHL_LOCK_SPIN((list)); \ 134*fdd8201dSApple OSS Distributions } \ 135*fdd8201dSApple OSS Distributions } \ 136*fdd8201dSApple OSS Distributions VERIFY((list)->el_runcount > 0); \ 137*fdd8201dSApple OSS Distributions (list)->el_runcount--; \ 138*fdd8201dSApple OSS Distributions if ((list)->el_runcount == 0) { \ 139*fdd8201dSApple OSS Distributions EHL_LOCK_CONVERT((list)); \ 140*fdd8201dSApple OSS Distributions eventhandler_prune_list(list); \ 141*fdd8201dSApple OSS Distributions } \ 142*fdd8201dSApple OSS Distributions EHL_UNLOCK((list)); \ 143*fdd8201dSApple OSS Distributions } while (0) 144*fdd8201dSApple OSS Distributions 145*fdd8201dSApple OSS Distributions /* 146*fdd8201dSApple OSS Distributions * Slow handlers are entirely dynamic; lists are created 147*fdd8201dSApple OSS Distributions * when entries are added to them, and thus have no concept of "owner", 148*fdd8201dSApple OSS Distributions * 149*fdd8201dSApple OSS Distributions * Slow handlers need to be declared, but do not need to be defined. The 150*fdd8201dSApple OSS Distributions * declaration must be in scope wherever the handler is to be invoked. 151*fdd8201dSApple OSS Distributions */ 152*fdd8201dSApple OSS Distributions #define EVENTHANDLER_DECLARE(name, type) \ 153*fdd8201dSApple OSS Distributions struct eventhandler_entry_ ## name \ 154*fdd8201dSApple OSS Distributions { \ 155*fdd8201dSApple OSS Distributions struct eventhandler_entry ee; \ 156*fdd8201dSApple OSS Distributions type eh_func; \ 157*fdd8201dSApple OSS Distributions }; \ 158*fdd8201dSApple OSS Distributions struct __hack 159*fdd8201dSApple OSS Distributions 160*fdd8201dSApple OSS Distributions /* 161*fdd8201dSApple OSS Distributions * XXX EVENTHANDLER_DEFINE by itself doesn't do much on XNU 162*fdd8201dSApple OSS Distributions * All it does is that it declares the static eventhandler_tag 163*fdd8201dSApple OSS Distributions * and defines an init routine that still needs to called to put the 164*fdd8201dSApple OSS Distributions * event and callback on the list. 165*fdd8201dSApple OSS Distributions */ 166*fdd8201dSApple OSS Distributions #define EVENTHANDLER_DEFINE(evthdlr_ref, name, func, arg, priority) \ 167*fdd8201dSApple OSS Distributions static eventhandler_tag name ## _tag; \ 168*fdd8201dSApple OSS Distributions static void name ## _evh_init(void *ctx) \ 169*fdd8201dSApple OSS Distributions { \ 170*fdd8201dSApple OSS Distributions name ## _tag = EVENTHANDLER_REGISTER(evthdlr_ref, name, func, ctx, \ 171*fdd8201dSApple OSS Distributions priority); \ 172*fdd8201dSApple OSS Distributions } \ 173*fdd8201dSApple OSS Distributions SYSINIT(name ## _evh_init, SI_SUB_CONFIGURE, SI_ORDER_ANY, \ 174*fdd8201dSApple OSS Distributions name ## _evh_init, arg); \ 175*fdd8201dSApple OSS Distributions struct __hack 176*fdd8201dSApple OSS Distributions 177*fdd8201dSApple OSS Distributions #define EVENTHANDLER_INVOKE(evthdlr_ref, name, ...) \ 178*fdd8201dSApple OSS Distributions do { \ 179*fdd8201dSApple OSS Distributions struct eventhandler_list *_el; \ 180*fdd8201dSApple OSS Distributions \ 181*fdd8201dSApple OSS Distributions if ((_el = eventhandler_find_list(evthdlr_ref, #name)) != NULL) \ 182*fdd8201dSApple OSS Distributions _EVENTHANDLER_INVOKE(name, _el , ## __VA_ARGS__); \ 183*fdd8201dSApple OSS Distributions } while (0) 184*fdd8201dSApple OSS Distributions 185*fdd8201dSApple OSS Distributions #define EVENTHANDLER_REGISTER(evthdlr_ref, name, func, arg, priority) \ 186*fdd8201dSApple OSS Distributions eventhandler_register(evthdlr_ref, NULL, #name, ptrauth_nop_cast(void *, &func), arg, priority) 187*fdd8201dSApple OSS Distributions 188*fdd8201dSApple OSS Distributions #define EVENTHANDLER_DEREGISTER(evthdlr_ref, name, tag) \ 189*fdd8201dSApple OSS Distributions do { \ 190*fdd8201dSApple OSS Distributions struct eventhandler_list *_el; \ 191*fdd8201dSApple OSS Distributions \ 192*fdd8201dSApple OSS Distributions if ((_el = eventhandler_find_list(evthdlr_ref, #name)) != NULL) \ 193*fdd8201dSApple OSS Distributions eventhandler_deregister(_el, tag); \ 194*fdd8201dSApple OSS Distributions } while(0) 195*fdd8201dSApple OSS Distributions 196*fdd8201dSApple OSS Distributions void eventhandler_init(void); 197*fdd8201dSApple OSS Distributions extern void eventhandler_reap_caches(boolean_t); 198*fdd8201dSApple OSS Distributions eventhandler_tag eventhandler_register(struct eventhandler_lists_ctxt *evthdlr_lists_ctxt, 199*fdd8201dSApple OSS Distributions struct eventhandler_list *list, const char *name, void *func, struct eventhandler_entry_arg arg, int priority); 200*fdd8201dSApple OSS Distributions void eventhandler_deregister(struct eventhandler_list *list, 201*fdd8201dSApple OSS Distributions eventhandler_tag tag); 202*fdd8201dSApple OSS Distributions struct eventhandler_list *eventhandler_find_list( 203*fdd8201dSApple OSS Distributions struct eventhandler_lists_ctxt *evthdlr_lists_ctxt, const char *name); 204*fdd8201dSApple OSS Distributions void eventhandler_prune_list(struct eventhandler_list *list); 205*fdd8201dSApple OSS Distributions void eventhandler_lists_ctxt_init(struct eventhandler_lists_ctxt *evthdlr_lists_ctxt); 206*fdd8201dSApple OSS Distributions void eventhandler_lists_ctxt_destroy(struct eventhandler_lists_ctxt *evthdlr_lists_ctxt); 207*fdd8201dSApple OSS Distributions 208*fdd8201dSApple OSS Distributions /* Generic priority levels */ 209*fdd8201dSApple OSS Distributions #define EVENTHANDLER_PRI_FIRST 0 210*fdd8201dSApple OSS Distributions #define EVENTHANDLER_PRI_ANY 10000 211*fdd8201dSApple OSS Distributions #define EVENTHANDLER_PRI_LAST 20000 212*fdd8201dSApple OSS Distributions 213*fdd8201dSApple OSS Distributions #endif /* _SYS_EVENTHANDLER_H_ */ 214