kern_module_hook.c revision 1.2 1 1.2 skrll /* $NetBSD: kern_module_hook.c,v 1.2 2019/12/13 07:59:36 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.2 skrll __KERNEL_RCSID(0, "$NetBSD: kern_module_hook.c,v 1.2 2019/12/13 07:59:36 skrll Exp $");
35 1.1 pgoyette
36 1.1 pgoyette #include <sys/param.h>
37 1.2 skrll #include <sys/atomic.h>
38 1.1 pgoyette #include <sys/module_hook.h>
39 1.1 pgoyette #include <sys/mutex.h>
40 1.1 pgoyette #include <sys/condvar.h>
41 1.1 pgoyette #include <sys/pserialize.h>
42 1.1 pgoyette
43 1.1 pgoyette #include <uvm/uvm_extern.h>
44 1.1 pgoyette
45 1.1 pgoyette /* Locking/synchronization stuff for module hooks */
46 1.1 pgoyette
47 1.1 pgoyette static struct {
48 1.1 pgoyette kmutex_t mtx;
49 1.1 pgoyette kcondvar_t cv;
50 1.1 pgoyette pserialize_t psz;
51 1.1 pgoyette } module_hook __cacheline_aligned;
52 1.1 pgoyette
53 1.1 pgoyette /*
54 1.1 pgoyette * We use pserialize_perform() to issue a memory barrier on the current
55 1.1 pgoyette * CPU and on all other CPUs so that all prior memory operations on the
56 1.1 pgoyette * current CPU globally happen before all subsequent memory operations
57 1.1 pgoyette * on the current CPU, as perceived by any other CPU.
58 1.1 pgoyette *
59 1.1 pgoyette * pserialize_perform() might be rather heavy-weight here, but it only
60 1.1 pgoyette * happens during module loading, and it allows MODULE_HOOK_CALL() to
61 1.1 pgoyette * work without any other memory barriers.
62 1.1 pgoyette */
63 1.1 pgoyette
64 1.1 pgoyette void
65 1.1 pgoyette module_hook_set(bool *hooked, struct localcount *lc)
66 1.1 pgoyette {
67 1.1 pgoyette
68 1.1 pgoyette KASSERT(kernconfig_is_held());
69 1.1 pgoyette KASSERT(!*hooked);
70 1.1 pgoyette
71 1.1 pgoyette localcount_init(lc);
72 1.1 pgoyette
73 1.1 pgoyette /* Wait until setup has been witnessed by all CPUs. */
74 1.1 pgoyette pserialize_perform(module_hook.psz);
75 1.1 pgoyette
76 1.1 pgoyette /* Let others use it */
77 1.1 pgoyette atomic_store_relaxed(hooked, true);
78 1.1 pgoyette }
79 1.1 pgoyette
80 1.1 pgoyette void
81 1.1 pgoyette module_hook_unset(bool *hooked, struct localcount *lc)
82 1.1 pgoyette {
83 1.1 pgoyette
84 1.1 pgoyette KASSERT(kernconfig_is_held());
85 1.1 pgoyette KASSERT(*hooked);
86 1.1 pgoyette
87 1.1 pgoyette /* Get exclusive with pserialize and localcount. */
88 1.1 pgoyette mutex_enter(&module_hook.mtx);
89 1.1 pgoyette
90 1.1 pgoyette /* Prevent new calls to module_hook_tryenter(). */
91 1.1 pgoyette atomic_store_relaxed(hooked, false);
92 1.1 pgoyette
93 1.1 pgoyette /* Wait for existing calls to module_hook_tryenter(). */
94 1.1 pgoyette pserialize_perform(module_hook.psz);
95 1.1 pgoyette
96 1.1 pgoyette /* Wait for module_hook_exit. */
97 1.1 pgoyette localcount_drain(lc, &module_hook.cv, &module_hook.mtx);
98 1.1 pgoyette
99 1.1 pgoyette /* All done! */
100 1.1 pgoyette mutex_exit(&module_hook.mtx);
101 1.1 pgoyette localcount_fini(lc);
102 1.1 pgoyette }
103 1.1 pgoyette
104 1.1 pgoyette bool
105 1.1 pgoyette module_hook_tryenter(bool *hooked, struct localcount *lc)
106 1.1 pgoyette {
107 1.1 pgoyette bool call_hook;
108 1.1 pgoyette int s;
109 1.1 pgoyette
110 1.1 pgoyette s = pserialize_read_enter();
111 1.1 pgoyette call_hook = atomic_load_relaxed(hooked);
112 1.1 pgoyette if (call_hook)
113 1.1 pgoyette localcount_acquire(lc);
114 1.1 pgoyette pserialize_read_exit(s);
115 1.1 pgoyette
116 1.1 pgoyette return call_hook;
117 1.1 pgoyette }
118 1.1 pgoyette
119 1.1 pgoyette void
120 1.1 pgoyette module_hook_exit(struct localcount *lc)
121 1.1 pgoyette {
122 1.1 pgoyette
123 1.1 pgoyette localcount_release(lc, &module_hook.cv, &module_hook.mtx);
124 1.1 pgoyette }
125 1.1 pgoyette
126 1.1 pgoyette void
127 1.1 pgoyette module_hook_init(void)
128 1.1 pgoyette {
129 1.1 pgoyette
130 1.1 pgoyette mutex_init(&module_hook.mtx, MUTEX_DEFAULT, IPL_NONE);
131 1.1 pgoyette cv_init(&module_hook.cv, "mod_hook");
132 1.1 pgoyette module_hook.psz = pserialize_create();
133 1.1 pgoyette }
134