mtree/BSD.root.dist: Use spaces.
[dragonfly.git] / sys / kern / subr_cpuhelper.c
blobde76b74f57d6ab5946694db8dd9686b1fb89c7e1
1 /*
2 * Copyright (c) 2017 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/thread.h>
40 #include <sys/cpuhelper.h>
42 static struct thread *cpuhelper[MAXCPU];
43 static int (*cpuhelper_saved_putport)(lwkt_port_t, lwkt_msg_t);
45 static __inline lwkt_port_t
46 cpuhelper_port(int cpuid)
49 return (&cpuhelper[cpuid]->td_msgport);
53 * To prevent deadlocking, we must execute these self-referential messages
54 * synchronously, effectively turning the message into a direct procedure
55 * call.
57 static int
58 cpuhelper_putport(lwkt_port_t port, lwkt_msg_t lmsg)
60 struct cpuhelper_msg *msg = (struct cpuhelper_msg *)lmsg;
63 if ((lmsg->ms_flags & MSGF_SYNC) && port == &curthread->td_msgport) {
64 msg->ch_cb(msg);
65 return (EASYNC);
66 } else {
67 return (cpuhelper_saved_putport(port, lmsg));
71 static void
72 cpuhelper_mainloop(void *arg __unused)
74 struct cpuhelper_msg *msg;
77 while ((msg = lwkt_waitport(&curthread->td_msgport, 0))) {
78 KASSERT(msg->ch_cb != NULL, ("cpuhelper%d: badmsg", mycpuid));
79 msg->ch_cb(msg);
81 /* NEVER REACHED */
84 void
85 cpuhelper_initmsg(struct cpuhelper_msg *msg, lwkt_port_t rport,
86 cpuhelper_cb_t cb, void *cbarg, int flags)
89 lwkt_initmsg(&msg->ch_lmsg, rport, flags);
90 msg->ch_cb = cb;
91 msg->ch_cbarg = cbarg;
92 msg->ch_cbarg1 = 0;
95 void
96 cpuhelper_replymsg(struct cpuhelper_msg *msg, int error)
99 lwkt_replymsg(&msg->ch_lmsg, error);
103 cpuhelper_domsg(struct cpuhelper_msg *msg, int cpuid)
106 KASSERT(cpuid >= 0 && cpuid < ncpus, ("invalid cpuid"));
107 return (lwkt_domsg(cpuhelper_port(cpuid), &msg->ch_lmsg, 0));
110 void
111 cpuhelper_assert(int cpuid, bool in)
114 #ifdef INVARIANTS
115 KASSERT(cpuid >= 0 && cpuid < ncpus, ("invalid cpuid"));
116 if (in) {
117 KASSERT(&curthread->td_msgport == cpuhelper_port(cpuid),
118 ("not in cpuhelper%d", cpuid));
119 } else {
120 KASSERT(&curthread->td_msgport != cpuhelper_port(cpuid),
121 ("in cpuhelper%d", cpuid));
123 #endif
126 static void
127 cpuhelper_init(void)
129 int i;
132 * Create per-cpu helper threads.
134 for (i = 0; i < ncpus; ++i) {
135 lwkt_port_t port;
137 lwkt_create(cpuhelper_mainloop, NULL, &cpuhelper[i], NULL,
138 TDF_NOSTART|TDF_FORCE_SPINPORT|TDF_FIXEDCPU, i,
139 "cpuhelper %d", i);
142 * Override the putport function. Our custom function checks
143 * for self-references.
145 port = cpuhelper_port(i);
146 if (cpuhelper_saved_putport == NULL)
147 cpuhelper_saved_putport = port->mp_putport;
148 KKASSERT(cpuhelper_saved_putport == port->mp_putport);
149 port->mp_putport = cpuhelper_putport;
151 lwkt_schedule(cpuhelper[i]);
154 SYSINIT(cpuhelper, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, cpuhelper_init, NULL);