1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1999 Michael Smith <msmith@freebsd.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #ifndef _SYS_EVENTHANDLER_H_ 32 #define _SYS_EVENTHANDLER_H_ 33 34 #include <sys/lock.h> 35 #include <sys/ktr.h> 36 #include <sys/mutex.h> 37 #include <sys/queue.h> 38 39 struct eventhandler_entry { 40 TAILQ_ENTRY(eventhandler_entry) ee_link; 41 int ee_priority; 42 #define EHE_DEAD_PRIORITY (-1) 43 void *ee_arg; 44 }; 45 46 #ifdef VIMAGE 47 struct eventhandler_entry_vimage { 48 void (* func)(void); /* Original function registered. */ 49 void *ee_arg; /* Original argument registered. */ 50 void *sparep[2]; 51 }; 52 #endif 53 54 struct eventhandler_list { 55 char *el_name; 56 int el_flags; /* Unused. */ 57 u_int el_runcount; 58 struct mtx el_lock; 59 TAILQ_ENTRY(eventhandler_list) el_link; 60 TAILQ_HEAD(,eventhandler_entry) el_entries; 61 }; 62 63 typedef struct eventhandler_entry *eventhandler_tag; 64 65 #define EHL_LOCK(p) mtx_lock(&(p)->el_lock) 66 #define EHL_UNLOCK(p) mtx_unlock(&(p)->el_lock) 67 #define EHL_LOCK_ASSERT(p, x) mtx_assert(&(p)->el_lock, x) 68 69 /* 70 * Macro to invoke the handlers for a given event. 71 */ 72 #define _EVENTHANDLER_INVOKE(name, list, ...) do { \ 73 struct eventhandler_entry *_ep; \ 74 struct eventhandler_entry_ ## name *_t; \ 75 \ 76 EHL_LOCK_ASSERT((list), MA_OWNED); \ 77 (list)->el_runcount++; \ 78 KASSERT((list)->el_runcount > 0, \ 79 ("eventhandler_invoke: runcount overflow")); \ 80 CTR0(KTR_EVH, "eventhandler_invoke(\"" __STRING(name) "\")"); \ 81 TAILQ_FOREACH(_ep, &((list)->el_entries), ee_link) { \ 82 if (_ep->ee_priority != EHE_DEAD_PRIORITY) { \ 83 EHL_UNLOCK((list)); \ 84 _t = (struct eventhandler_entry_ ## name *)_ep; \ 85 CTR1(KTR_EVH, "eventhandler_invoke: executing %p", \ 86 (void *)_t->eh_func); \ 87 _t->eh_func(_ep->ee_arg , ## __VA_ARGS__); \ 88 EHL_LOCK((list)); \ 89 } \ 90 } \ 91 KASSERT((list)->el_runcount > 0, \ 92 ("eventhandler_invoke: runcount underflow")); \ 93 (list)->el_runcount--; \ 94 if ((list)->el_runcount == 0) \ 95 eventhandler_prune_list(list); \ 96 EHL_UNLOCK((list)); \ 97 } while (0) 98 99 /* 100 * You can optionally use the EVENTHANDLER_LIST and EVENTHANDLER_DIRECT macros 101 * to pre-define a symbol for the eventhandler list. This symbol can be used by 102 * EVENTHANDLER_DIRECT_INVOKE, which has the advantage of not needing to do a 103 * locked search of the global list of eventhandler lists. At least 104 * EVENTHANDLER_LIST_DEFINE must be be used for EVENTHANDLER_DIRECT_INVOKE to 105 * work. EVENTHANDLER_LIST_DECLARE is only needed if the call to 106 * EVENTHANDLER_DIRECT_INVOKE is in a different compilation unit from 107 * EVENTHANDLER_LIST_DEFINE. If the events are even relatively high frequency 108 * it is suggested that you directly define a list for them. 109 */ 110 #define EVENTHANDLER_LIST_DECLARE(name) \ 111 extern struct eventhandler_list *_eventhandler_list_ ## name \ 112 113 #define EVENTHANDLER_LIST_DEFINE(name) \ 114 struct eventhandler_list *_eventhandler_list_ ## name ; \ 115 static void _ehl_init_ ## name (void * ctx __unused) \ 116 { \ 117 _eventhandler_list_ ## name = eventhandler_create_list(#name); \ 118 } \ 119 SYSINIT(name ## _ehl_init, SI_SUB_EVENTHANDLER, SI_ORDER_ANY, \ 120 _ehl_init_ ## name, NULL); \ 121 struct __hack 122 123 #define EVENTHANDLER_DIRECT_INVOKE(name, ...) do { \ 124 struct eventhandler_list *_el; \ 125 \ 126 _el = _eventhandler_list_ ## name ; \ 127 if (!TAILQ_EMPTY(&_el->el_entries)) { \ 128 EHL_LOCK(_el); \ 129 _EVENTHANDLER_INVOKE(name, _el , ## __VA_ARGS__); \ 130 } \ 131 } while (0) 132 133 /* 134 * Event handlers need to be declared, but do not need to be defined. The 135 * declaration must be in scope wherever the handler is to be invoked. 136 */ 137 #define EVENTHANDLER_DECLARE(name, type) \ 138 struct eventhandler_entry_ ## name \ 139 { \ 140 struct eventhandler_entry ee; \ 141 type eh_func; \ 142 }; \ 143 struct __hack 144 145 #define EVENTHANDLER_DEFINE(name, func, arg, priority) \ 146 static eventhandler_tag name ## _tag; \ 147 static void name ## _evh_init(void *ctx) \ 148 { \ 149 name ## _tag = EVENTHANDLER_REGISTER(name, func, ctx, \ 150 priority); \ 151 } \ 152 SYSINIT(name ## _evh_init, SI_SUB_CONFIGURE, SI_ORDER_ANY, \ 153 name ## _evh_init, arg); \ 154 struct __hack 155 156 #define EVENTHANDLER_INVOKE(name, ...) \ 157 do { \ 158 struct eventhandler_list *_el; \ 159 \ 160 if ((_el = eventhandler_find_list(#name)) != NULL) \ 161 _EVENTHANDLER_INVOKE(name, _el , ## __VA_ARGS__); \ 162 } while (0) 163 164 #define EVENTHANDLER_REGISTER(name, func, arg, priority) \ 165 eventhandler_register(NULL, #name, func, arg, priority) 166 167 #define EVENTHANDLER_DEREGISTER(name, tag) \ 168 do { \ 169 struct eventhandler_list *_el; \ 170 \ 171 if ((_el = eventhandler_find_list(#name)) != NULL) \ 172 eventhandler_deregister(_el, tag); \ 173 } while(0) 174 175 #define EVENTHANDLER_DEREGISTER_NOWAIT(name, tag) \ 176 do { \ 177 struct eventhandler_list *_el; \ 178 \ 179 if ((_el = eventhandler_find_list(#name)) != NULL) \ 180 eventhandler_deregister_nowait(_el, tag); \ 181 } while(0) 182 183 eventhandler_tag eventhandler_register(struct eventhandler_list *list, 184 const char *name, void *func, void *arg, int priority); 185 void eventhandler_deregister(struct eventhandler_list *list, 186 eventhandler_tag tag); 187 void eventhandler_deregister_nowait(struct eventhandler_list *list, 188 eventhandler_tag tag); 189 struct eventhandler_list *eventhandler_find_list(const char *name); 190 void eventhandler_prune_list(struct eventhandler_list *list); 191 struct eventhandler_list *eventhandler_create_list(const char *name); 192 193 #ifdef VIMAGE 194 typedef void (*vimage_iterator_func_t)(void *, ...); 195 196 eventhandler_tag vimage_eventhandler_register(struct eventhandler_list *list, 197 const char *name, void *func, void *arg, int priority, 198 vimage_iterator_func_t); 199 #endif 200 201 /* 202 * Standard system event queues. 203 */ 204 205 /* Generic priority levels */ 206 #define EVENTHANDLER_PRI_FIRST 0 207 #define EVENTHANDLER_PRI_ANY 10000 208 #define EVENTHANDLER_PRI_LAST 20000 209 210 /* Shutdown events */ 211 typedef void (*shutdown_fn)(void *, int); 212 213 #define SHUTDOWN_PRI_FIRST EVENTHANDLER_PRI_FIRST 214 #define SHUTDOWN_PRI_DEFAULT EVENTHANDLER_PRI_ANY 215 #define SHUTDOWN_PRI_LAST EVENTHANDLER_PRI_LAST 216 217 EVENTHANDLER_DECLARE(shutdown_pre_sync, shutdown_fn); /* before fs sync */ 218 EVENTHANDLER_DECLARE(shutdown_post_sync, shutdown_fn); /* after fs sync */ 219 EVENTHANDLER_DECLARE(shutdown_final, shutdown_fn); 220 221 /* Power state change events */ 222 typedef void (*power_change_fn)(void *); 223 EVENTHANDLER_DECLARE(power_resume, power_change_fn); 224 EVENTHANDLER_DECLARE(power_suspend, power_change_fn); 225 EVENTHANDLER_DECLARE(power_suspend_early, power_change_fn); 226 227 /* Low memory event */ 228 typedef void (*vm_lowmem_handler_t)(void *, int); 229 #define LOWMEM_PRI_DEFAULT EVENTHANDLER_PRI_FIRST 230 EVENTHANDLER_DECLARE(vm_lowmem, vm_lowmem_handler_t); 231 232 /* Root mounted event */ 233 typedef void (*mountroot_handler_t)(void *); 234 EVENTHANDLER_DECLARE(mountroot, mountroot_handler_t); 235 236 /* File system mount events */ 237 struct mount; 238 struct vnode; 239 struct thread; 240 typedef void (*vfs_mounted_notify_fn)(void *, struct mount *, struct vnode *, 241 struct thread *); 242 typedef void (*vfs_unmounted_notify_fn)(void *, struct mount *, 243 struct thread *); 244 EVENTHANDLER_DECLARE(vfs_mounted, vfs_mounted_notify_fn); 245 EVENTHANDLER_DECLARE(vfs_unmounted, vfs_unmounted_notify_fn); 246 247 /* 248 * Process events 249 * process_fork and exit handlers are called without Giant. 250 * exec handlers are called with Giant, but that is by accident. 251 */ 252 struct proc; 253 struct image_params; 254 255 typedef void (*exitlist_fn)(void *, struct proc *); 256 typedef void (*forklist_fn)(void *, struct proc *, struct proc *, int); 257 typedef void (*execlist_fn)(void *, struct proc *, struct image_params *); 258 typedef void (*proc_ctor_fn)(void *, struct proc *); 259 typedef void (*proc_dtor_fn)(void *, struct proc *); 260 typedef void (*proc_init_fn)(void *, struct proc *); 261 typedef void (*proc_fini_fn)(void *, struct proc *); 262 EVENTHANDLER_DECLARE(process_ctor, proc_ctor_fn); 263 EVENTHANDLER_DECLARE(process_dtor, proc_dtor_fn); 264 EVENTHANDLER_DECLARE(process_init, proc_init_fn); 265 EVENTHANDLER_DECLARE(process_fini, proc_fini_fn); 266 EVENTHANDLER_DECLARE(process_exit, exitlist_fn); 267 EVENTHANDLER_DECLARE(process_fork, forklist_fn); 268 EVENTHANDLER_DECLARE(process_exec, execlist_fn); 269 270 /* 271 * application dump event 272 */ 273 typedef void (*app_coredump_start_fn)(void *, struct thread *, char *name); 274 typedef void (*app_coredump_progress_fn)(void *, struct thread *td, int byte_count); 275 typedef void (*app_coredump_finish_fn)(void *, struct thread *td); 276 typedef void (*app_coredump_error_fn)(void *, struct thread *td, char *msg, ...); 277 278 EVENTHANDLER_DECLARE(app_coredump_start, app_coredump_start_fn); 279 EVENTHANDLER_DECLARE(app_coredump_progress, app_coredump_progress_fn); 280 EVENTHANDLER_DECLARE(app_coredump_finish, app_coredump_finish_fn); 281 EVENTHANDLER_DECLARE(app_coredump_error, app_coredump_error_fn); 282 283 typedef void (*thread_ctor_fn)(void *, struct thread *); 284 typedef void (*thread_dtor_fn)(void *, struct thread *); 285 typedef void (*thread_fini_fn)(void *, struct thread *); 286 typedef void (*thread_init_fn)(void *, struct thread *); 287 EVENTHANDLER_DECLARE(thread_ctor, thread_ctor_fn); 288 EVENTHANDLER_DECLARE(thread_dtor, thread_dtor_fn); 289 EVENTHANDLER_DECLARE(thread_init, thread_init_fn); 290 EVENTHANDLER_DECLARE(thread_fini, thread_fini_fn); 291 292 typedef void (*uma_zone_chfn)(void *); 293 EVENTHANDLER_DECLARE(nmbclusters_change, uma_zone_chfn); 294 EVENTHANDLER_DECLARE(nmbufs_change, uma_zone_chfn); 295 EVENTHANDLER_DECLARE(maxsockets_change, uma_zone_chfn); 296 297 /* Kernel linker file load and unload events */ 298 struct linker_file; 299 typedef void (*kld_load_fn)(void *, struct linker_file *); 300 typedef void (*kld_unload_fn)(void *, const char *, caddr_t, size_t); 301 typedef void (*kld_unload_try_fn)(void *, struct linker_file *, int *); 302 EVENTHANDLER_DECLARE(kld_load, kld_load_fn); 303 EVENTHANDLER_DECLARE(kld_unload, kld_unload_fn); 304 EVENTHANDLER_DECLARE(kld_unload_try, kld_unload_try_fn); 305 306 /* Generic graphics framebuffer interface */ 307 struct fb_info; 308 typedef void (*register_framebuffer_fn)(void *, struct fb_info *); 309 typedef void (*unregister_framebuffer_fn)(void *, struct fb_info *); 310 EVENTHANDLER_DECLARE(register_framebuffer, register_framebuffer_fn); 311 EVENTHANDLER_DECLARE(unregister_framebuffer, unregister_framebuffer_fn); 312 313 /* Veto ada attachment */ 314 struct cam_path; 315 struct ata_params; 316 typedef void (*ada_probe_veto_fn)(void *, struct cam_path *, 317 struct ata_params *, int *); 318 EVENTHANDLER_DECLARE(ada_probe_veto, ada_probe_veto_fn); 319 320 /* Swap device events */ 321 struct swdevt; 322 typedef void (*swapon_fn)(void *, struct swdevt *); 323 typedef void (*swapoff_fn)(void *, struct swdevt *); 324 EVENTHANDLER_DECLARE(swapon, swapon_fn); 325 EVENTHANDLER_DECLARE(swapoff, swapoff_fn); 326 327 /* newbus device events */ 328 enum evhdev_detach { 329 EVHDEV_DETACH_BEGIN, /* Before detach() is called */ 330 EVHDEV_DETACH_COMPLETE, /* After detach() returns 0 */ 331 EVHDEV_DETACH_FAILED /* After detach() returns err */ 332 }; 333 typedef void (*device_attach_fn)(void *, device_t); 334 typedef void (*device_detach_fn)(void *, device_t, enum evhdev_detach); 335 EVENTHANDLER_DECLARE(device_attach, device_attach_fn); 336 EVENTHANDLER_DECLARE(device_detach, device_detach_fn); 337 338 #endif /* _SYS_EVENTHANDLER_H_ */ 339