1 /* $NetBSD: kern_cpu.c,v 1.97 2023/09/02 17:44:59 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2007, 2008, 2009, 2010, 2012, 2019 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c)2007 YAMAMOTO Takashi, 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * SUCH DAMAGE. 56 */ 57 58 /* 59 * CPU related routines not shared with rump. 60 */ 61 62 #include <sys/cdefs.h> 63 __KERNEL_RCSID(0, "$NetBSD: kern_cpu.c,v 1.97 2023/09/02 17:44:59 riastradh Exp $"); 64 65 #ifdef _KERNEL_OPT 66 #include "opt_cpu_ucode.h" 67 #include "opt_heartbeat.h" 68 #endif 69 70 #include <sys/param.h> 71 #include <sys/systm.h> 72 #include <sys/idle.h> 73 #include <sys/sched.h> 74 #include <sys/intr.h> 75 #include <sys/conf.h> 76 #include <sys/cpu.h> 77 #include <sys/cpuio.h> 78 #include <sys/proc.h> 79 #include <sys/percpu.h> 80 #include <sys/kernel.h> 81 #include <sys/kauth.h> 82 #include <sys/xcall.h> 83 #include <sys/pool.h> 84 #include <sys/kmem.h> 85 #include <sys/select.h> 86 #include <sys/namei.h> 87 #include <sys/callout.h> 88 #include <sys/pcu.h> 89 #include <sys/heartbeat.h> 90 91 #include <uvm/uvm_extern.h> 92 93 #include "ioconf.h" 94 95 /* 96 * If the port has stated that cpu_data is the first thing in cpu_info, 97 * verify that the claim is true. This will prevent them from getting out 98 * of sync. 99 */ 100 #ifdef __HAVE_CPU_DATA_FIRST 101 CTASSERT(offsetof(struct cpu_info, ci_data) == 0); 102 #else 103 CTASSERT(offsetof(struct cpu_info, ci_data) != 0); 104 #endif 105 106 int (*compat_cpuctl_ioctl)(struct lwp *, u_long, void *) = (void *)enosys; 107 108 static void cpu_xc_online(struct cpu_info *, void *); 109 static void cpu_xc_offline(struct cpu_info *, void *); 110 111 dev_type_ioctl(cpuctl_ioctl); 112 113 const struct cdevsw cpuctl_cdevsw = { 114 .d_open = nullopen, 115 .d_close = nullclose, 116 .d_read = nullread, 117 .d_write = nullwrite, 118 .d_ioctl = cpuctl_ioctl, 119 .d_stop = nullstop, 120 .d_tty = notty, 121 .d_poll = nopoll, 122 .d_mmap = nommap, 123 .d_kqfilter = nokqfilter, 124 .d_discard = nodiscard, 125 .d_flag = D_OTHER | D_MPSAFE 126 }; 127 128 int 129 mi_cpu_attach(struct cpu_info *ci) 130 { 131 int error; 132 133 KASSERT(maxcpus > 0); 134 135 if ((ci->ci_index = ncpu) >= maxcpus) 136 panic("Too many CPUs. Increase MAXCPUS?"); 137 kcpuset_set(kcpuset_attached, cpu_index(ci)); 138 139 /* 140 * Create a convenience cpuset of just ourselves. 141 */ 142 kcpuset_create(&ci->ci_kcpuset, true); 143 kcpuset_set(ci->ci_kcpuset, cpu_index(ci)); 144 145 TAILQ_INIT(&ci->ci_data.cpu_ld_locks); 146 __cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock); 147 148 /* This is useful for eg, per-cpu evcnt */ 149 snprintf(ci->ci_data.cpu_name, sizeof(ci->ci_data.cpu_name), "cpu%d", 150 cpu_index(ci)); 151 152 if (__predict_false(cpu_infos == NULL)) { 153 size_t ci_bufsize = (maxcpus + 1) * sizeof(struct cpu_info *); 154 cpu_infos = kmem_zalloc(ci_bufsize, KM_SLEEP); 155 } 156 cpu_infos[cpu_index(ci)] = ci; 157 158 sched_cpuattach(ci); 159 160 error = create_idle_lwp(ci); 161 if (error != 0) { 162 /* XXX revert sched_cpuattach */ 163 return error; 164 } 165 166 if (ci == curcpu()) 167 ci->ci_onproc = curlwp; 168 else 169 ci->ci_onproc = ci->ci_data.cpu_idlelwp; 170 171 percpu_init_cpu(ci); 172 softint_init(ci); 173 callout_init_cpu(ci); 174 xc_init_cpu(ci); 175 pool_cache_cpu_init(ci); 176 selsysinit(ci); 177 cache_cpu_init(ci); 178 TAILQ_INIT(&ci->ci_data.cpu_biodone); 179 ncpu++; 180 ncpuonline++; 181 182 return 0; 183 } 184 185 void 186 cpuctlattach(int dummy __unused) 187 { 188 189 KASSERT(cpu_infos != NULL); 190 } 191 192 int 193 cpuctl_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l) 194 { 195 CPU_INFO_ITERATOR cii; 196 cpustate_t *cs; 197 struct cpu_info *ci; 198 int error, i; 199 u_int id; 200 201 error = 0; 202 203 mutex_enter(&cpu_lock); 204 switch (cmd) { 205 case IOC_CPU_SETSTATE: 206 cs = data; 207 error = kauth_authorize_system(l->l_cred, 208 KAUTH_SYSTEM_CPU, KAUTH_REQ_SYSTEM_CPU_SETSTATE, cs, NULL, 209 NULL); 210 if (error != 0) 211 break; 212 if (cs->cs_id >= maxcpus || 213 (ci = cpu_lookup(cs->cs_id)) == NULL) { 214 error = ESRCH; 215 break; 216 } 217 cpu_setintr(ci, cs->cs_intr); /* XXX neglect errors */ 218 error = cpu_setstate(ci, cs->cs_online); 219 break; 220 221 case IOC_CPU_GETSTATE: 222 cs = data; 223 id = cs->cs_id; 224 memset(cs, 0, sizeof(*cs)); 225 cs->cs_id = id; 226 if (cs->cs_id >= maxcpus || 227 (ci = cpu_lookup(id)) == NULL) { 228 error = ESRCH; 229 break; 230 } 231 if ((ci->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0) 232 cs->cs_online = false; 233 else 234 cs->cs_online = true; 235 if ((ci->ci_schedstate.spc_flags & SPCF_NOINTR) != 0) 236 cs->cs_intr = false; 237 else 238 cs->cs_intr = true; 239 cs->cs_lastmod = (int32_t)ci->ci_schedstate.spc_lastmod; 240 cs->cs_lastmodhi = (int32_t) 241 (ci->ci_schedstate.spc_lastmod >> 32); 242 cs->cs_intrcnt = cpu_intr_count(ci) + 1; 243 cs->cs_hwid = ci->ci_cpuid; 244 break; 245 246 case IOC_CPU_MAPID: 247 i = 0; 248 for (CPU_INFO_FOREACH(cii, ci)) { 249 if (i++ == *(int *)data) 250 break; 251 } 252 if (ci == NULL) 253 error = ESRCH; 254 else 255 *(int *)data = cpu_index(ci); 256 break; 257 258 case IOC_CPU_GETCOUNT: 259 *(int *)data = ncpu; 260 break; 261 262 #ifdef CPU_UCODE 263 case IOC_CPU_UCODE_GET_VERSION: 264 error = cpu_ucode_get_version((struct cpu_ucode_version *)data); 265 break; 266 267 case IOC_CPU_UCODE_APPLY: 268 error = kauth_authorize_machdep(l->l_cred, 269 KAUTH_MACHDEP_CPU_UCODE_APPLY, 270 NULL, NULL, NULL, NULL); 271 if (error != 0) 272 break; 273 error = cpu_ucode_apply((const struct cpu_ucode *)data); 274 break; 275 #endif 276 277 default: 278 error = (*compat_cpuctl_ioctl)(l, cmd, data); 279 break; 280 } 281 mutex_exit(&cpu_lock); 282 283 return error; 284 } 285 286 struct cpu_info * 287 cpu_lookup(u_int idx) 288 { 289 struct cpu_info *ci; 290 291 /* 292 * cpu_infos is a NULL terminated array of MAXCPUS + 1 entries, 293 * so an index of MAXCPUS here is ok. See mi_cpu_attach. 294 */ 295 KASSERT(idx <= maxcpus); 296 297 if (__predict_false(cpu_infos == NULL)) { 298 KASSERT(idx == 0); 299 return curcpu(); 300 } 301 302 ci = cpu_infos[idx]; 303 KASSERT(ci == NULL || cpu_index(ci) == idx); 304 KASSERTMSG(idx < maxcpus || ci == NULL, "idx %d ci %p", idx, ci); 305 306 return ci; 307 } 308 309 static void 310 cpu_xc_offline(struct cpu_info *ci, void *unused) 311 { 312 struct schedstate_percpu *spc, *mspc = NULL; 313 struct cpu_info *target_ci; 314 struct lwp *l; 315 CPU_INFO_ITERATOR cii; 316 int s; 317 318 /* 319 * Thread that made the cross call (separate context) holds 320 * cpu_lock on our behalf. 321 */ 322 spc = &ci->ci_schedstate; 323 s = splsched(); 324 spc->spc_flags |= SPCF_OFFLINE; 325 splx(s); 326 327 /* Take the first available CPU for the migration. */ 328 for (CPU_INFO_FOREACH(cii, target_ci)) { 329 mspc = &target_ci->ci_schedstate; 330 if ((mspc->spc_flags & SPCF_OFFLINE) == 0) 331 break; 332 } 333 KASSERT(target_ci != NULL); 334 335 /* 336 * Migrate all non-bound threads to the other CPU. Note that this 337 * runs from the xcall thread, thus handling of LSONPROC is not needed. 338 */ 339 mutex_enter(&proc_lock); 340 LIST_FOREACH(l, &alllwp, l_list) { 341 struct cpu_info *mci; 342 343 lwp_lock(l); 344 if (l->l_cpu != ci || (l->l_pflag & (LP_BOUND | LP_INTR))) { 345 lwp_unlock(l); 346 continue; 347 } 348 /* Regular case - no affinity. */ 349 if (l->l_affinity == NULL) { 350 lwp_migrate(l, target_ci); 351 continue; 352 } 353 /* Affinity is set, find an online CPU in the set. */ 354 for (CPU_INFO_FOREACH(cii, mci)) { 355 mspc = &mci->ci_schedstate; 356 if ((mspc->spc_flags & SPCF_OFFLINE) == 0 && 357 kcpuset_isset(l->l_affinity, cpu_index(mci))) 358 break; 359 } 360 if (mci == NULL) { 361 lwp_unlock(l); 362 mutex_exit(&proc_lock); 363 goto fail; 364 } 365 lwp_migrate(l, mci); 366 } 367 mutex_exit(&proc_lock); 368 369 #if PCU_UNIT_COUNT > 0 370 pcu_save_all_on_cpu(); 371 #endif 372 373 heartbeat_suspend(); 374 375 #ifdef __HAVE_MD_CPU_OFFLINE 376 cpu_offline_md(); 377 #endif 378 return; 379 fail: 380 /* Just unset the SPCF_OFFLINE flag, caller will check */ 381 s = splsched(); 382 spc->spc_flags &= ~SPCF_OFFLINE; 383 splx(s); 384 } 385 386 static void 387 cpu_xc_online(struct cpu_info *ci, void *unused) 388 { 389 struct schedstate_percpu *spc; 390 int s; 391 392 heartbeat_resume(); 393 394 spc = &ci->ci_schedstate; 395 s = splsched(); 396 spc->spc_flags &= ~SPCF_OFFLINE; 397 splx(s); 398 } 399 400 int 401 cpu_setstate(struct cpu_info *ci, bool online) 402 { 403 struct schedstate_percpu *spc; 404 CPU_INFO_ITERATOR cii; 405 struct cpu_info *ci2; 406 uint64_t where; 407 xcfunc_t func; 408 int nonline; 409 410 spc = &ci->ci_schedstate; 411 412 KASSERT(mutex_owned(&cpu_lock)); 413 414 if (online) { 415 if ((spc->spc_flags & SPCF_OFFLINE) == 0) 416 return 0; 417 func = (xcfunc_t)cpu_xc_online; 418 } else { 419 if ((spc->spc_flags & SPCF_OFFLINE) != 0) 420 return 0; 421 nonline = 0; 422 /* 423 * Ensure that at least one CPU within the processor set 424 * stays online. Revisit this later. 425 */ 426 for (CPU_INFO_FOREACH(cii, ci2)) { 427 if ((ci2->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0) 428 continue; 429 if (ci2->ci_schedstate.spc_psid != spc->spc_psid) 430 continue; 431 nonline++; 432 } 433 if (nonline == 1) 434 return EBUSY; 435 func = (xcfunc_t)cpu_xc_offline; 436 } 437 438 where = xc_unicast(0, func, ci, NULL, ci); 439 xc_wait(where); 440 if (online) { 441 KASSERT((spc->spc_flags & SPCF_OFFLINE) == 0); 442 ncpuonline++; 443 } else { 444 if ((spc->spc_flags & SPCF_OFFLINE) == 0) { 445 /* If was not set offline, then it is busy */ 446 return EBUSY; 447 } 448 ncpuonline--; 449 } 450 451 spc->spc_lastmod = time_second; 452 return 0; 453 } 454 455 #if defined(__HAVE_INTR_CONTROL) 456 static void 457 cpu_xc_intr(struct cpu_info *ci, void *unused) 458 { 459 struct schedstate_percpu *spc; 460 int s; 461 462 spc = &ci->ci_schedstate; 463 s = splsched(); 464 spc->spc_flags &= ~SPCF_NOINTR; 465 splx(s); 466 } 467 468 static void 469 cpu_xc_nointr(struct cpu_info *ci, void *unused) 470 { 471 struct schedstate_percpu *spc; 472 int s; 473 474 spc = &ci->ci_schedstate; 475 s = splsched(); 476 spc->spc_flags |= SPCF_NOINTR; 477 splx(s); 478 } 479 480 int 481 cpu_setintr(struct cpu_info *ci, bool intr) 482 { 483 struct schedstate_percpu *spc; 484 CPU_INFO_ITERATOR cii; 485 struct cpu_info *ci2; 486 uint64_t where; 487 xcfunc_t func; 488 int nintr; 489 490 spc = &ci->ci_schedstate; 491 492 KASSERT(mutex_owned(&cpu_lock)); 493 494 if (intr) { 495 if ((spc->spc_flags & SPCF_NOINTR) == 0) 496 return 0; 497 func = (xcfunc_t)cpu_xc_intr; 498 } else { 499 if (CPU_IS_PRIMARY(ci)) /* XXX kern/45117 */ 500 return EINVAL; 501 if ((spc->spc_flags & SPCF_NOINTR) != 0) 502 return 0; 503 /* 504 * Ensure that at least one CPU within the system 505 * is handing device interrupts. 506 */ 507 nintr = 0; 508 for (CPU_INFO_FOREACH(cii, ci2)) { 509 if ((ci2->ci_schedstate.spc_flags & SPCF_NOINTR) != 0) 510 continue; 511 if (ci2 == ci) 512 continue; 513 nintr++; 514 } 515 if (nintr == 0) 516 return EBUSY; 517 func = (xcfunc_t)cpu_xc_nointr; 518 } 519 520 where = xc_unicast(0, func, ci, NULL, ci); 521 xc_wait(where); 522 if (intr) { 523 KASSERT((spc->spc_flags & SPCF_NOINTR) == 0); 524 } else if ((spc->spc_flags & SPCF_NOINTR) == 0) { 525 /* If was not set offline, then it is busy */ 526 return EBUSY; 527 } 528 529 /* Direct interrupts away from the CPU and record the change. */ 530 cpu_intr_redistribute(); 531 spc->spc_lastmod = time_second; 532 return 0; 533 } 534 #else /* __HAVE_INTR_CONTROL */ 535 int 536 cpu_setintr(struct cpu_info *ci, bool intr) 537 { 538 539 return EOPNOTSUPP; 540 } 541 542 u_int 543 cpu_intr_count(struct cpu_info *ci) 544 { 545 546 return 0; /* 0 == "don't know" */ 547 } 548 #endif /* __HAVE_INTR_CONTROL */ 549 550 #ifdef CPU_UCODE 551 int 552 cpu_ucode_load(struct cpu_ucode_softc *sc, const char *fwname) 553 { 554 firmware_handle_t fwh; 555 int error; 556 557 if (sc->sc_blob != NULL) { 558 firmware_free(sc->sc_blob, sc->sc_blobsize); 559 sc->sc_blob = NULL; 560 sc->sc_blobsize = 0; 561 } 562 563 error = cpu_ucode_md_open(&fwh, sc->loader_version, fwname); 564 if (error != 0) { 565 #ifdef DEBUG 566 printf("ucode: firmware_open(%s) failed: %i\n", fwname, error); 567 #endif 568 goto err0; 569 } 570 571 sc->sc_blobsize = firmware_get_size(fwh); 572 if (sc->sc_blobsize == 0) { 573 error = EFTYPE; 574 firmware_close(fwh); 575 goto err0; 576 } 577 sc->sc_blob = firmware_malloc(sc->sc_blobsize); 578 if (sc->sc_blob == NULL) { 579 error = ENOMEM; 580 firmware_close(fwh); 581 goto err0; 582 } 583 584 error = firmware_read(fwh, 0, sc->sc_blob, sc->sc_blobsize); 585 firmware_close(fwh); 586 if (error != 0) 587 goto err1; 588 589 return 0; 590 591 err1: 592 firmware_free(sc->sc_blob, sc->sc_blobsize); 593 sc->sc_blob = NULL; 594 sc->sc_blobsize = 0; 595 err0: 596 return error; 597 } 598 #endif 599