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.
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/smp.h>
52 #include "opt_sched.h"
55 volatile cpumask_t stopped_cpus;
56 volatile cpumask_t started_cpus;
57 cpumask_t idle_cpus_mask;
58 cpumask_t hlt_cpus_mask;
59 cpumask_t logical_cpus_mask;
61 void (*cpustop_restartfunc)(void);
63 /* This is used in modules that need to work in both SMP and UP. */
67 /* export this for libkvm consumers. */
68 int mp_maxcpus = MAXCPU;
70 struct cpu_top *smp_topology;
71 volatile int smp_started;
74 SYSCTL_NODE(_kern, OID_AUTO, smp, CTLFLAG_RD, NULL, "Kernel SMP");
76 SYSCTL_INT(_kern_smp, OID_AUTO, maxcpus, CTLFLAG_RD, &mp_maxcpus, 0,
77 "Max number of CPUs that the system was compiled for.");
79 int smp_active = 0; /* are the APs allowed to run? */
80 SYSCTL_INT(_kern_smp, OID_AUTO, active, CTLFLAG_RW, &smp_active, 0,
81 "Number of Auxillary Processors (APs) that were successfully started");
83 int smp_disabled = 0; /* has smp been disabled? */
84 SYSCTL_INT(_kern_smp, OID_AUTO, disabled, CTLFLAG_RDTUN, &smp_disabled, 0,
85 "SMP has been disabled from the loader");
86 TUNABLE_INT("kern.smp.disabled", &smp_disabled);
88 int smp_cpus = 1; /* how many cpu's running */
89 SYSCTL_INT(_kern_smp, OID_AUTO, cpus, CTLFLAG_RD, &smp_cpus, 0,
90 "Number of CPUs online");
93 /* Enable forwarding of a signal to a process running on a different CPU */
94 static int forward_signal_enabled = 1;
95 SYSCTL_INT(_kern_smp, OID_AUTO, forward_signal_enabled, CTLFLAG_RW,
96 &forward_signal_enabled, 0,
97 "Forwarding of a signal to a process on a different CPU");
99 /* Enable forwarding of roundrobin to all other cpus */
100 static int forward_roundrobin_enabled = 1;
101 SYSCTL_INT(_kern_smp, OID_AUTO, forward_roundrobin_enabled, CTLFLAG_RW,
102 &forward_roundrobin_enabled, 0,
103 "Forwarding of roundrobin to all other CPUs");
105 /* Variables needed for SMP rendezvous. */
106 static void (*smp_rv_setup_func)(void *arg);
107 static void (*smp_rv_action_func)(void *arg);
108 static void (*smp_rv_teardown_func)(void *arg);
109 static void *smp_rv_func_arg;
110 static volatile int smp_rv_waiters[2];
113 * Shared mutex to restrict busywaits between smp_rendezvous() and
114 * smp(_targeted)_tlb_shootdown(). A deadlock occurs if both of these
115 * functions trigger at once and cause multiple CPUs to busywait with
116 * interrupts disabled.
118 struct mtx smp_ipi_mtx;
121 * Let the MD SMP code initialize mp_maxid very early if it can.
124 mp_setmaxid(void *dummy)
128 SYSINIT(cpu_mp_setmaxid, SI_SUB_TUNABLES, SI_ORDER_FIRST, mp_setmaxid, NULL)
131 * Call the MD SMP initialization code.
134 mp_start(void *dummy)
137 /* Probe for MP hardware. */
138 if (smp_disabled != 0 || cpu_mp_probe() == 0) {
140 all_cpus = PCPU_GET(cpumask);
144 mtx_init(&smp_ipi_mtx, "smp rendezvous", NULL, MTX_SPIN);
146 printf("FreeBSD/SMP: Multiprocessor System Detected: %d CPUs\n",
150 SYSINIT(cpu_mp, SI_SUB_CPU, SI_ORDER_SECOND, mp_start, NULL)
153 forward_signal(struct thread *td)
158 * signotify() has already set TDF_ASTPENDING and TDF_NEEDSIGCHECK on
159 * this thread, so all we need to do is poke it if it is currently
160 * executing so that it executes ast().
162 mtx_assert(&sched_lock, MA_OWNED);
163 KASSERT(TD_IS_RUNNING(td),
164 ("forward_signal: thread is not TDS_RUNNING"));
166 CTR1(KTR_SMP, "forward_signal(%p)", td->td_proc);
168 if (!smp_started || cold || panicstr)
170 if (!forward_signal_enabled)
173 /* No need to IPI ourself. */
180 ipi_selected(1 << id, IPI_AST);
184 forward_roundrobin(void)
188 cpumask_t id, map, me;
190 mtx_assert(&sched_lock, MA_OWNED);
192 CTR0(KTR_SMP, "forward_roundrobin()");
194 if (!smp_started || cold || panicstr)
196 if (!forward_roundrobin_enabled)
199 me = PCPU_GET(cpumask);
200 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
201 td = pc->pc_curthread;
203 if (id != me && (id & stopped_cpus) == 0 &&
204 td != pc->pc_idlethread) {
205 td->td_flags |= TDF_NEEDRESCHED;
209 ipi_selected(map, IPI_AST);
213 * When called the executing CPU will send an IPI to all other CPUs
214 * requesting that they halt execution.
216 * Usually (but not necessarily) called with 'other_cpus' as its arg.
218 * - Signals all CPUs in map to stop.
219 * - Waits for each to stop.
226 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs
227 * from executing at same time.
230 stop_cpus(cpumask_t map)
237 CTR1(KTR_SMP, "stop_cpus(%x)", map);
239 /* send the stop IPI to all CPUs in map */
240 ipi_selected(map, IPI_STOP);
243 while ((atomic_load_acq_int(&stopped_cpus) & map) != map) {
248 printf("timeout stopping cpus\n");
258 * Called by a CPU to restart stopped CPUs.
260 * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
262 * - Signals all CPUs in map to restart.
263 * - Waits for each to restart.
271 restart_cpus(cpumask_t map)
277 CTR1(KTR_SMP, "restart_cpus(%x)", map);
279 /* signal other cpus to restart */
280 atomic_store_rel_int(&started_cpus, map);
282 /* wait for each to clear its bit */
283 while ((atomic_load_acq_int(&stopped_cpus) & map) != 0)
290 * All-CPU rendezvous. CPUs are signalled, all execute the setup function
291 * (if specified), rendezvous, execute the action function (if specified),
292 * rendezvous again, execute the teardown function (if specified), and then
295 * Note that the supplied external functions _must_ be reentrant and aware
296 * that they are running in parallel and in an unknown lock context.
299 smp_rendezvous_action(void)
303 if (smp_rv_setup_func != NULL)
304 smp_rv_setup_func(smp_rv_func_arg);
305 /* spin on entry rendezvous */
306 atomic_add_int(&smp_rv_waiters[0], 1);
307 while (atomic_load_acq_int(&smp_rv_waiters[0]) < mp_ncpus)
309 /* action function */
310 if (smp_rv_action_func != NULL)
311 smp_rv_action_func(smp_rv_func_arg);
312 /* spin on exit rendezvous */
313 atomic_add_int(&smp_rv_waiters[1], 1);
314 while (atomic_load_acq_int(&smp_rv_waiters[1]) < mp_ncpus)
316 /* teardown function */
317 if (smp_rv_teardown_func != NULL)
318 smp_rv_teardown_func(smp_rv_func_arg);
322 smp_rendezvous(void (* setup_func)(void *),
323 void (* action_func)(void *),
324 void (* teardown_func)(void *),
329 if (setup_func != NULL)
331 if (action_func != NULL)
333 if (teardown_func != NULL)
338 /* obtain rendezvous lock */
339 mtx_lock_spin(&smp_ipi_mtx);
341 /* set static function pointers */
342 smp_rv_setup_func = setup_func;
343 smp_rv_action_func = action_func;
344 smp_rv_teardown_func = teardown_func;
345 smp_rv_func_arg = arg;
346 smp_rv_waiters[0] = 0;
347 smp_rv_waiters[1] = 0;
349 /* signal other processors, which will enter the IPI with interrupts off */
350 ipi_all_but_self(IPI_RENDEZVOUS);
352 /* call executor function */
353 smp_rendezvous_action();
356 mtx_unlock_spin(&smp_ipi_mtx);
361 * Provide dummy SMP support for UP kernels. Modules that need to use SMP
362 * APIs will still work using this dummy support.
365 mp_setvariables_for_up(void *dummy)
368 mp_maxid = PCPU_GET(cpuid);
369 all_cpus = PCPU_GET(cpumask);
370 KASSERT(PCPU_GET(cpuid) == 0, ("UP must have a CPU ID of zero"));
372 SYSINIT(cpu_mp_setvariables, SI_SUB_TUNABLES, SI_ORDER_FIRST,
373 mp_setvariables_for_up, NULL)
376 smp_rendezvous(void (* setup_func)(void *),
377 void (* action_func)(void *),
378 void (* teardown_func)(void *),
382 if (setup_func != NULL)
384 if (action_func != NULL)
386 if (teardown_func != NULL)