1 /* 2 * Copyright (c) 2001 3 * John Baldwin <jhb@FreeBSD.org>. 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 * 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. 16 * 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. 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/lock.h> 44 #include <sys/mutex.h> 45 #include <sys/pcpu.h> 46 #include <sys/smp.h> 47 #include <sys/sysctl.h> 48 49 #include <machine/smp.h> 50 51 #ifdef SMP 52 volatile u_int stopped_cpus; 53 volatile u_int started_cpus; 54 55 void (*cpustop_restartfunc)(void); 56 #endif 57 58 int mp_ncpus; 59 /* export this for libkvm consumers. */ 60 int mp_maxcpus = MAXCPU; 61 62 volatile int smp_started; 63 u_int all_cpus; 64 u_int mp_maxid; 65 66 SYSCTL_NODE(_kern, OID_AUTO, smp, CTLFLAG_RD, NULL, "Kernel SMP"); 67 68 SYSCTL_INT(_kern_smp, OID_AUTO, maxcpus, CTLFLAG_RD, &mp_maxcpus, 0, 69 "Max number of CPUs that the system was compiled for."); 70 71 int smp_active = 0; /* are the APs allowed to run? */ 72 SYSCTL_INT(_kern_smp, OID_AUTO, active, CTLFLAG_RW, &smp_active, 0, 73 "Number of Auxillary Processors (APs) that were successfully started"); 74 75 int smp_disabled = 0; /* has smp been disabled? */ 76 SYSCTL_INT(_kern_smp, OID_AUTO, disabled, CTLFLAG_RDTUN, &smp_disabled, 0, 77 "SMP has been disabled from the loader"); 78 TUNABLE_INT("kern.smp.disabled", &smp_disabled); 79 80 int smp_cpus = 1; /* how many cpu's running */ 81 SYSCTL_INT(_kern_smp, OID_AUTO, cpus, CTLFLAG_RD, &smp_cpus, 0, 82 "Number of CPUs online"); 83 84 #ifdef SMP 85 /* Enable forwarding of a signal to a process running on a different CPU */ 86 static int forward_signal_enabled = 1; 87 SYSCTL_INT(_kern_smp, OID_AUTO, forward_signal_enabled, CTLFLAG_RW, 88 &forward_signal_enabled, 0, 89 "Forwarding of a signal to a process on a different CPU"); 90 91 /* Enable forwarding of roundrobin to all other cpus */ 92 static int forward_roundrobin_enabled = 1; 93 SYSCTL_INT(_kern_smp, OID_AUTO, forward_roundrobin_enabled, CTLFLAG_RW, 94 &forward_roundrobin_enabled, 0, 95 "Forwarding of roundrobin to all other CPUs"); 96 97 /* Variables needed for SMP rendezvous. */ 98 static void (*smp_rv_setup_func)(void *arg); 99 static void (*smp_rv_action_func)(void *arg); 100 static void (*smp_rv_teardown_func)(void *arg); 101 static void *smp_rv_func_arg; 102 static volatile int smp_rv_waiters[2]; 103 static struct mtx smp_rv_mtx; 104 105 /* 106 * Let the MD SMP code initialize mp_maxid very early if it can. 107 */ 108 static void 109 mp_setmaxid(void *dummy) 110 { 111 cpu_mp_setmaxid(); 112 } 113 SYSINIT(cpu_mp_setmaxid, SI_SUB_TUNABLES, SI_ORDER_FIRST, mp_setmaxid, NULL) 114 115 /* 116 * Call the MD SMP initialization code. 117 */ 118 static void 119 mp_start(void *dummy) 120 { 121 122 /* Probe for MP hardware. */ 123 if (smp_disabled != 0 || cpu_mp_probe() == 0) { 124 mp_ncpus = 1; 125 all_cpus = PCPU_GET(cpumask); 126 return; 127 } 128 129 mtx_init(&smp_rv_mtx, "smp rendezvous", NULL, MTX_SPIN); 130 cpu_mp_start(); 131 printf("FreeBSD/SMP: Multiprocessor System Detected: %d CPUs\n", 132 mp_ncpus); 133 cpu_mp_announce(); 134 } 135 SYSINIT(cpu_mp, SI_SUB_CPU, SI_ORDER_SECOND, mp_start, NULL) 136 137 void 138 forward_signal(struct thread *td) 139 { 140 int id; 141 142 /* 143 * signotify() has already set TDF_ASTPENDING and TDF_NEEDSIGCHECK on 144 * this thread, so all we need to do is poke it if it is currently 145 * executing so that it executes ast(). 146 */ 147 mtx_assert(&sched_lock, MA_OWNED); 148 KASSERT(TD_IS_RUNNING(td), 149 ("forward_signal: thread is not TDS_RUNNING")); 150 151 CTR1(KTR_SMP, "forward_signal(%p)", td->td_proc); 152 153 if (!smp_started || cold || panicstr) 154 return; 155 if (!forward_signal_enabled) 156 return; 157 158 /* No need to IPI ourself. */ 159 if (td == curthread) 160 return; 161 162 id = td->td_oncpu; 163 if (id == NOCPU) 164 return; 165 ipi_selected(1 << id, IPI_AST); 166 } 167 168 void 169 forward_roundrobin(void) 170 { 171 struct pcpu *pc; 172 struct thread *td; 173 u_int id, map; 174 175 mtx_assert(&sched_lock, MA_OWNED); 176 177 CTR0(KTR_SMP, "forward_roundrobin()"); 178 179 if (!smp_started || cold || panicstr) 180 return; 181 if (!forward_roundrobin_enabled) 182 return; 183 map = 0; 184 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) { 185 td = pc->pc_curthread; 186 id = pc->pc_cpumask; 187 if (id != PCPU_GET(cpumask) && (id & stopped_cpus) == 0 && 188 td != pc->pc_idlethread) { 189 td->td_flags |= TDF_NEEDRESCHED; 190 map |= id; 191 } 192 } 193 ipi_selected(map, IPI_AST); 194 } 195 196 /* 197 * When called the executing CPU will send an IPI to all other CPUs 198 * requesting that they halt execution. 199 * 200 * Usually (but not necessarily) called with 'other_cpus' as its arg. 201 * 202 * - Signals all CPUs in map to stop. 203 * - Waits for each to stop. 204 * 205 * Returns: 206 * -1: error 207 * 0: NA 208 * 1: ok 209 * 210 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs 211 * from executing at same time. 212 */ 213 int 214 stop_cpus(u_int map) 215 { 216 int i; 217 218 if (!smp_started) 219 return 0; 220 221 CTR1(KTR_SMP, "stop_cpus(%x)", map); 222 223 /* send the stop IPI to all CPUs in map */ 224 ipi_selected(map, IPI_STOP); 225 226 i = 0; 227 while ((atomic_load_acq_int(&stopped_cpus) & map) != map) { 228 /* spin */ 229 i++; 230 #ifdef DIAGNOSTIC 231 if (i == 100000) { 232 printf("timeout stopping cpus\n"); 233 break; 234 } 235 #endif 236 } 237 238 return 1; 239 } 240 241 242 /* 243 * Called by a CPU to restart stopped CPUs. 244 * 245 * Usually (but not necessarily) called with 'stopped_cpus' as its arg. 246 * 247 * - Signals all CPUs in map to restart. 248 * - Waits for each to restart. 249 * 250 * Returns: 251 * -1: error 252 * 0: NA 253 * 1: ok 254 */ 255 int 256 restart_cpus(u_int map) 257 { 258 259 if (!smp_started) 260 return 0; 261 262 CTR1(KTR_SMP, "restart_cpus(%x)", map); 263 264 /* signal other cpus to restart */ 265 atomic_store_rel_int(&started_cpus, map); 266 267 /* wait for each to clear its bit */ 268 while ((atomic_load_acq_int(&stopped_cpus) & map) != 0) 269 ; /* nothing */ 270 271 return 1; 272 } 273 274 /* 275 * All-CPU rendezvous. CPUs are signalled, all execute the setup function 276 * (if specified), rendezvous, execute the action function (if specified), 277 * rendezvous again, execute the teardown function (if specified), and then 278 * resume. 279 * 280 * Note that the supplied external functions _must_ be reentrant and aware 281 * that they are running in parallel and in an unknown lock context. 282 */ 283 void 284 smp_rendezvous_action(void) 285 { 286 287 /* setup function */ 288 if (smp_rv_setup_func != NULL) 289 smp_rv_setup_func(smp_rv_func_arg); 290 /* spin on entry rendezvous */ 291 atomic_add_int(&smp_rv_waiters[0], 1); 292 while (atomic_load_acq_int(&smp_rv_waiters[0]) < mp_ncpus) 293 ; /* nothing */ 294 /* action function */ 295 if (smp_rv_action_func != NULL) 296 smp_rv_action_func(smp_rv_func_arg); 297 /* spin on exit rendezvous */ 298 atomic_add_int(&smp_rv_waiters[1], 1); 299 while (atomic_load_acq_int(&smp_rv_waiters[1]) < mp_ncpus) 300 ; /* nothing */ 301 /* teardown function */ 302 if (smp_rv_teardown_func != NULL) 303 smp_rv_teardown_func(smp_rv_func_arg); 304 } 305 306 void 307 smp_rendezvous(void (* setup_func)(void *), 308 void (* action_func)(void *), 309 void (* teardown_func)(void *), 310 void *arg) 311 { 312 313 if (!smp_started) { 314 if (setup_func != NULL) 315 setup_func(arg); 316 if (action_func != NULL) 317 action_func(arg); 318 if (teardown_func != NULL) 319 teardown_func(arg); 320 return; 321 } 322 323 /* obtain rendezvous lock */ 324 mtx_lock_spin(&smp_rv_mtx); 325 326 /* set static function pointers */ 327 smp_rv_setup_func = setup_func; 328 smp_rv_action_func = action_func; 329 smp_rv_teardown_func = teardown_func; 330 smp_rv_func_arg = arg; 331 smp_rv_waiters[0] = 0; 332 smp_rv_waiters[1] = 0; 333 334 /* signal other processors, which will enter the IPI with interrupts off */ 335 ipi_all_but_self(IPI_RENDEZVOUS); 336 337 /* call executor function */ 338 smp_rendezvous_action(); 339 340 /* release lock */ 341 mtx_unlock_spin(&smp_rv_mtx); 342 } 343 #else /* !SMP */ 344 345 /* 346 * Provide dummy SMP support for UP kernels. Modules that need to use SMP 347 * APIs will still work using this dummy support. 348 */ 349 static void 350 mp_setvariables_for_up(void *dummy) 351 { 352 mp_ncpus = 1; 353 mp_maxid = PCPU_GET(cpuid); 354 all_cpus = PCPU_GET(cpumask); 355 KASSERT(PCPU_GET(cpuid) == 0, ("UP must have a CPU ID of zero")); 356 } 357 SYSINIT(cpu_mp_setvariables, SI_SUB_TUNABLES, SI_ORDER_FIRST, 358 mp_setvariables_for_up, NULL) 359 360 void 361 smp_rendezvous(void (* setup_func)(void *), 362 void (* action_func)(void *), 363 void (* teardown_func)(void *), 364 void *arg) 365 { 366 367 if (setup_func != NULL) 368 setup_func(arg); 369 if (action_func != NULL) 370 action_func(arg); 371 if (teardown_func != NULL) 372 teardown_func(arg); 373 } 374 #endif /* SMP */ 375