3 * John Baldwin <jhb@FreeBSD.org>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY JOHN BALDWIN AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL JOHN BALDWIN OR THE VOICES IN HIS HEAD
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGE.
33 * This module holds the global variables and machine independent functions
34 * used for the kernel SMP support.
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
43 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
48 #include <machine/smp.h>
50 volatile u_int stopped_cpus;
51 volatile u_int started_cpus;
53 void (*cpustop_restartfunc)(void);
56 volatile int smp_started;
60 SYSCTL_NODE(_kern, OID_AUTO, smp, CTLFLAG_RD, NULL, "Kernel SMP");
62 int smp_active = 0; /* are the APs allowed to run? */
63 SYSCTL_INT(_kern_smp, OID_AUTO, active, CTLFLAG_RW, &smp_active, 0, "");
65 int smp_cpus = 1; /* how many cpu's running */
66 SYSCTL_INT(_kern_smp, OID_AUTO, cpus, CTLFLAG_RD, &smp_cpus, 0, "");
68 /* Enable forwarding of a signal to a process running on a different CPU */
69 static int forward_signal_enabled = 1;
70 SYSCTL_INT(_kern_smp, OID_AUTO, forward_signal_enabled, CTLFLAG_RW,
71 &forward_signal_enabled, 0, "");
73 /* Enable forwarding of roundrobin to all other cpus */
74 static int forward_roundrobin_enabled = 1;
75 SYSCTL_INT(_kern_smp, OID_AUTO, forward_roundrobin_enabled, CTLFLAG_RW,
76 &forward_roundrobin_enabled, 0, "");
78 /* Variables needed for SMP rendezvous. */
79 static void (*smp_rv_setup_func)(void *arg);
80 static void (*smp_rv_action_func)(void *arg);
81 static void (*smp_rv_teardown_func)(void *arg);
82 static void *smp_rv_func_arg;
83 static volatile int smp_rv_waiters[2];
84 static struct mtx smp_rv_mtx;
85 static int mp_probe_status;
88 * Initialize MI SMP variables.
93 mp_probe_status = cpu_mp_probe();
95 SYSINIT(cpu_mp_probe, SI_SUB_TUNABLES, SI_ORDER_FIRST, mp_probe, NULL)
98 * Call the MD SMP initialization code.
101 mp_start(void *dummy)
104 /* Probe for MP hardware. */
105 if (mp_probe_status == 0)
108 mtx_init(&smp_rv_mtx, "smp rendezvous", NULL, MTX_SPIN);
110 printf("FreeBSD/SMP: Multiprocessor System Detected: %d CPUs\n",
114 SYSINIT(cpu_mp, SI_SUB_CPU, SI_ORDER_SECOND, mp_start, NULL)
117 forward_signal(struct thread *td)
122 * signotify() has already set KEF_ASTPENDING and PS_NEEDSIGCHECK on
123 * this process, so all we need to do is poke it if it is currently
124 * executing so that it executes ast().
126 mtx_assert(&sched_lock, MA_OWNED);
127 KASSERT(td->td_state == TDS_RUNNING,
128 ("forward_signal: thread is not TDS_RUNNING"));
130 CTR1(KTR_SMP, "forward_signal(%p)", td->td_proc);
132 if (!smp_started || cold || panicstr)
134 if (!forward_signal_enabled)
137 /* No need to IPI ourself. */
141 id = td->td_kse->ke_oncpu;
144 ipi_selected(1 << id, IPI_AST);
148 forward_roundrobin(void)
154 mtx_assert(&sched_lock, MA_OWNED);
156 CTR0(KTR_SMP, "forward_roundrobin()");
158 if (!smp_started || cold || panicstr)
160 if (!forward_roundrobin_enabled)
163 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
164 td = pc->pc_curthread;
166 if (id != PCPU_GET(cpumask) && (id & stopped_cpus) == 0 &&
167 td != pc->pc_idlethread) {
168 td->td_kse->ke_flags |= KEF_NEEDRESCHED;
172 ipi_selected(map, IPI_AST);
176 * When called the executing CPU will send an IPI to all other CPUs
177 * requesting that they halt execution.
179 * Usually (but not necessarily) called with 'other_cpus' as its arg.
181 * - Signals all CPUs in map to stop.
182 * - Waits for each to stop.
189 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs
190 * from executing at same time.
200 CTR1(KTR_SMP, "stop_cpus(%x)", map);
202 /* send the stop IPI to all CPUs in map */
203 ipi_selected(map, IPI_STOP);
206 while ((atomic_load_acq_int(&stopped_cpus) & map) != map) {
211 printf("timeout stopping cpus\n");
222 * Called by a CPU to restart stopped CPUs.
224 * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
226 * - Signals all CPUs in map to restart.
227 * - Waits for each to restart.
235 restart_cpus(u_int map)
241 CTR1(KTR_SMP, "restart_cpus(%x)", map);
243 /* signal other cpus to restart */
244 atomic_store_rel_int(&started_cpus, map);
246 /* wait for each to clear its bit */
247 while ((atomic_load_acq_int(&stopped_cpus) & map) != 0)
254 * All-CPU rendezvous. CPUs are signalled, all execute the setup function
255 * (if specified), rendezvous, execute the action function (if specified),
256 * rendezvous again, execute the teardown function (if specified), and then
259 * Note that the supplied external functions _must_ be reentrant and aware
260 * that they are running in parallel and in an unknown lock context.
263 smp_rendezvous_action(void)
267 if (smp_rv_setup_func != NULL)
268 smp_rv_setup_func(smp_rv_func_arg);
269 /* spin on entry rendezvous */
270 atomic_add_int(&smp_rv_waiters[0], 1);
271 while (atomic_load_acq_int(&smp_rv_waiters[0]) < mp_ncpus)
273 /* action function */
274 if (smp_rv_action_func != NULL)
275 smp_rv_action_func(smp_rv_func_arg);
276 /* spin on exit rendezvous */
277 atomic_add_int(&smp_rv_waiters[1], 1);
278 while (atomic_load_acq_int(&smp_rv_waiters[1]) < mp_ncpus)
280 /* teardown function */
281 if (smp_rv_teardown_func != NULL)
282 smp_rv_teardown_func(smp_rv_func_arg);
286 smp_rendezvous(void (* setup_func)(void *),
287 void (* action_func)(void *),
288 void (* teardown_func)(void *),
293 if (setup_func != NULL)
295 if (action_func != NULL)
297 if (teardown_func != NULL)
302 /* obtain rendezvous lock */
303 mtx_lock_spin(&smp_rv_mtx);
305 /* set static function pointers */
306 smp_rv_setup_func = setup_func;
307 smp_rv_action_func = action_func;
308 smp_rv_teardown_func = teardown_func;
309 smp_rv_func_arg = arg;
310 smp_rv_waiters[0] = 0;
311 smp_rv_waiters[1] = 0;
313 /* signal other processors, which will enter the IPI with interrupts off */
314 ipi_all_but_self(IPI_RENDEZVOUS);
316 /* call executor function */
317 smp_rendezvous_action();
320 mtx_unlock_spin(&smp_rv_mtx);