2 * Copyright (c) 2001, John Baldwin <jhb@FreeBSD.org>.
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 * 3. 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 THE AUTHOR 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 THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * This module holds the global variables and machine independent functions
32 * used for the kernel SMP support.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
45 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
50 #include <machine/cpu.h>
51 #include <machine/smp.h>
53 #include "opt_sched.h"
56 volatile cpumask_t stopped_cpus;
57 volatile cpumask_t started_cpus;
58 cpumask_t idle_cpus_mask;
59 cpumask_t hlt_cpus_mask;
60 cpumask_t logical_cpus_mask;
62 void (*cpustop_restartfunc)(void);
64 /* This is used in modules that need to work in both SMP and UP. */
68 /* export this for libkvm consumers. */
69 int mp_maxcpus = MAXCPU;
71 struct cpu_top *smp_topology;
72 volatile int smp_started;
75 SYSCTL_NODE(_kern, OID_AUTO, smp, CTLFLAG_RD, NULL, "Kernel SMP");
77 SYSCTL_INT(_kern_smp, OID_AUTO, maxid, CTLFLAG_RD, &mp_maxid, 0,
80 SYSCTL_INT(_kern_smp, OID_AUTO, maxcpus, CTLFLAG_RD, &mp_maxcpus, 0,
81 "Max number of CPUs that the system was compiled for.");
83 int smp_active = 0; /* are the APs allowed to run? */
84 SYSCTL_INT(_kern_smp, OID_AUTO, active, CTLFLAG_RW, &smp_active, 0,
85 "Number of Auxillary Processors (APs) that were successfully started");
87 int smp_disabled = 0; /* has smp been disabled? */
88 SYSCTL_INT(_kern_smp, OID_AUTO, disabled, CTLFLAG_RDTUN, &smp_disabled, 0,
89 "SMP has been disabled from the loader");
90 TUNABLE_INT("kern.smp.disabled", &smp_disabled);
92 int smp_cpus = 1; /* how many cpu's running */
93 SYSCTL_INT(_kern_smp, OID_AUTO, cpus, CTLFLAG_RD, &smp_cpus, 0,
94 "Number of CPUs online");
97 /* Enable forwarding of a signal to a process running on a different CPU */
98 static int forward_signal_enabled = 1;
99 SYSCTL_INT(_kern_smp, OID_AUTO, forward_signal_enabled, CTLFLAG_RW,
100 &forward_signal_enabled, 0,
101 "Forwarding of a signal to a process on a different CPU");
103 /* Enable forwarding of roundrobin to all other cpus */
104 static int forward_roundrobin_enabled = 1;
105 SYSCTL_INT(_kern_smp, OID_AUTO, forward_roundrobin_enabled, CTLFLAG_RW,
106 &forward_roundrobin_enabled, 0,
107 "Forwarding of roundrobin to all other CPUs");
109 /* Variables needed for SMP rendezvous. */
110 static volatile int smp_rv_ncpus;
111 static void (*volatile smp_rv_setup_func)(void *arg);
112 static void (*volatile smp_rv_action_func)(void *arg);
113 static void (*volatile smp_rv_teardown_func)(void *arg);
114 static void * volatile smp_rv_func_arg;
115 static volatile int smp_rv_waiters[3];
118 * Shared mutex to restrict busywaits between smp_rendezvous() and
119 * smp(_targeted)_tlb_shootdown(). A deadlock occurs if both of these
120 * functions trigger at once and cause multiple CPUs to busywait with
121 * interrupts disabled.
123 struct mtx smp_ipi_mtx;
126 * Let the MD SMP code initialize mp_maxid very early if it can.
129 mp_setmaxid(void *dummy)
133 SYSINIT(cpu_mp_setmaxid, SI_SUB_TUNABLES, SI_ORDER_FIRST, mp_setmaxid, NULL);
136 * Call the MD SMP initialization code.
139 mp_start(void *dummy)
142 /* Probe for MP hardware. */
143 if (smp_disabled != 0 || cpu_mp_probe() == 0) {
145 all_cpus = PCPU_GET(cpumask);
149 mtx_init(&smp_ipi_mtx, "smp rendezvous", NULL, MTX_SPIN);
151 printf("FreeBSD/SMP: Multiprocessor System Detected: %d CPUs\n",
155 SYSINIT(cpu_mp, SI_SUB_CPU, SI_ORDER_THIRD, mp_start, NULL);
158 forward_signal(struct thread *td)
163 * signotify() has already set TDF_ASTPENDING and TDF_NEEDSIGCHECK on
164 * this thread, so all we need to do is poke it if it is currently
165 * executing so that it executes ast().
167 THREAD_LOCK_ASSERT(td, MA_OWNED);
168 KASSERT(TD_IS_RUNNING(td),
169 ("forward_signal: thread is not TDS_RUNNING"));
171 CTR1(KTR_SMP, "forward_signal(%p)", td->td_proc);
173 if (!smp_started || cold || panicstr)
175 if (!forward_signal_enabled)
178 /* No need to IPI ourself. */
185 ipi_selected(1 << id, IPI_AST);
189 forward_roundrobin(void)
193 cpumask_t id, map, me;
195 CTR0(KTR_SMP, "forward_roundrobin()");
197 if (!smp_started || cold || panicstr)
199 if (!forward_roundrobin_enabled)
202 me = PCPU_GET(cpumask);
203 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
204 td = pc->pc_curthread;
206 if (id != me && (id & stopped_cpus) == 0 &&
207 !TD_IS_IDLETHREAD(td)) {
208 td->td_flags |= TDF_NEEDRESCHED;
212 ipi_selected(map, IPI_AST);
216 * When called the executing CPU will send an IPI to all other CPUs
217 * requesting that they halt execution.
219 * Usually (but not necessarily) called with 'other_cpus' as its arg.
221 * - Signals all CPUs in map to stop.
222 * - Waits for each to stop.
229 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs
230 * from executing at same time.
233 stop_cpus(cpumask_t map)
240 CTR1(KTR_SMP, "stop_cpus(%x)", map);
242 /* send the stop IPI to all CPUs in map */
243 ipi_selected(map, IPI_STOP);
246 while ((stopped_cpus & map) != map) {
252 printf("timeout stopping cpus\n");
262 * Called by a CPU to restart stopped CPUs.
264 * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
266 * - Signals all CPUs in map to restart.
267 * - Waits for each to restart.
275 restart_cpus(cpumask_t map)
281 CTR1(KTR_SMP, "restart_cpus(%x)", map);
283 /* signal other cpus to restart */
284 atomic_store_rel_int(&started_cpus, map);
286 /* wait for each to clear its bit */
287 while ((stopped_cpus & map) != 0)
294 * All-CPU rendezvous. CPUs are signalled, all execute the setup function
295 * (if specified), rendezvous, execute the action function (if specified),
296 * rendezvous again, execute the teardown function (if specified), and then
299 * Note that the supplied external functions _must_ be reentrant and aware
300 * that they are running in parallel and in an unknown lock context.
303 smp_rendezvous_action(void)
305 void* local_func_arg = smp_rv_func_arg;
306 void (*local_setup_func)(void*) = smp_rv_setup_func;
307 void (*local_action_func)(void*) = smp_rv_action_func;
308 void (*local_teardown_func)(void*) = smp_rv_teardown_func;
310 /* Ensure we have up-to-date values. */
311 atomic_add_acq_int(&smp_rv_waiters[0], 1);
312 while (smp_rv_waiters[0] < smp_rv_ncpus)
316 if (local_setup_func != smp_no_rendevous_barrier) {
317 if (smp_rv_setup_func != NULL)
318 smp_rv_setup_func(smp_rv_func_arg);
320 /* spin on entry rendezvous */
321 atomic_add_int(&smp_rv_waiters[1], 1);
322 while (smp_rv_waiters[1] < smp_rv_ncpus)
326 /* action function */
327 if (local_action_func != NULL)
328 local_action_func(local_func_arg);
330 /* spin on exit rendezvous */
331 atomic_add_int(&smp_rv_waiters[2], 1);
332 if (local_teardown_func == smp_no_rendevous_barrier)
334 while (smp_rv_waiters[2] < smp_rv_ncpus)
337 /* teardown function */
338 if (local_teardown_func != NULL)
339 local_teardown_func(local_func_arg);
343 smp_rendezvous_cpus(cpumask_t map,
344 void (* setup_func)(void *),
345 void (* action_func)(void *),
346 void (* teardown_func)(void *),
352 if (setup_func != NULL)
354 if (action_func != NULL)
356 if (teardown_func != NULL)
361 for (i = 0; i <= mp_maxid; i++)
362 if (((1 << i) & map) != 0 && !CPU_ABSENT(i))
365 panic("ncpus is 0 with map=0x%x", map);
367 /* obtain rendezvous lock */
368 mtx_lock_spin(&smp_ipi_mtx);
370 /* set static function pointers */
371 smp_rv_ncpus = ncpus;
372 smp_rv_setup_func = setup_func;
373 smp_rv_action_func = action_func;
374 smp_rv_teardown_func = teardown_func;
375 smp_rv_func_arg = arg;
376 smp_rv_waiters[1] = 0;
377 smp_rv_waiters[2] = 0;
378 atomic_store_rel_int(&smp_rv_waiters[0], 0);
380 /* signal other processors, which will enter the IPI with interrupts off */
381 ipi_selected(map & ~(1 << curcpu), IPI_RENDEZVOUS);
383 /* Check if the current CPU is in the map */
384 if ((map & (1 << curcpu)) != 0)
385 smp_rendezvous_action();
387 if (teardown_func == smp_no_rendevous_barrier)
388 while (atomic_load_acq_int(&smp_rv_waiters[2]) < ncpus)
392 mtx_unlock_spin(&smp_ipi_mtx);
396 smp_rendezvous(void (* setup_func)(void *),
397 void (* action_func)(void *),
398 void (* teardown_func)(void *),
401 smp_rendezvous_cpus(all_cpus, setup_func, action_func, teardown_func, arg);
406 * Provide dummy SMP support for UP kernels. Modules that need to use SMP
407 * APIs will still work using this dummy support.
410 mp_setvariables_for_up(void *dummy)
413 mp_maxid = PCPU_GET(cpuid);
414 all_cpus = PCPU_GET(cpumask);
415 KASSERT(PCPU_GET(cpuid) == 0, ("UP must have a CPU ID of zero"));
417 SYSINIT(cpu_mp_setvariables, SI_SUB_TUNABLES, SI_ORDER_FIRST,
418 mp_setvariables_for_up, NULL);
421 smp_rendezvous_cpus(cpumask_t map,
422 void (*setup_func)(void *),
423 void (*action_func)(void *),
424 void (*teardown_func)(void *),
427 if (setup_func != NULL)
429 if (action_func != NULL)
431 if (teardown_func != NULL)
436 smp_rendezvous(void (*setup_func)(void *),
437 void (*action_func)(void *),
438 void (*teardown_func)(void *),
442 if (setup_func != NULL)
444 if (action_func != NULL)
446 if (teardown_func != NULL)
452 smp_no_rendevous_barrier(void *dummy)
455 KASSERT((!smp_started),("smp_no_rendevous called and smp is started"));