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>
47 #include <sys/sched.h>
49 #include <sys/sysctl.h>
51 #include <machine/cpu.h>
52 #include <machine/smp.h>
54 #include "opt_sched.h"
57 volatile cpuset_t stopped_cpus;
58 volatile cpuset_t started_cpus;
59 volatile cpuset_t suspended_cpus;
60 cpuset_t hlt_cpus_mask;
61 cpuset_t logical_cpus_mask;
63 void (*cpustop_restartfunc)(void);
65 /* This is used in modules that need to work in both SMP and UP. */
69 /* export this for libkvm consumers. */
70 int mp_maxcpus = MAXCPU;
72 volatile int smp_started;
75 static SYSCTL_NODE(_kern, OID_AUTO, smp, CTLFLAG_RD|CTLFLAG_CAPRD, NULL,
78 SYSCTL_INT(_kern_smp, OID_AUTO, maxid, CTLFLAG_RD|CTLFLAG_CAPRD, &mp_maxid, 0,
81 SYSCTL_INT(_kern_smp, OID_AUTO, maxcpus, CTLFLAG_RD|CTLFLAG_CAPRD, &mp_maxcpus,
82 0, "Max number of CPUs that the system was compiled for.");
84 int smp_active = 0; /* are the APs allowed to run? */
85 SYSCTL_INT(_kern_smp, OID_AUTO, active, CTLFLAG_RW, &smp_active, 0,
86 "Number of Auxillary Processors (APs) that were successfully started");
88 int smp_disabled = 0; /* has smp been disabled? */
89 SYSCTL_INT(_kern_smp, OID_AUTO, disabled, CTLFLAG_RDTUN|CTLFLAG_CAPRD,
90 &smp_disabled, 0, "SMP has been disabled from the loader");
91 TUNABLE_INT("kern.smp.disabled", &smp_disabled);
93 int smp_cpus = 1; /* how many cpu's running */
94 SYSCTL_INT(_kern_smp, OID_AUTO, cpus, CTLFLAG_RD|CTLFLAG_CAPRD, &smp_cpus, 0,
95 "Number of CPUs online");
97 int smp_topology = 0; /* Which topology we're using. */
98 SYSCTL_INT(_kern_smp, OID_AUTO, topology, CTLFLAG_RD, &smp_topology, 0,
99 "Topology override setting; 0 is default provided by hardware.");
100 TUNABLE_INT("kern.smp.topology", &smp_topology);
103 /* Enable forwarding of a signal to a process running on a different CPU */
104 static int forward_signal_enabled = 1;
105 SYSCTL_INT(_kern_smp, OID_AUTO, forward_signal_enabled, CTLFLAG_RW,
106 &forward_signal_enabled, 0,
107 "Forwarding of a signal to a process on a different CPU");
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[4];
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 mtx_init(&smp_ipi_mtx, "smp rendezvous", NULL, MTX_SPIN);
144 /* Probe for MP hardware. */
145 if (smp_disabled != 0 || cpu_mp_probe() == 0) {
147 CPU_SETOF(PCPU_GET(cpuid), &all_cpus);
152 printf("FreeBSD/SMP: Multiprocessor System Detected: %d CPUs\n",
156 SYSINIT(cpu_mp, SI_SUB_CPU, SI_ORDER_THIRD, mp_start, NULL);
159 forward_signal(struct thread *td)
164 * signotify() has already set TDF_ASTPENDING and TDF_NEEDSIGCHECK on
165 * this thread, so all we need to do is poke it if it is currently
166 * executing so that it executes ast().
168 THREAD_LOCK_ASSERT(td, MA_OWNED);
169 KASSERT(TD_IS_RUNNING(td),
170 ("forward_signal: thread is not TDS_RUNNING"));
172 CTR1(KTR_SMP, "forward_signal(%p)", td->td_proc);
174 if (!smp_started || cold || panicstr)
176 if (!forward_signal_enabled)
179 /* No need to IPI ourself. */
186 ipi_cpu(id, IPI_AST);
190 * When called the executing CPU will send an IPI to all other CPUs
191 * requesting that they halt execution.
193 * Usually (but not necessarily) called with 'other_cpus' as its arg.
195 * - Signals all CPUs in map to stop.
196 * - Waits for each to stop.
205 generic_stop_cpus(cpuset_t map, u_int type)
208 char cpusetbuf[CPUSETBUFSIZ];
210 static volatile u_int stopping_cpu = NOCPU;
212 volatile cpuset_t *cpus;
215 #if defined(__amd64__) || defined(__i386__)
216 type == IPI_STOP || type == IPI_STOP_HARD || type == IPI_SUSPEND,
218 type == IPI_STOP || type == IPI_STOP_HARD,
220 ("%s: invalid stop type", __func__));
225 CTR2(KTR_SMP, "stop_cpus(%s) with %u type",
226 cpusetobj_strprint(cpusetbuf, &map), type);
228 #if defined(__amd64__) || defined(__i386__)
230 * When suspending, ensure there are are no IPIs in progress.
231 * IPIs that have been issued, but not yet delivered (e.g.
232 * not pending on a vCPU when running under virtualization)
233 * will be lost, violating FreeBSD's assumption of reliable
236 if (type == IPI_SUSPEND)
237 mtx_lock_spin(&smp_ipi_mtx);
240 if (stopping_cpu != PCPU_GET(cpuid))
241 while (atomic_cmpset_int(&stopping_cpu, NOCPU,
242 PCPU_GET(cpuid)) == 0)
243 while (stopping_cpu != NOCPU)
244 cpu_spinwait(); /* spin */
246 /* send the stop IPI to all CPUs in map */
247 ipi_selected(map, type);
249 #if defined(__amd64__) || defined(__i386__)
250 if (type == IPI_SUSPEND)
251 cpus = &suspended_cpus;
254 cpus = &stopped_cpus;
257 while (!CPU_SUBSET(cpus, &map)) {
261 if (i == 100000000) {
262 printf("timeout stopping cpus\n");
267 #if defined(__amd64__) || defined(__i386__)
268 if (type == IPI_SUSPEND)
269 mtx_unlock_spin(&smp_ipi_mtx);
272 stopping_cpu = NOCPU;
277 stop_cpus(cpuset_t map)
280 return (generic_stop_cpus(map, IPI_STOP));
284 stop_cpus_hard(cpuset_t map)
287 return (generic_stop_cpus(map, IPI_STOP_HARD));
290 #if defined(__amd64__) || defined(__i386__)
292 suspend_cpus(cpuset_t map)
295 return (generic_stop_cpus(map, IPI_SUSPEND));
300 * Called by a CPU to restart stopped CPUs.
302 * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
304 * - Signals all CPUs in map to restart.
305 * - Waits for each to restart.
313 generic_restart_cpus(cpuset_t map, u_int type)
316 char cpusetbuf[CPUSETBUFSIZ];
318 volatile cpuset_t *cpus;
321 #if defined(__amd64__) || defined(__i386__)
322 type == IPI_STOP || type == IPI_STOP_HARD || type == IPI_SUSPEND,
324 type == IPI_STOP || type == IPI_STOP_HARD,
326 ("%s: invalid stop type", __func__));
331 CTR1(KTR_SMP, "restart_cpus(%s)", cpusetobj_strprint(cpusetbuf, &map));
333 #if defined(__amd64__) || defined(__i386__)
334 if (type == IPI_SUSPEND)
335 cpus = &suspended_cpus;
338 cpus = &stopped_cpus;
340 /* signal other cpus to restart */
341 CPU_COPY_STORE_REL(&map, &started_cpus);
343 /* wait for each to clear its bit */
344 while (CPU_OVERLAP(cpus, &map))
351 restart_cpus(cpuset_t map)
354 return (generic_restart_cpus(map, IPI_STOP));
357 #if defined(__amd64__) || defined(__i386__)
359 resume_cpus(cpuset_t map)
362 return (generic_restart_cpus(map, IPI_SUSPEND));
367 * All-CPU rendezvous. CPUs are signalled, all execute the setup function
368 * (if specified), rendezvous, execute the action function (if specified),
369 * rendezvous again, execute the teardown function (if specified), and then
372 * Note that the supplied external functions _must_ be reentrant and aware
373 * that they are running in parallel and in an unknown lock context.
376 smp_rendezvous_action(void)
379 void *local_func_arg;
380 void (*local_setup_func)(void*);
381 void (*local_action_func)(void*);
382 void (*local_teardown_func)(void*);
387 /* Ensure we have up-to-date values. */
388 atomic_add_acq_int(&smp_rv_waiters[0], 1);
389 while (smp_rv_waiters[0] < smp_rv_ncpus)
392 /* Fetch rendezvous parameters after acquire barrier. */
393 local_func_arg = smp_rv_func_arg;
394 local_setup_func = smp_rv_setup_func;
395 local_action_func = smp_rv_action_func;
396 local_teardown_func = smp_rv_teardown_func;
399 * Use a nested critical section to prevent any preemptions
400 * from occurring during a rendezvous action routine.
401 * Specifically, if a rendezvous handler is invoked via an IPI
402 * and the interrupted thread was in the critical_exit()
403 * function after setting td_critnest to 0 but before
404 * performing a deferred preemption, this routine can be
405 * invoked with td_critnest set to 0 and td_owepreempt true.
406 * In that case, a critical_exit() during the rendezvous
407 * action would trigger a preemption which is not permitted in
408 * a rendezvous action. To fix this, wrap all of the
409 * rendezvous action handlers in a critical section. We
410 * cannot use a regular critical section however as having
411 * critical_exit() preempt from this routine would also be
412 * problematic (the preemption must not occur before the IPI
413 * has been acknowledged via an EOI). Instead, we
414 * intentionally ignore td_owepreempt when leaving the
415 * critical section. This should be harmless because we do
416 * not permit rendezvous action routines to schedule threads,
417 * and thus td_owepreempt should never transition from 0 to 1
418 * during this routine.
423 owepreempt = td->td_owepreempt;
427 * If requested, run a setup function before the main action
428 * function. Ensure all CPUs have completed the setup
429 * function before moving on to the action function.
431 if (local_setup_func != smp_no_rendevous_barrier) {
432 if (smp_rv_setup_func != NULL)
433 smp_rv_setup_func(smp_rv_func_arg);
434 atomic_add_int(&smp_rv_waiters[1], 1);
435 while (smp_rv_waiters[1] < smp_rv_ncpus)
439 if (local_action_func != NULL)
440 local_action_func(local_func_arg);
442 if (local_teardown_func != smp_no_rendevous_barrier) {
444 * Signal that the main action has been completed. If a
445 * full exit rendezvous is requested, then all CPUs will
446 * wait here until all CPUs have finished the main action.
448 atomic_add_int(&smp_rv_waiters[2], 1);
449 while (smp_rv_waiters[2] < smp_rv_ncpus)
452 if (local_teardown_func != NULL)
453 local_teardown_func(local_func_arg);
457 * Signal that the rendezvous is fully completed by this CPU.
458 * This means that no member of smp_rv_* pseudo-structure will be
459 * accessed by this target CPU after this point; in particular,
460 * memory pointed by smp_rv_func_arg.
462 atomic_add_int(&smp_rv_waiters[3], 1);
465 KASSERT(owepreempt == td->td_owepreempt,
466 ("rendezvous action changed td_owepreempt"));
470 smp_rendezvous_cpus(cpuset_t map,
471 void (* setup_func)(void *),
472 void (* action_func)(void *),
473 void (* teardown_func)(void *),
476 int curcpumap, i, ncpus = 0;
478 /* Look comments in the !SMP case. */
481 if (setup_func != NULL)
483 if (action_func != NULL)
485 if (teardown_func != NULL)
492 if (CPU_ISSET(i, &map))
496 panic("ncpus is 0 with non-zero map");
498 mtx_lock_spin(&smp_ipi_mtx);
500 /* Pass rendezvous parameters via global variables. */
501 smp_rv_ncpus = ncpus;
502 smp_rv_setup_func = setup_func;
503 smp_rv_action_func = action_func;
504 smp_rv_teardown_func = teardown_func;
505 smp_rv_func_arg = arg;
506 smp_rv_waiters[1] = 0;
507 smp_rv_waiters[2] = 0;
508 smp_rv_waiters[3] = 0;
509 atomic_store_rel_int(&smp_rv_waiters[0], 0);
512 * Signal other processors, which will enter the IPI with
515 curcpumap = CPU_ISSET(curcpu, &map);
516 CPU_CLR(curcpu, &map);
517 ipi_selected(map, IPI_RENDEZVOUS);
519 /* Check if the current CPU is in the map */
521 smp_rendezvous_action();
524 * Ensure that the master CPU waits for all the other
525 * CPUs to finish the rendezvous, so that smp_rv_*
526 * pseudo-structure and the arg are guaranteed to not
529 while (atomic_load_acq_int(&smp_rv_waiters[3]) < ncpus)
532 mtx_unlock_spin(&smp_ipi_mtx);
536 smp_rendezvous(void (* setup_func)(void *),
537 void (* action_func)(void *),
538 void (* teardown_func)(void *),
541 smp_rendezvous_cpus(all_cpus, setup_func, action_func, teardown_func, arg);
544 static struct cpu_group group[MAXCPU];
549 char cpusetbuf[CPUSETBUFSIZ], cpusetbuf2[CPUSETBUFSIZ];
550 struct cpu_group *top;
553 * Check for a fake topology request for debugging purposes.
555 switch (smp_topology) {
557 /* Dual core with no sharing. */
558 top = smp_topo_1level(CG_SHARE_NONE, 2, 0);
561 /* No topology, all cpus are equal. */
562 top = smp_topo_none();
565 /* Dual core with shared L2. */
566 top = smp_topo_1level(CG_SHARE_L2, 2, 0);
569 /* quad core, shared l3 among each package, private l2. */
570 top = smp_topo_1level(CG_SHARE_L3, 4, 0);
573 /* quad core, 2 dualcore parts on each package share l2. */
574 top = smp_topo_2level(CG_SHARE_NONE, 2, CG_SHARE_L2, 2, 0);
577 /* Single-core 2xHTT */
578 top = smp_topo_1level(CG_SHARE_L1, 2, CG_FLAG_HTT);
581 /* quad core with a shared l3, 8 threads sharing L2. */
582 top = smp_topo_2level(CG_SHARE_L3, 4, CG_SHARE_L2, 8,
586 /* Default, ask the system what it wants. */
591 * Verify the returned topology.
593 if (top->cg_count != mp_ncpus)
594 panic("Built bad topology at %p. CPU count %d != %d",
595 top, top->cg_count, mp_ncpus);
596 if (CPU_CMP(&top->cg_mask, &all_cpus))
597 panic("Built bad topology at %p. CPU mask (%s) != (%s)",
598 top, cpusetobj_strprint(cpusetbuf, &top->cg_mask),
599 cpusetobj_strprint(cpusetbuf2, &all_cpus));
606 struct cpu_group *top;
609 top->cg_parent = NULL;
610 top->cg_child = NULL;
611 top->cg_mask = all_cpus;
612 top->cg_count = mp_ncpus;
613 top->cg_children = 0;
614 top->cg_level = CG_SHARE_NONE;
621 smp_topo_addleaf(struct cpu_group *parent, struct cpu_group *child, int share,
622 int count, int flags, int start)
624 char cpusetbuf[CPUSETBUFSIZ], cpusetbuf2[CPUSETBUFSIZ];
629 for (i = 0; i < count; i++, start++)
630 CPU_SET(start, &mask);
631 child->cg_parent = parent;
632 child->cg_child = NULL;
633 child->cg_children = 0;
634 child->cg_level = share;
635 child->cg_count = count;
636 child->cg_flags = flags;
637 child->cg_mask = mask;
638 parent->cg_children++;
639 for (; parent != NULL; parent = parent->cg_parent) {
640 if (CPU_OVERLAP(&parent->cg_mask, &child->cg_mask))
641 panic("Duplicate children in %p. mask (%s) child (%s)",
643 cpusetobj_strprint(cpusetbuf, &parent->cg_mask),
644 cpusetobj_strprint(cpusetbuf2, &child->cg_mask));
645 CPU_OR(&parent->cg_mask, &child->cg_mask);
646 parent->cg_count += child->cg_count;
653 smp_topo_1level(int share, int count, int flags)
655 struct cpu_group *child;
656 struct cpu_group *top;
663 packages = mp_ncpus / count;
664 top->cg_child = child = &group[1];
665 top->cg_level = CG_SHARE_NONE;
666 for (i = 0; i < packages; i++, child++)
667 cpu = smp_topo_addleaf(top, child, share, count, flags, cpu);
672 smp_topo_2level(int l2share, int l2count, int l1share, int l1count,
675 struct cpu_group *top;
676 struct cpu_group *l1g;
677 struct cpu_group *l2g;
686 top->cg_level = CG_SHARE_NONE;
687 top->cg_children = mp_ncpus / (l2count * l1count);
688 l1g = l2g + top->cg_children;
689 for (i = 0; i < top->cg_children; i++, l2g++) {
690 l2g->cg_parent = top;
692 l2g->cg_level = l2share;
693 for (j = 0; j < l2count; j++, l1g++)
694 cpu = smp_topo_addleaf(l2g, l1g, l1share, l1count,
702 smp_topo_find(struct cpu_group *top, int cpu)
704 struct cpu_group *cg;
709 CPU_SETOF(cpu, &mask);
712 if (!CPU_OVERLAP(&cg->cg_mask, &mask))
714 if (cg->cg_children == 0)
716 children = cg->cg_children;
717 for (i = 0, cg = cg->cg_child; i < children; cg++, i++)
718 if (CPU_OVERLAP(&cg->cg_mask, &mask))
726 smp_rendezvous_cpus(cpuset_t map,
727 void (*setup_func)(void *),
728 void (*action_func)(void *),
729 void (*teardown_func)(void *),
733 * In the !SMP case we just need to ensure the same initial conditions
737 if (setup_func != NULL)
739 if (action_func != NULL)
741 if (teardown_func != NULL)
747 smp_rendezvous(void (*setup_func)(void *),
748 void (*action_func)(void *),
749 void (*teardown_func)(void *),
753 /* Look comments in the smp_rendezvous_cpus() case. */
755 if (setup_func != NULL)
757 if (action_func != NULL)
759 if (teardown_func != NULL)
765 * Provide dummy SMP support for UP kernels. Modules that need to use SMP
766 * APIs will still work using this dummy support.
769 mp_setvariables_for_up(void *dummy)
772 mp_maxid = PCPU_GET(cpuid);
773 CPU_SETOF(mp_maxid, &all_cpus);
774 KASSERT(PCPU_GET(cpuid) == 0, ("UP must have a CPU ID of zero"));
776 SYSINIT(cpu_mp_setvariables, SI_SUB_TUNABLES, SI_ORDER_FIRST,
777 mp_setvariables_for_up, NULL);
781 smp_no_rendevous_barrier(void *dummy)
784 KASSERT((!smp_started),("smp_no_rendevous called and smp is started"));
789 * Wait specified idle threads to switch once. This ensures that even
790 * preempted threads have cycled through the switch function once,
791 * exiting their codepaths. This allows us to change global pointers
792 * with no other synchronization.
795 quiesce_cpus(cpuset_t map, const char *wmesg, int prio)
803 for (cpu = 0; cpu <= mp_maxid; cpu++) {
804 if (!CPU_ISSET(cpu, &map) || CPU_ABSENT(cpu))
806 pcpu = pcpu_find(cpu);
807 gen[cpu] = pcpu->pc_idlethread->td_generation;
809 for (cpu = 0; cpu <= mp_maxid; cpu++) {
810 if (!CPU_ISSET(cpu, &map) || CPU_ABSENT(cpu))
812 pcpu = pcpu_find(cpu);
813 thread_lock(curthread);
814 sched_bind(curthread, cpu);
815 thread_unlock(curthread);
816 while (gen[cpu] == pcpu->pc_idlethread->td_generation) {
817 error = tsleep(quiesce_cpus, prio, wmesg, 1);
818 if (error != EWOULDBLOCK)
824 thread_lock(curthread);
825 sched_unbind(curthread);
826 thread_unlock(curthread);
832 quiesce_all_cpus(const char *wmesg, int prio)
835 return quiesce_cpus(all_cpus, wmesg, prio);