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