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