xref: /freebsd/sys/kern/subr_smp.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*-
2  * Copyright (c) 2001, John Baldwin <jhb@FreeBSD.org>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * This module holds the global variables and machine independent functions
32  * used for the kernel SMP support.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/ktr.h>
42 #include <sys/proc.h>
43 #include <sys/bus.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/pcpu.h>
47 #include <sys/smp.h>
48 #include <sys/sysctl.h>
49 
50 #include <machine/cpu.h>
51 #include <machine/smp.h>
52 
53 #include "opt_sched.h"
54 
55 #ifdef SMP
56 volatile cpumask_t stopped_cpus;
57 volatile cpumask_t started_cpus;
58 cpumask_t hlt_cpus_mask;
59 cpumask_t logical_cpus_mask;
60 
61 void (*cpustop_restartfunc)(void);
62 #endif
63 /* This is used in modules that need to work in both SMP and UP. */
64 cpumask_t all_cpus;
65 
66 int mp_ncpus;
67 /* export this for libkvm consumers. */
68 int mp_maxcpus = MAXCPU;
69 
70 volatile int smp_started;
71 u_int mp_maxid;
72 
73 SYSCTL_NODE(_kern, OID_AUTO, smp, CTLFLAG_RD, NULL, "Kernel SMP");
74 
75 SYSCTL_UINT(_kern_smp, OID_AUTO, maxid, CTLFLAG_RD, &mp_maxid, 0,
76     "Max CPU ID.");
77 
78 SYSCTL_INT(_kern_smp, OID_AUTO, maxcpus, CTLFLAG_RD, &mp_maxcpus, 0,
79     "Max number of CPUs that the system was compiled for.");
80 
81 int smp_active = 0;	/* are the APs allowed to run? */
82 SYSCTL_INT(_kern_smp, OID_AUTO, active, CTLFLAG_RW, &smp_active, 0,
83     "Number of Auxillary Processors (APs) that were successfully started");
84 
85 int smp_disabled = 0;	/* has smp been disabled? */
86 SYSCTL_INT(_kern_smp, OID_AUTO, disabled, CTLFLAG_RDTUN, &smp_disabled, 0,
87     "SMP has been disabled from the loader");
88 TUNABLE_INT("kern.smp.disabled", &smp_disabled);
89 
90 int smp_cpus = 1;	/* how many cpu's running */
91 SYSCTL_INT(_kern_smp, OID_AUTO, cpus, CTLFLAG_RD, &smp_cpus, 0,
92     "Number of CPUs online");
93 
94 int smp_topology = 0;	/* Which topology we're using. */
95 SYSCTL_INT(_kern_smp, OID_AUTO, topology, CTLFLAG_RD, &smp_topology, 0,
96     "Topology override setting; 0 is default provided by hardware.");
97 TUNABLE_INT("kern.smp.topology", &smp_topology);
98 
99 #ifdef SMP
100 /* Enable forwarding of a signal to a process running on a different CPU */
101 static int forward_signal_enabled = 1;
102 SYSCTL_INT(_kern_smp, OID_AUTO, forward_signal_enabled, CTLFLAG_RW,
103 	   &forward_signal_enabled, 0,
104 	   "Forwarding of a signal to a process on a different CPU");
105 
106 /* Variables needed for SMP rendezvous. */
107 static volatile int smp_rv_ncpus;
108 static void (*volatile smp_rv_setup_func)(void *arg);
109 static void (*volatile smp_rv_action_func)(void *arg);
110 static void (*volatile smp_rv_teardown_func)(void *arg);
111 static void *volatile smp_rv_func_arg;
112 static volatile int smp_rv_waiters[3];
113 static volatile int smp_rv_generation;
114 
115 /*
116  * Shared mutex to restrict busywaits between smp_rendezvous() and
117  * smp(_targeted)_tlb_shootdown().  A deadlock occurs if both of these
118  * functions trigger at once and cause multiple CPUs to busywait with
119  * interrupts disabled.
120  */
121 struct mtx smp_ipi_mtx;
122 
123 /*
124  * Let the MD SMP code initialize mp_maxid very early if it can.
125  */
126 static void
127 mp_setmaxid(void *dummy)
128 {
129 	cpu_mp_setmaxid();
130 }
131 SYSINIT(cpu_mp_setmaxid, SI_SUB_TUNABLES, SI_ORDER_FIRST, mp_setmaxid, NULL);
132 
133 /*
134  * Call the MD SMP initialization code.
135  */
136 static void
137 mp_start(void *dummy)
138 {
139 
140 	mtx_init(&smp_ipi_mtx, "smp rendezvous", NULL, MTX_SPIN);
141 
142 	/* Probe for MP hardware. */
143 	if (smp_disabled != 0 || cpu_mp_probe() == 0) {
144 		mp_ncpus = 1;
145 		all_cpus = PCPU_GET(cpumask);
146 		return;
147 	}
148 
149 	cpu_mp_start();
150 	printf("FreeBSD/SMP: Multiprocessor System Detected: %d CPUs\n",
151 	    mp_ncpus);
152 	cpu_mp_announce();
153 }
154 SYSINIT(cpu_mp, SI_SUB_CPU, SI_ORDER_THIRD, mp_start, NULL);
155 
156 void
157 forward_signal(struct thread *td)
158 {
159 	int id;
160 
161 	/*
162 	 * signotify() has already set TDF_ASTPENDING and TDF_NEEDSIGCHECK on
163 	 * this thread, so all we need to do is poke it if it is currently
164 	 * executing so that it executes ast().
165 	 */
166 	THREAD_LOCK_ASSERT(td, MA_OWNED);
167 	KASSERT(TD_IS_RUNNING(td),
168 	    ("forward_signal: thread is not TDS_RUNNING"));
169 
170 	CTR1(KTR_SMP, "forward_signal(%p)", td->td_proc);
171 
172 	if (!smp_started || cold || panicstr)
173 		return;
174 	if (!forward_signal_enabled)
175 		return;
176 
177 	/* No need to IPI ourself. */
178 	if (td == curthread)
179 		return;
180 
181 	id = td->td_oncpu;
182 	if (id == NOCPU)
183 		return;
184 	ipi_cpu(id, IPI_AST);
185 }
186 
187 /*
188  * When called the executing CPU will send an IPI to all other CPUs
189  *  requesting that they halt execution.
190  *
191  * Usually (but not necessarily) called with 'other_cpus' as its arg.
192  *
193  *  - Signals all CPUs in map to stop.
194  *  - Waits for each to stop.
195  *
196  * Returns:
197  *  -1: error
198  *   0: NA
199  *   1: ok
200  *
201  */
202 static int
203 generic_stop_cpus(cpumask_t map, u_int type)
204 {
205 	static volatile u_int stopping_cpu = NOCPU;
206 	int i;
207 
208 	KASSERT(
209 #if defined(__amd64__)
210 	    type == IPI_STOP || type == IPI_STOP_HARD || type == IPI_SUSPEND,
211 #else
212 	    type == IPI_STOP || type == IPI_STOP_HARD,
213 #endif
214 	    ("%s: invalid stop type", __func__));
215 
216 	if (!smp_started)
217 		return (0);
218 
219 	CTR2(KTR_SMP, "stop_cpus(%x) with %u type", map, type);
220 
221 	if (stopping_cpu != PCPU_GET(cpuid))
222 		while (atomic_cmpset_int(&stopping_cpu, NOCPU,
223 		    PCPU_GET(cpuid)) == 0)
224 			while (stopping_cpu != NOCPU)
225 				cpu_spinwait(); /* spin */
226 
227 	/* send the stop IPI to all CPUs in map */
228 	ipi_selected(map, type);
229 
230 	i = 0;
231 	while ((stopped_cpus & map) != map) {
232 		/* spin */
233 		cpu_spinwait();
234 		i++;
235 #ifdef DIAGNOSTIC
236 		if (i == 100000) {
237 			printf("timeout stopping cpus\n");
238 			break;
239 		}
240 #endif
241 	}
242 
243 	stopping_cpu = NOCPU;
244 	return (1);
245 }
246 
247 int
248 stop_cpus(cpumask_t map)
249 {
250 
251 	return (generic_stop_cpus(map, IPI_STOP));
252 }
253 
254 int
255 stop_cpus_hard(cpumask_t map)
256 {
257 
258 	return (generic_stop_cpus(map, IPI_STOP_HARD));
259 }
260 
261 #if defined(__amd64__)
262 int
263 suspend_cpus(cpumask_t map)
264 {
265 
266 	return (generic_stop_cpus(map, IPI_SUSPEND));
267 }
268 #endif
269 
270 /*
271  * Called by a CPU to restart stopped CPUs.
272  *
273  * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
274  *
275  *  - Signals all CPUs in map to restart.
276  *  - Waits for each to restart.
277  *
278  * Returns:
279  *  -1: error
280  *   0: NA
281  *   1: ok
282  */
283 int
284 restart_cpus(cpumask_t map)
285 {
286 
287 	if (!smp_started)
288 		return 0;
289 
290 	CTR1(KTR_SMP, "restart_cpus(%x)", map);
291 
292 	/* signal other cpus to restart */
293 	atomic_store_rel_int(&started_cpus, map);
294 
295 	/* wait for each to clear its bit */
296 	while ((stopped_cpus & map) != 0)
297 		cpu_spinwait();
298 
299 	return 1;
300 }
301 
302 /*
303  * All-CPU rendezvous.  CPUs are signalled, all execute the setup function
304  * (if specified), rendezvous, execute the action function (if specified),
305  * rendezvous again, execute the teardown function (if specified), and then
306  * resume.
307  *
308  * Note that the supplied external functions _must_ be reentrant and aware
309  * that they are running in parallel and in an unknown lock context.
310  */
311 void
312 smp_rendezvous_action(void)
313 {
314 	struct thread *td;
315 	void *local_func_arg;
316 	void (*local_setup_func)(void*);
317 	void (*local_action_func)(void*);
318 	void (*local_teardown_func)(void*);
319 	int generation;
320 #ifdef INVARIANTS
321 	int owepreempt;
322 #endif
323 
324 	/* Ensure we have up-to-date values. */
325 	atomic_add_acq_int(&smp_rv_waiters[0], 1);
326 	while (smp_rv_waiters[0] < smp_rv_ncpus)
327 		cpu_spinwait();
328 
329 	/* Fetch rendezvous parameters after acquire barrier. */
330 	local_func_arg = smp_rv_func_arg;
331 	local_setup_func = smp_rv_setup_func;
332 	local_action_func = smp_rv_action_func;
333 	local_teardown_func = smp_rv_teardown_func;
334 	generation = smp_rv_generation;
335 
336 	/*
337 	 * Use a nested critical section to prevent any preemptions
338 	 * from occurring during a rendezvous action routine.
339 	 * Specifically, if a rendezvous handler is invoked via an IPI
340 	 * and the interrupted thread was in the critical_exit()
341 	 * function after setting td_critnest to 0 but before
342 	 * performing a deferred preemption, this routine can be
343 	 * invoked with td_critnest set to 0 and td_owepreempt true.
344 	 * In that case, a critical_exit() during the rendezvous
345 	 * action would trigger a preemption which is not permitted in
346 	 * a rendezvous action.  To fix this, wrap all of the
347 	 * rendezvous action handlers in a critical section.  We
348 	 * cannot use a regular critical section however as having
349 	 * critical_exit() preempt from this routine would also be
350 	 * problematic (the preemption must not occur before the IPI
351 	 * has been acknowledged via an EOI).  Instead, we
352 	 * intentionally ignore td_owepreempt when leaving the
353 	 * critical section.  This should be harmless because we do
354 	 * not permit rendezvous action routines to schedule threads,
355 	 * and thus td_owepreempt should never transition from 0 to 1
356 	 * during this routine.
357 	 */
358 	td = curthread;
359 	td->td_critnest++;
360 #ifdef INVARIANTS
361 	owepreempt = td->td_owepreempt;
362 #endif
363 
364 	/*
365 	 * If requested, run a setup function before the main action
366 	 * function.  Ensure all CPUs have completed the setup
367 	 * function before moving on to the action function.
368 	 */
369 	if (local_setup_func != smp_no_rendevous_barrier) {
370 		if (smp_rv_setup_func != NULL)
371 			smp_rv_setup_func(smp_rv_func_arg);
372 		atomic_add_int(&smp_rv_waiters[1], 1);
373 		while (smp_rv_waiters[1] < smp_rv_ncpus)
374                 	cpu_spinwait();
375 	}
376 
377 	if (local_action_func != NULL)
378 		local_action_func(local_func_arg);
379 
380 	/*
381 	 * Signal that the main action has been completed.  If a
382 	 * full exit rendezvous is requested, then all CPUs will
383 	 * wait here until all CPUs have finished the main action.
384 	 *
385 	 * Note that the write by the last CPU to finish the action
386 	 * may become visible to different CPUs at different times.
387 	 * As a result, the CPU that initiated the rendezvous may
388 	 * exit the rendezvous and drop the lock allowing another
389 	 * rendezvous to be initiated on the same CPU or a different
390 	 * CPU.  In that case the exit sentinel may be cleared before
391 	 * all CPUs have noticed causing those CPUs to hang forever.
392 	 * Workaround this by using a generation count to notice when
393 	 * this race occurs and to exit the rendezvous in that case.
394 	 */
395 	MPASS(generation == smp_rv_generation);
396 	atomic_add_int(&smp_rv_waiters[2], 1);
397 	if (local_teardown_func != smp_no_rendevous_barrier) {
398 		while (smp_rv_waiters[2] < smp_rv_ncpus &&
399 		    generation == smp_rv_generation)
400 			cpu_spinwait();
401 
402 		if (local_teardown_func != NULL)
403 			local_teardown_func(local_func_arg);
404 	}
405 
406 	td->td_critnest--;
407 	KASSERT(owepreempt == td->td_owepreempt,
408 	    ("rendezvous action changed td_owepreempt"));
409 }
410 
411 void
412 smp_rendezvous_cpus(cpumask_t map,
413 	void (* setup_func)(void *),
414 	void (* action_func)(void *),
415 	void (* teardown_func)(void *),
416 	void *arg)
417 {
418 	int i, ncpus = 0;
419 
420 	if (!smp_started) {
421 		if (setup_func != NULL)
422 			setup_func(arg);
423 		if (action_func != NULL)
424 			action_func(arg);
425 		if (teardown_func != NULL)
426 			teardown_func(arg);
427 		return;
428 	}
429 
430 	CPU_FOREACH(i) {
431 		if (((1 << i) & map) != 0)
432 			ncpus++;
433 	}
434 	if (ncpus == 0)
435 		panic("ncpus is 0 with map=0x%x", map);
436 
437 	mtx_lock_spin(&smp_ipi_mtx);
438 
439 	atomic_add_acq_int(&smp_rv_generation, 1);
440 
441 	/* Pass rendezvous parameters via global variables. */
442 	smp_rv_ncpus = ncpus;
443 	smp_rv_setup_func = setup_func;
444 	smp_rv_action_func = action_func;
445 	smp_rv_teardown_func = teardown_func;
446 	smp_rv_func_arg = arg;
447 	smp_rv_waiters[1] = 0;
448 	smp_rv_waiters[2] = 0;
449 	atomic_store_rel_int(&smp_rv_waiters[0], 0);
450 
451 	/*
452 	 * Signal other processors, which will enter the IPI with
453 	 * interrupts off.
454 	 */
455 	ipi_selected(map & ~(1 << curcpu), IPI_RENDEZVOUS);
456 
457 	/* Check if the current CPU is in the map */
458 	if ((map & (1 << curcpu)) != 0)
459 		smp_rendezvous_action();
460 
461 	/*
462 	 * If the caller did not request an exit barrier to be enforced
463 	 * on each CPU, ensure that this CPU waits for all the other
464 	 * CPUs to finish the rendezvous.
465 	 */
466 	if (teardown_func == smp_no_rendevous_barrier)
467 		while (atomic_load_acq_int(&smp_rv_waiters[2]) < ncpus)
468 			cpu_spinwait();
469 
470 	mtx_unlock_spin(&smp_ipi_mtx);
471 }
472 
473 void
474 smp_rendezvous(void (* setup_func)(void *),
475 	       void (* action_func)(void *),
476 	       void (* teardown_func)(void *),
477 	       void *arg)
478 {
479 	smp_rendezvous_cpus(all_cpus, setup_func, action_func, teardown_func, arg);
480 }
481 
482 static struct cpu_group group[MAXCPU];
483 
484 struct cpu_group *
485 smp_topo(void)
486 {
487 	struct cpu_group *top;
488 
489 	/*
490 	 * Check for a fake topology request for debugging purposes.
491 	 */
492 	switch (smp_topology) {
493 	case 1:
494 		/* Dual core with no sharing.  */
495 		top = smp_topo_1level(CG_SHARE_NONE, 2, 0);
496 		break;
497 	case 2:
498 		/* No topology, all cpus are equal. */
499 		top = smp_topo_none();
500 		break;
501 	case 3:
502 		/* Dual core with shared L2.  */
503 		top = smp_topo_1level(CG_SHARE_L2, 2, 0);
504 		break;
505 	case 4:
506 		/* quad core, shared l3 among each package, private l2.  */
507 		top = smp_topo_1level(CG_SHARE_L3, 4, 0);
508 		break;
509 	case 5:
510 		/* quad core,  2 dualcore parts on each package share l2.  */
511 		top = smp_topo_2level(CG_SHARE_NONE, 2, CG_SHARE_L2, 2, 0);
512 		break;
513 	case 6:
514 		/* Single-core 2xHTT */
515 		top = smp_topo_1level(CG_SHARE_L1, 2, CG_FLAG_HTT);
516 		break;
517 	case 7:
518 		/* quad core with a shared l3, 8 threads sharing L2.  */
519 		top = smp_topo_2level(CG_SHARE_L3, 4, CG_SHARE_L2, 8,
520 		    CG_FLAG_SMT);
521 		break;
522 	default:
523 		/* Default, ask the system what it wants. */
524 		top = cpu_topo();
525 		break;
526 	}
527 	/*
528 	 * Verify the returned topology.
529 	 */
530 	if (top->cg_count != mp_ncpus)
531 		panic("Built bad topology at %p.  CPU count %d != %d",
532 		    top, top->cg_count, mp_ncpus);
533 	if (top->cg_mask != all_cpus)
534 		panic("Built bad topology at %p.  CPU mask 0x%X != 0x%X",
535 		    top, top->cg_mask, all_cpus);
536 	return (top);
537 }
538 
539 struct cpu_group *
540 smp_topo_none(void)
541 {
542 	struct cpu_group *top;
543 
544 	top = &group[0];
545 	top->cg_parent = NULL;
546 	top->cg_child = NULL;
547 	top->cg_mask = all_cpus;
548 	top->cg_count = mp_ncpus;
549 	top->cg_children = 0;
550 	top->cg_level = CG_SHARE_NONE;
551 	top->cg_flags = 0;
552 
553 	return (top);
554 }
555 
556 static int
557 smp_topo_addleaf(struct cpu_group *parent, struct cpu_group *child, int share,
558     int count, int flags, int start)
559 {
560 	cpumask_t mask;
561 	int i;
562 
563 	for (mask = 0, i = 0; i < count; i++, start++)
564 		mask |= (1 << start);
565 	child->cg_parent = parent;
566 	child->cg_child = NULL;
567 	child->cg_children = 0;
568 	child->cg_level = share;
569 	child->cg_count = count;
570 	child->cg_flags = flags;
571 	child->cg_mask = mask;
572 	parent->cg_children++;
573 	for (; parent != NULL; parent = parent->cg_parent) {
574 		if ((parent->cg_mask & child->cg_mask) != 0)
575 			panic("Duplicate children in %p.  mask 0x%X child 0x%X",
576 			    parent, parent->cg_mask, child->cg_mask);
577 		parent->cg_mask |= child->cg_mask;
578 		parent->cg_count += child->cg_count;
579 	}
580 
581 	return (start);
582 }
583 
584 struct cpu_group *
585 smp_topo_1level(int share, int count, int flags)
586 {
587 	struct cpu_group *child;
588 	struct cpu_group *top;
589 	int packages;
590 	int cpu;
591 	int i;
592 
593 	cpu = 0;
594 	top = &group[0];
595 	packages = mp_ncpus / count;
596 	top->cg_child = child = &group[1];
597 	top->cg_level = CG_SHARE_NONE;
598 	for (i = 0; i < packages; i++, child++)
599 		cpu = smp_topo_addleaf(top, child, share, count, flags, cpu);
600 	return (top);
601 }
602 
603 struct cpu_group *
604 smp_topo_2level(int l2share, int l2count, int l1share, int l1count,
605     int l1flags)
606 {
607 	struct cpu_group *top;
608 	struct cpu_group *l1g;
609 	struct cpu_group *l2g;
610 	int cpu;
611 	int i;
612 	int j;
613 
614 	cpu = 0;
615 	top = &group[0];
616 	l2g = &group[1];
617 	top->cg_child = l2g;
618 	top->cg_level = CG_SHARE_NONE;
619 	top->cg_children = mp_ncpus / (l2count * l1count);
620 	l1g = l2g + top->cg_children;
621 	for (i = 0; i < top->cg_children; i++, l2g++) {
622 		l2g->cg_parent = top;
623 		l2g->cg_child = l1g;
624 		l2g->cg_level = l2share;
625 		for (j = 0; j < l2count; j++, l1g++)
626 			cpu = smp_topo_addleaf(l2g, l1g, l1share, l1count,
627 			    l1flags, cpu);
628 	}
629 	return (top);
630 }
631 
632 
633 struct cpu_group *
634 smp_topo_find(struct cpu_group *top, int cpu)
635 {
636 	struct cpu_group *cg;
637 	cpumask_t mask;
638 	int children;
639 	int i;
640 
641 	mask = (1 << cpu);
642 	cg = top;
643 	for (;;) {
644 		if ((cg->cg_mask & mask) == 0)
645 			return (NULL);
646 		if (cg->cg_children == 0)
647 			return (cg);
648 		children = cg->cg_children;
649 		for (i = 0, cg = cg->cg_child; i < children; cg++, i++)
650 			if ((cg->cg_mask & mask) != 0)
651 				break;
652 	}
653 	return (NULL);
654 }
655 #else /* !SMP */
656 
657 void
658 smp_rendezvous_cpus(cpumask_t map,
659 	void (*setup_func)(void *),
660 	void (*action_func)(void *),
661 	void (*teardown_func)(void *),
662 	void *arg)
663 {
664 	if (setup_func != NULL)
665 		setup_func(arg);
666 	if (action_func != NULL)
667 		action_func(arg);
668 	if (teardown_func != NULL)
669 		teardown_func(arg);
670 }
671 
672 void
673 smp_rendezvous(void (*setup_func)(void *),
674 	       void (*action_func)(void *),
675 	       void (*teardown_func)(void *),
676 	       void *arg)
677 {
678 
679 	if (setup_func != NULL)
680 		setup_func(arg);
681 	if (action_func != NULL)
682 		action_func(arg);
683 	if (teardown_func != NULL)
684 		teardown_func(arg);
685 }
686 
687 /*
688  * Provide dummy SMP support for UP kernels.  Modules that need to use SMP
689  * APIs will still work using this dummy support.
690  */
691 static void
692 mp_setvariables_for_up(void *dummy)
693 {
694 	mp_ncpus = 1;
695 	mp_maxid = PCPU_GET(cpuid);
696 	all_cpus = PCPU_GET(cpumask);
697 	KASSERT(PCPU_GET(cpuid) == 0, ("UP must have a CPU ID of zero"));
698 }
699 SYSINIT(cpu_mp_setvariables, SI_SUB_TUNABLES, SI_ORDER_FIRST,
700     mp_setvariables_for_up, NULL);
701 #endif /* SMP */
702 
703 void
704 smp_no_rendevous_barrier(void *dummy)
705 {
706 #ifdef SMP
707 	KASSERT((!smp_started),("smp_no_rendevous called and smp is started"));
708 #endif
709 }
710