1 1.4 skrll /* $NetBSD: kern_module_hook.c,v 1.4 2019/12/13 08:02:53 skrll Exp $ */ 2 1.1 pgoyette 3 1.1 pgoyette /*- 4 1.1 pgoyette * Copyright (c) 2019 The NetBSD Foundation, Inc. 5 1.1 pgoyette * All rights reserved. 6 1.1 pgoyette * 7 1.1 pgoyette * Redistribution and use in source and binary forms, with or without 8 1.1 pgoyette * modification, are permitted provided that the following conditions 9 1.1 pgoyette * are met: 10 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright 11 1.1 pgoyette * notice, this list of conditions and the following disclaimer. 12 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the 14 1.1 pgoyette * documentation and/or other materials provided with the distribution. 15 1.1 pgoyette * 16 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 pgoyette * POSSIBILITY OF SUCH DAMAGE. 27 1.1 pgoyette */ 28 1.1 pgoyette 29 1.1 pgoyette /* 30 1.1 pgoyette * Kernel module support. 31 1.1 pgoyette */ 32 1.1 pgoyette 33 1.1 pgoyette #include <sys/cdefs.h> 34 1.4 skrll __KERNEL_RCSID(0, "$NetBSD: kern_module_hook.c,v 1.4 2019/12/13 08:02:53 skrll Exp $"); 35 1.1 pgoyette 36 1.1 pgoyette #include <sys/param.h> 37 1.3 skrll 38 1.2 skrll #include <sys/atomic.h> 39 1.3 skrll #include <sys/condvar.h> 40 1.1 pgoyette #include <sys/module_hook.h> 41 1.1 pgoyette #include <sys/mutex.h> 42 1.1 pgoyette #include <sys/pserialize.h> 43 1.1 pgoyette 44 1.1 pgoyette #include <uvm/uvm_extern.h> 45 1.1 pgoyette 46 1.1 pgoyette /* Locking/synchronization stuff for module hooks */ 47 1.1 pgoyette 48 1.1 pgoyette static struct { 49 1.1 pgoyette kmutex_t mtx; 50 1.1 pgoyette kcondvar_t cv; 51 1.1 pgoyette pserialize_t psz; 52 1.1 pgoyette } module_hook __cacheline_aligned; 53 1.1 pgoyette 54 1.4 skrll /* 55 1.1 pgoyette * We use pserialize_perform() to issue a memory barrier on the current 56 1.4 skrll * CPU and on all other CPUs so that all prior memory operations on the 57 1.4 skrll * current CPU globally happen before all subsequent memory operations 58 1.1 pgoyette * on the current CPU, as perceived by any other CPU. 59 1.4 skrll * 60 1.1 pgoyette * pserialize_perform() might be rather heavy-weight here, but it only 61 1.1 pgoyette * happens during module loading, and it allows MODULE_HOOK_CALL() to 62 1.1 pgoyette * work without any other memory barriers. 63 1.1 pgoyette */ 64 1.1 pgoyette 65 1.1 pgoyette void 66 1.1 pgoyette module_hook_set(bool *hooked, struct localcount *lc) 67 1.1 pgoyette { 68 1.1 pgoyette 69 1.1 pgoyette KASSERT(kernconfig_is_held()); 70 1.1 pgoyette KASSERT(!*hooked); 71 1.1 pgoyette 72 1.1 pgoyette localcount_init(lc); 73 1.1 pgoyette 74 1.1 pgoyette /* Wait until setup has been witnessed by all CPUs. */ 75 1.1 pgoyette pserialize_perform(module_hook.psz); 76 1.1 pgoyette 77 1.1 pgoyette /* Let others use it */ 78 1.1 pgoyette atomic_store_relaxed(hooked, true); 79 1.1 pgoyette } 80 1.1 pgoyette 81 1.1 pgoyette void 82 1.1 pgoyette module_hook_unset(bool *hooked, struct localcount *lc) 83 1.1 pgoyette { 84 1.1 pgoyette 85 1.1 pgoyette KASSERT(kernconfig_is_held()); 86 1.1 pgoyette KASSERT(*hooked); 87 1.1 pgoyette 88 1.1 pgoyette /* Get exclusive with pserialize and localcount. */ 89 1.1 pgoyette mutex_enter(&module_hook.mtx); 90 1.1 pgoyette 91 1.1 pgoyette /* Prevent new calls to module_hook_tryenter(). */ 92 1.1 pgoyette atomic_store_relaxed(hooked, false); 93 1.1 pgoyette 94 1.1 pgoyette /* Wait for existing calls to module_hook_tryenter(). */ 95 1.1 pgoyette pserialize_perform(module_hook.psz); 96 1.1 pgoyette 97 1.1 pgoyette /* Wait for module_hook_exit. */ 98 1.1 pgoyette localcount_drain(lc, &module_hook.cv, &module_hook.mtx); 99 1.1 pgoyette 100 1.1 pgoyette /* All done! */ 101 1.1 pgoyette mutex_exit(&module_hook.mtx); 102 1.1 pgoyette localcount_fini(lc); 103 1.1 pgoyette } 104 1.1 pgoyette 105 1.1 pgoyette bool 106 1.1 pgoyette module_hook_tryenter(bool *hooked, struct localcount *lc) 107 1.1 pgoyette { 108 1.1 pgoyette bool call_hook; 109 1.1 pgoyette int s; 110 1.1 pgoyette 111 1.1 pgoyette s = pserialize_read_enter(); 112 1.1 pgoyette call_hook = atomic_load_relaxed(hooked); 113 1.1 pgoyette if (call_hook) 114 1.1 pgoyette localcount_acquire(lc); 115 1.1 pgoyette pserialize_read_exit(s); 116 1.1 pgoyette 117 1.1 pgoyette return call_hook; 118 1.1 pgoyette } 119 1.1 pgoyette 120 1.1 pgoyette void 121 1.1 pgoyette module_hook_exit(struct localcount *lc) 122 1.1 pgoyette { 123 1.1 pgoyette 124 1.1 pgoyette localcount_release(lc, &module_hook.cv, &module_hook.mtx); 125 1.1 pgoyette } 126 1.1 pgoyette 127 1.1 pgoyette void 128 1.1 pgoyette module_hook_init(void) 129 1.1 pgoyette { 130 1.1 pgoyette 131 1.1 pgoyette mutex_init(&module_hook.mtx, MUTEX_DEFAULT, IPL_NONE); 132 1.1 pgoyette cv_init(&module_hook.cv, "mod_hook"); 133 1.1 pgoyette module_hook.psz = pserialize_create(); 134 1.1 pgoyette } 135