1 /* $NetBSD: cpu.c,v 1.26 2008/05/16 17:20:01 bouyer Exp $ */ 2 /* NetBSD: cpu.c,v 1.18 2004/02/20 17:35:01 yamt Exp */ 3 4 /*- 5 * Copyright (c) 2000 The NetBSD Foundation, Inc. 6 * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi, 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by RedBack Networks Inc. 11 * 12 * Author: Bill Sommerfeld 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /* 37 * Copyright (c) 1999 Stefan Grefen 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. All advertising materials mentioning features or use of this software 48 * must display the following acknowledgement: 49 * This product includes software developed by the NetBSD 50 * Foundation, Inc. and its contributors. 51 * 4. Neither the name of The NetBSD Foundation nor the names of its 52 * contributors may be used to endorse or promote products derived 53 * from this software without specific prior written permission. 54 * 55 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY 56 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE 59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65 * SUCH DAMAGE. 66 */ 67 68 #include <sys/cdefs.h> 69 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.26 2008/05/16 17:20:01 bouyer Exp $"); 70 71 #include "opt_ddb.h" 72 #include "opt_multiprocessor.h" 73 #include "opt_mpbios.h" /* for MPDEBUG */ 74 #include "opt_mtrr.h" 75 #include "opt_xen.h" 76 77 #include "lapic.h" 78 #include "ioapic.h" 79 80 #include <sys/param.h> 81 #include <sys/proc.h> 82 #include <sys/user.h> 83 #include <sys/systm.h> 84 #include <sys/device.h> 85 #include <sys/malloc.h> 86 #include <sys/cpu.h> 87 #include <sys/atomic.h> 88 89 #include <uvm/uvm_extern.h> 90 91 #include <machine/cpufunc.h> 92 #include <machine/cpuvar.h> 93 #include <machine/pmap.h> 94 #include <machine/vmparam.h> 95 #include <machine/mpbiosvar.h> 96 #include <machine/pcb.h> 97 #include <machine/specialreg.h> 98 #include <machine/segments.h> 99 #include <machine/gdt.h> 100 #include <machine/mtrr.h> 101 #include <machine/pio.h> 102 103 #ifdef XEN3 104 #include <xen/vcpuvar.h> 105 #endif 106 107 #if NLAPIC > 0 108 #include <machine/apicvar.h> 109 #include <machine/i82489reg.h> 110 #include <machine/i82489var.h> 111 #endif 112 113 #include <dev/ic/mc146818reg.h> 114 #include <dev/isa/isareg.h> 115 116 int cpu_match(device_t, cfdata_t, void *); 117 void cpu_attach(device_t, device_t, void *); 118 #ifdef XEN3 119 int vcpu_match(device_t, cfdata_t, void *); 120 void vcpu_attach(device_t, device_t, void *); 121 #endif 122 void cpu_attach_common(device_t, device_t, void *); 123 void cpu_offline_md(void); 124 125 struct cpu_softc { 126 device_t sc_dev; /* device tree glue */ 127 struct cpu_info *sc_info; /* pointer to CPU info */ 128 }; 129 130 int mp_cpu_start(struct cpu_info *, paddr_t); 131 void mp_cpu_start_cleanup(struct cpu_info *); 132 const struct cpu_functions mp_cpu_funcs = { mp_cpu_start, NULL, 133 mp_cpu_start_cleanup }; 134 135 CFATTACH_DECL_NEW(cpu, sizeof(struct cpu_softc), 136 cpu_match, cpu_attach, NULL, NULL); 137 #ifdef XEN3 138 CFATTACH_DECL_NEW(vcpu, sizeof(struct cpu_softc), 139 vcpu_match, vcpu_attach, NULL, NULL); 140 #endif 141 142 /* 143 * Statically-allocated CPU info for the primary CPU (or the only 144 * CPU, on uniprocessors). The CPU info list is initialized to 145 * point at it. 146 */ 147 #ifdef TRAPLOG 148 #include <machine/tlog.h> 149 struct tlog tlog_primary; 150 #endif 151 struct cpu_info cpu_info_primary = { 152 .ci_dev = 0, 153 .ci_self = &cpu_info_primary, 154 .ci_idepth = -1, 155 .ci_curlwp = &lwp0, 156 .ci_curldt = -1, 157 #ifdef TRAPLOG 158 .ci_tlog = &tlog_primary, 159 #endif 160 161 }; 162 struct cpu_info phycpu_info_primary = { 163 .ci_dev = 0, 164 .ci_self = &phycpu_info_primary, 165 }; 166 167 struct cpu_info *cpu_info_list = &cpu_info_primary; 168 169 static void cpu_set_tss_gates(struct cpu_info *ci); 170 171 uint32_t cpus_attached = 0; 172 uint32_t cpus_running = 0; 173 174 bool x86_mp_online; 175 paddr_t mp_trampoline_paddr = MP_TRAMPOLINE; 176 177 struct cpu_info *phycpu_info[X86_MAXPROCS] = { &cpu_info_primary }; 178 179 #ifdef MULTIPROCESSOR 180 /* 181 * Array of CPU info structures. Must be statically-allocated because 182 * curproc, etc. are used early. 183 */ 184 struct cpu_info *cpu_info[X86_MAXPROCS] = { &cpu_info_primary }; 185 186 void cpu_hatch(void *); 187 static void cpu_boot_secondary(struct cpu_info *ci); 188 static void cpu_start_secondary(struct cpu_info *ci); 189 static void cpu_copy_trampoline(void); 190 191 /* 192 * Runs once per boot once multiprocessor goo has been detected and 193 * the local APIC on the boot processor has been mapped. 194 * 195 * Called from lapic_boot_init() (from mpbios_scan()). 196 */ 197 void 198 cpu_init_first(void) 199 { 200 int cpunum = lapic_cpu_number(); 201 202 if (cpunum != 0) { 203 cpu_info[0] = NULL; 204 cpu_info[cpunum] = &cpu_info_primary; 205 } 206 207 cpu_copy_trampoline(); 208 } 209 #endif 210 211 int 212 cpu_match(device_t parent, cfdata_t match, void *aux) 213 { 214 215 return 1; 216 } 217 218 void 219 cpu_attach(device_t parent, device_t self, void *aux) 220 { 221 #ifdef XEN3 222 struct cpu_softc *sc = device_private(self); 223 struct cpu_attach_args *caa = aux; 224 struct cpu_info *ci; 225 int cpunum = caa->cpu_number; 226 227 sc->sc_dev = self; 228 229 /* 230 * If we're an Application Processor, allocate a cpu_info 231 * structure, otherwise use the primary's. 232 */ 233 if (caa->cpu_role == CPU_ROLE_AP) { 234 ci = malloc(sizeof(*ci), M_DEVBUF, M_WAITOK | M_ZERO); 235 ci->ci_curldt = -1; 236 if (phycpu_info[cpunum] != NULL) 237 panic("cpu at apic id %d already attached?", cpunum); 238 phycpu_info[cpunum] = ci; 239 } else { 240 ci = &phycpu_info_primary; 241 if (cpunum != 0) { 242 phycpu_info[0] = NULL; 243 phycpu_info[cpunum] = ci; 244 } 245 } 246 247 ci->ci_self = ci; 248 sc->sc_info = ci; 249 250 ci->ci_dev = self; 251 ci->ci_cpuid = caa->cpu_number; 252 ci->ci_vcpu = NULL; 253 254 printf(": "); 255 switch (caa->cpu_role) { 256 case CPU_ROLE_SP: 257 printf("(uniprocessor)\n"); 258 ci->ci_flags |= CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY; 259 break; 260 261 case CPU_ROLE_BP: 262 printf("(boot processor)\n"); 263 ci->ci_flags |= CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY; 264 break; 265 266 case CPU_ROLE_AP: 267 /* 268 * report on an AP 269 */ 270 printf("(application processor)\n"); 271 break; 272 273 default: 274 panic("unknown processor type??\n"); 275 } 276 return; 277 #else 278 cpu_attach_common(parent, self, aux); 279 #endif 280 } 281 282 #ifdef XEN3 283 int 284 vcpu_match(device_t parent, cfdata_t match, void *aux) 285 { 286 struct vcpu_attach_args *vcaa = aux; 287 288 if (strcmp(vcaa->vcaa_name, match->cf_name) == 0) 289 return 1; 290 return 0; 291 } 292 293 void 294 vcpu_attach(device_t parent, device_t self, void *aux) 295 { 296 struct vcpu_attach_args *vcaa = aux; 297 298 cpu_attach_common(parent, self, &vcaa->vcaa_caa); 299 } 300 #endif 301 302 static void 303 cpu_vm_init(struct cpu_info *ci) 304 { 305 int ncolors = 2, i; 306 307 for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) { 308 struct x86_cache_info *cai; 309 int tcolors; 310 311 cai = &ci->ci_cinfo[i]; 312 313 tcolors = atop(cai->cai_totalsize); 314 switch(cai->cai_associativity) { 315 case 0xff: 316 tcolors = 1; /* fully associative */ 317 break; 318 case 0: 319 case 1: 320 break; 321 default: 322 tcolors /= cai->cai_associativity; 323 } 324 ncolors = max(ncolors, tcolors); 325 } 326 327 /* 328 * Knowing the size of the largest cache on this CPU, re-color 329 * our pages. 330 */ 331 if (ncolors <= uvmexp.ncolors) 332 return; 333 printf("%s: %d page colors\n", device_xname(ci->ci_dev), ncolors); 334 uvm_page_recolor(ncolors); 335 } 336 337 void 338 cpu_attach_common(device_t parent, device_t self, void *aux) 339 { 340 struct cpu_softc *sc = device_private(self); 341 struct cpu_attach_args *caa = aux; 342 struct cpu_info *ci; 343 uintptr_t ptr; 344 int cpunum = caa->cpu_number; 345 346 sc->sc_dev = self; 347 348 /* 349 * If we're an Application Processor, allocate a cpu_info 350 * structure, otherwise use the primary's. 351 */ 352 if (caa->cpu_role == CPU_ROLE_AP) { 353 if (cpunum >= X86_MAXPROCS) { 354 aprint_error(": apic id %d ignored, " 355 "please increase X86_MAXPROCS\n", cpunum); 356 } 357 358 aprint_naive(": Application Processor\n"); 359 ptr = (uintptr_t)malloc(sizeof(*ci) + CACHE_LINE_SIZE - 1, 360 M_DEVBUF, M_WAITOK); 361 ci = (struct cpu_info *)((ptr + CACHE_LINE_SIZE - 1) & 362 ~(CACHE_LINE_SIZE - 1)); 363 memset(ci, 0, sizeof(*ci)); 364 #if defined(MULTIPROCESSOR) 365 if (cpu_info[cpunum] != NULL) 366 panic("cpu at apic id %d already attached?", cpunum); 367 cpu_info[cpunum] = ci; 368 #endif 369 #ifdef TRAPLOG 370 ci->ci_tlog_base = malloc(sizeof(struct tlog), 371 M_DEVBUF, M_WAITOK); 372 #endif 373 } else { 374 aprint_naive(": %s Processor\n", 375 caa->cpu_role == CPU_ROLE_SP ? "Single" : "Boot"); 376 ci = &cpu_info_primary; 377 #if defined(MULTIPROCESSOR) 378 if (cpunum != lapic_cpu_number()) { 379 panic("%s: running CPU is at apic %d" 380 " instead of at expected %d", 381 device_xname(sc->sc_dev), lapic_cpu_number(), cpunum); 382 } 383 #endif 384 } 385 386 ci->ci_self = ci; 387 sc->sc_info = ci; 388 389 ci->ci_dev = self; 390 ci->ci_cpuid = cpunum; 391 392 KASSERT(HYPERVISOR_shared_info != NULL); 393 ci->ci_vcpu = &HYPERVISOR_shared_info->vcpu_info[cpunum]; 394 395 ci->ci_func = caa->cpu_func; 396 397 if (caa->cpu_role == CPU_ROLE_AP) { 398 #if defined(MULTIPROCESSOR) 399 int error; 400 401 error = mi_cpu_attach(ci); 402 if (error != 0) { 403 aprint_normal("\n"); 404 aprint_error_dev(sc->sc_dev, "mi_cpu_attach failed with %d\n", 405 error); 406 return; 407 } 408 #endif 409 } else { 410 KASSERT(ci->ci_data.cpu_idlelwp != NULL); 411 } 412 413 ci->ci_cpumask = (1 << cpu_index(ci)); 414 pmap_reference(pmap_kernel()); 415 ci->ci_pmap = pmap_kernel(); 416 ci->ci_tlbstate = TLBSTATE_STALE; 417 418 /* further PCB init done later. */ 419 420 switch (caa->cpu_role) { 421 case CPU_ROLE_SP: 422 atomic_or_32(&ci->ci_flags, 423 CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY); 424 cpu_intr_init(ci); 425 cpu_get_tsc_freq(ci); 426 cpu_identify(ci); 427 cpu_init(ci); 428 cpu_set_tss_gates(ci); 429 pmap_cpu_init_late(ci); 430 x86_cpu_idle_init(); 431 #if 0 432 x86_errata(); 433 #endif 434 break; 435 436 case CPU_ROLE_BP: 437 atomic_or_32(&ci->ci_flags, 438 CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY); 439 cpu_intr_init(ci); 440 cpu_get_tsc_freq(ci); 441 cpu_identify(ci); 442 cpu_init(ci); 443 cpu_set_tss_gates(ci); 444 pmap_cpu_init_late(ci); 445 x86_cpu_idle_init(); 446 #if NLAPIC > 0 447 /* 448 * Enable local apic 449 */ 450 lapic_enable(); 451 lapic_set_lvt(); 452 lapic_calibrate_timer(ci); 453 #endif 454 #if 0 455 x86_errata(); 456 #endif 457 break; 458 459 case CPU_ROLE_AP: 460 /* 461 * report on an AP 462 */ 463 464 #if defined(MULTIPROCESSOR) 465 cpu_intr_init(ci); 466 gdt_alloc_cpu(ci); 467 cpu_set_tss_gates(ci); 468 pmap_cpu_init_early(ci); 469 pmap_cpu_init_late(ci); 470 cpu_start_secondary(ci); 471 if (ci->ci_flags & CPUF_PRESENT) { 472 identifycpu(ci); 473 ci->ci_next = cpu_info_list->ci_next; 474 cpu_info_list->ci_next = ci; 475 } 476 #else 477 aprint_normal_dev(sc->sc_dev, "not started\n"); 478 #endif 479 break; 480 481 default: 482 aprint_normal("\n"); 483 panic("unknown processor type??\n"); 484 } 485 cpu_vm_init(ci); 486 487 cpus_attached |= (1 << ci->ci_cpuid); 488 489 #if 0 490 if (!pmf_device_register(self, cpu_suspend, cpu_resume)) 491 aprint_error_dev(self, "couldn't establish power handler\n"); 492 #endif 493 494 #if defined(MULTIPROCESSOR) 495 if (mp_verbose) { 496 struct lwp *l = ci->ci_data.cpu_idlelwp; 497 498 aprint_verbose_dev(sc->sc_dev, "idle lwp at %p, idle sp at 0x%p\n", 499 l, 500 #ifdef i386 501 (void *)l->l_addr->u_pcb.pcb_esp 502 #else 503 (void *)l->l_addr->u_pcb.pcb_rsp 504 #endif 505 ); 506 507 } 508 #endif 509 } 510 511 /* 512 * Initialize the processor appropriately. 513 */ 514 515 void 516 cpu_init(struct cpu_info *ci) 517 { 518 519 /* 520 * On a P6 or above, enable global TLB caching if the 521 * hardware supports it. 522 */ 523 if (cpu_feature & CPUID_PGE) 524 lcr4(rcr4() | CR4_PGE); /* enable global TLB caching */ 525 526 #ifdef XXXMTRR 527 /* 528 * On a P6 or above, initialize MTRR's if the hardware supports them. 529 */ 530 if (cpu_feature & CPUID_MTRR) { 531 if ((ci->ci_flags & CPUF_AP) == 0) 532 i686_mtrr_init_first(); 533 mtrr_init_cpu(ci); 534 } 535 #endif 536 /* 537 * If we have FXSAVE/FXRESTOR, use them. 538 */ 539 if (cpu_feature & CPUID_FXSR) { 540 lcr4(rcr4() | CR4_OSFXSR); 541 542 /* 543 * If we have SSE/SSE2, enable XMM exceptions. 544 */ 545 if (cpu_feature & (CPUID_SSE|CPUID_SSE2)) 546 lcr4(rcr4() | CR4_OSXMMEXCPT); 547 } 548 549 #ifdef MULTIPROCESSOR 550 atomic_or_32(&ci->ci_flags, CPUF_RUNNING); 551 atomic_or_32(&cpus_running, ci->ci_cpumask); 552 #endif 553 } 554 555 556 #ifdef MULTIPROCESSOR 557 void 558 cpu_boot_secondary_processors(void) 559 { 560 struct cpu_info *ci; 561 u_long i; 562 563 for (i = 0; i < X86_MAXPROCS; i++) { 564 ci = cpu_info[i]; 565 if (ci == NULL) 566 continue; 567 if (ci->ci_data.cpu_idlelwp == NULL) 568 continue; 569 if ((ci->ci_flags & CPUF_PRESENT) == 0) 570 continue; 571 if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY)) 572 continue; 573 cpu_boot_secondary(ci); 574 } 575 576 x86_mp_online = true; 577 } 578 579 static void 580 cpu_init_idle_lwp(struct cpu_info *ci) 581 { 582 struct lwp *l = ci->ci_data.cpu_idlelwp; 583 struct pcb *pcb = &l->l_addr->u_pcb; 584 585 pcb->pcb_cr0 = rcr0(); 586 } 587 588 void 589 cpu_init_idle_lwps(void) 590 { 591 struct cpu_info *ci; 592 u_long i; 593 594 for (i = 0; i < X86_MAXPROCS; i++) { 595 ci = cpu_info[i]; 596 if (ci == NULL) 597 continue; 598 if (ci->ci_data.cpu_idlelwp == NULL) 599 continue; 600 if ((ci->ci_flags & CPUF_PRESENT) == 0) 601 continue; 602 cpu_init_idle_lwp(ci); 603 } 604 } 605 606 void 607 cpu_start_secondary(struct cpu_info *ci) 608 { 609 int i; 610 struct pmap *kpm = pmap_kernel(); 611 extern uint32_t mp_pdirpa; 612 613 mp_pdirpa = kpm->pm_pdirpa; /* XXX move elsewhere, not per CPU. */ 614 615 atomic_or_32(&ci->ci_flags, CPUF_AP); 616 617 aprint_debug_dev(ci->ci_dev, "starting\n"); 618 619 ci->ci_curlwp = ci->ci_data.cpu_idlelwp; 620 if (CPU_STARTUP(ci, mp_trampoline_paddr) != 0) 621 return; 622 623 /* 624 * wait for it to become ready 625 */ 626 for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i > 0; i--) { 627 #ifdef MPDEBUG 628 extern int cpu_trace[3]; 629 static int otrace[3]; 630 if (memcmp(otrace, cpu_trace, sizeof(otrace)) != 0) { 631 aprint_debug_dev(ci->ci_dev, "trace %02x %02x %02x\n", 632 cpu_trace[0], cpu_trace[1], cpu_trace[2]); 633 memcpy(otrace, cpu_trace, sizeof(otrace)); 634 } 635 #endif 636 delay(10); 637 } 638 if ((ci->ci_flags & CPUF_PRESENT) == 0) { 639 aprint_error_dev(ci->ci_dev, "failed to become ready\n"); 640 #if defined(MPDEBUG) && defined(DDB) 641 printf("dropping into debugger; continue from here to resume boot\n"); 642 Debugger(); 643 #endif 644 } 645 646 CPU_START_CLEANUP(ci); 647 } 648 649 void 650 cpu_boot_secondary(struct cpu_info *ci) 651 { 652 int i; 653 654 atomic_or_32(&ci->ci_flags, CPUF_GO); 655 for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i > 0; i--) { 656 delay(10); 657 } 658 if ((ci->ci_flags & CPUF_RUNNING) == 0) { 659 aprint_error_dev(ci->ci_dev, "CPU failed to start\n"); 660 #if defined(MPDEBUG) && defined(DDB) 661 printf("dropping into debugger; continue from here to resume boot\n"); 662 Debugger(); 663 #endif 664 } 665 } 666 667 /* 668 * The CPU ends up here when its ready to run 669 * This is called from code in mptramp.s; at this point, we are running 670 * in the idle pcb/idle stack of the new CPU. When this function returns, 671 * this processor will enter the idle loop and start looking for work. 672 * 673 * XXX should share some of this with init386 in machdep.c 674 */ 675 void 676 cpu_hatch(void *v) 677 { 678 struct cpu_info *ci = (struct cpu_info *)v; 679 int s, i; 680 uint32_t blacklist_features; 681 682 #ifdef __x86_64__ 683 cpu_init_msrs(ci, true); 684 #endif 685 686 cpu_probe(ci); 687 688 /* not on Xen... */ 689 blacklist_features = ~(CPUID_PGE|CPUID_PSE|CPUID_MTRR|CPUID_FXSR|CPUID_NOX); /* XXX add CPUID_SVM */ 690 691 cpu_feature &= blacklist_features; 692 693 KDASSERT((ci->ci_flags & CPUF_PRESENT) == 0); 694 atomic_or_32(&ci->ci_flags, CPUF_PRESENT); 695 while ((ci->ci_flags & CPUF_GO) == 0) { 696 /* Don't use delay, boot CPU may be patching the text. */ 697 for (i = 10000; i != 0; i--) 698 x86_pause(); 699 } 700 701 /* Because the text may have been patched in x86_patch(). */ 702 wbinvd(); 703 x86_flush(); 704 705 KASSERT((ci->ci_flags & CPUF_RUNNING) == 0); 706 707 lcr3(pmap_kernel()->pm_pdirpa); 708 curlwp->l_addr->u_pcb.pcb_cr3 = pmap_kernel()->pm_pdirpa; 709 lcr0(ci->ci_data.cpu_idlelwp->l_addr->u_pcb.pcb_cr0); 710 cpu_init_idt(); 711 gdt_init_cpu(ci); 712 lapic_enable(); 713 lapic_set_lvt(); 714 lapic_initclocks(); 715 716 #ifdef i386 717 npxinit(ci); 718 #else 719 fpuinit(ci); 720 #endif 721 722 lldt(GSEL(GLDT_SEL, SEL_KPL)); 723 ltr(ci->ci_tss_sel); 724 725 cpu_init(ci); 726 cpu_get_tsc_freq(ci); 727 728 s = splhigh(); 729 #ifdef i386 730 lapic_tpr = 0; 731 #else 732 lcr8(0); 733 #endif 734 x86_enable_intr(); 735 splx(s); 736 #if 0 737 x86_errata(); 738 #endif 739 740 aprint_debug_dev(ci->ci_dev, "CPU %ld running\n", 741 (long)ci->ci_cpuid); 742 } 743 744 #if defined(DDB) 745 746 #include <ddb/db_output.h> 747 #include <machine/db_machdep.h> 748 749 /* 750 * Dump CPU information from ddb. 751 */ 752 void 753 cpu_debug_dump(void) 754 { 755 struct cpu_info *ci; 756 CPU_INFO_ITERATOR cii; 757 758 db_printf("addr dev id flags ipis curlwp fpcurlwp\n"); 759 for (CPU_INFO_FOREACH(cii, ci)) { 760 db_printf("%p %s %ld %x %x %10p %10p\n", 761 ci, 762 ci->ci_dev == NULL ? "BOOT" : device_xname(ci->ci_dev), 763 (long)ci->ci_cpuid, 764 ci->ci_flags, ci->ci_ipis, 765 ci->ci_curlwp, 766 ci->ci_fpcurlwp); 767 } 768 } 769 #endif 770 771 static void 772 cpu_copy_trampoline(void) 773 { 774 /* 775 * Copy boot code. 776 */ 777 extern u_char cpu_spinup_trampoline[]; 778 extern u_char cpu_spinup_trampoline_end[]; 779 780 vaddr_t mp_trampoline_vaddr; 781 782 mp_trampoline_vaddr = uvm_km_alloc(kernel_map, PAGE_SIZE, 0, 783 UVM_KMF_VAONLY); 784 785 pmap_kenter_pa(mp_trampoline_vaddr, mp_trampoline_paddr, 786 VM_PROT_READ | VM_PROT_WRITE); 787 pmap_update(pmap_kernel()); 788 memcpy((void *)mp_trampoline_vaddr, 789 cpu_spinup_trampoline, 790 cpu_spinup_trampoline_end - cpu_spinup_trampoline); 791 792 pmap_kremove(mp_trampoline_vaddr, PAGE_SIZE); 793 pmap_update(pmap_kernel()); 794 uvm_km_free(kernel_map, mp_trampoline_vaddr, PAGE_SIZE, UVM_KMF_VAONLY); 795 } 796 797 #endif 798 799 #ifdef i386 800 #if 0 801 static void 802 tss_init(struct i386tss *tss, void *stack, void *func) 803 { 804 memset(tss, 0, sizeof *tss); 805 tss->tss_esp0 = tss->tss_esp = (int)((char *)stack + USPACE - 16); 806 tss->tss_ss0 = GSEL(GDATA_SEL, SEL_KPL); 807 tss->__tss_cs = GSEL(GCODE_SEL, SEL_KPL); 808 tss->tss_fs = GSEL(GCPU_SEL, SEL_KPL); 809 tss->tss_gs = tss->__tss_es = tss->__tss_ds = 810 tss->__tss_ss = GSEL(GDATA_SEL, SEL_KPL); 811 tss->tss_cr3 = pmap_kernel()->pm_pdirpa; 812 tss->tss_esp = (int)((char *)stack + USPACE - 16); 813 tss->tss_ldt = GSEL(GLDT_SEL, SEL_KPL); 814 tss->__tss_eflags = PSL_MBO | PSL_NT; /* XXX not needed? */ 815 tss->__tss_eip = (int)func; 816 } 817 #endif 818 819 /* XXX */ 820 #define IDTVEC(name) __CONCAT(X, name) 821 typedef void (vector)(void); 822 extern vector IDTVEC(tss_trap08); 823 #ifdef DDB 824 extern vector Xintrddbipi; 825 extern int ddb_vec; 826 #endif 827 828 static void 829 cpu_set_tss_gates(struct cpu_info *ci) 830 { 831 #if 0 832 struct segment_descriptor sd; 833 834 ci->ci_doubleflt_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0, 835 UVM_KMF_WIRED); 836 tss_init(&ci->ci_doubleflt_tss, ci->ci_doubleflt_stack, 837 IDTVEC(tss_trap08)); 838 setsegment(&sd, &ci->ci_doubleflt_tss, sizeof(struct i386tss) - 1, 839 SDT_SYS386TSS, SEL_KPL, 0, 0); 840 ci->ci_gdt[GTRAPTSS_SEL].sd = sd; 841 setgate(&idt[8], NULL, 0, SDT_SYSTASKGT, SEL_KPL, 842 GSEL(GTRAPTSS_SEL, SEL_KPL)); 843 #endif 844 845 #if defined(DDB) && defined(MULTIPROCESSOR) 846 /* 847 * Set up separate handler for the DDB IPI, so that it doesn't 848 * stomp on a possibly corrupted stack. 849 * 850 * XXX overwriting the gate set in db_machine_init. 851 * Should rearrange the code so that it's set only once. 852 */ 853 ci->ci_ddbipi_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0, 854 UVM_KMF_WIRED); 855 tss_init(&ci->ci_ddbipi_tss, ci->ci_ddbipi_stack, 856 Xintrddbipi); 857 858 setsegment(&sd, &ci->ci_ddbipi_tss, sizeof(struct i386tss) - 1, 859 SDT_SYS386TSS, SEL_KPL, 0, 0); 860 ci->ci_gdt[GIPITSS_SEL].sd = sd; 861 862 setgate(&idt[ddb_vec], NULL, 0, SDT_SYSTASKGT, SEL_KPL, 863 GSEL(GIPITSS_SEL, SEL_KPL)); 864 #endif 865 } 866 #else 867 static void 868 cpu_set_tss_gates(struct cpu_info *ci) 869 { 870 871 } 872 #endif /* i386 */ 873 874 int 875 mp_cpu_start(struct cpu_info *ci, paddr_t target) 876 { 877 #if 0 878 #if NLAPIC > 0 879 int error; 880 #endif 881 unsigned short dwordptr[2]; 882 883 /* 884 * Bootstrap code must be addressable in real mode 885 * and it must be page aligned. 886 */ 887 KASSERT(target < 0x10000 && target % PAGE_SIZE == 0); 888 889 /* 890 * "The BSP must initialize CMOS shutdown code to 0Ah ..." 891 */ 892 893 outb(IO_RTC, NVRAM_RESET); 894 outb(IO_RTC+1, NVRAM_RESET_JUMP); 895 896 /* 897 * "and the warm reset vector (DWORD based at 40:67) to point 898 * to the AP startup code ..." 899 */ 900 901 dwordptr[0] = 0; 902 dwordptr[1] = target >> 4; 903 904 pmap_kenter_pa (0, 0, VM_PROT_READ|VM_PROT_WRITE); 905 memcpy ((uint8_t *) 0x467, dwordptr, 4); 906 pmap_kremove (0, PAGE_SIZE); 907 908 #if NLAPIC > 0 909 /* 910 * ... prior to executing the following sequence:" 911 */ 912 913 if (ci->ci_flags & CPUF_AP) { 914 if ((error = x86_ipi_init(ci->ci_cpuid)) != 0) 915 return error; 916 917 delay(10000); 918 919 if (cpu_feature & CPUID_APIC) { 920 error = x86_ipi_init(ci->ci_cpuid); 921 if (error != 0) { 922 aprint_error_dev(ci->ci_dev, "%s: IPI not taken (1)\n", 923 __func__); 924 return error; 925 } 926 927 delay(10000); 928 929 error = x86_ipi(target / PAGE_SIZE, ci->ci_cpuid, 930 LAPIC_DLMODE_STARTUP); 931 if (error != 0) { 932 aprint_error_dev(ci->ci_dev, "%s: IPI not taken (2)\n", 933 __func__); 934 return error; 935 } 936 delay(200); 937 938 error = x86_ipi(target / PAGE_SIZE, ci->ci_cpuid, 939 LAPIC_DLMODE_STARTUP); 940 if (error != 0) { 941 aprint_error_dev(ci->ci_dev, "%s: IPI not taken ((3)\n", 942 __func__); 943 return error; 944 } 945 delay(200); 946 } 947 } 948 #endif 949 #endif /* 0 */ 950 return 0; 951 } 952 953 void 954 mp_cpu_start_cleanup(struct cpu_info *ci) 955 { 956 #if 0 957 /* 958 * Ensure the NVRAM reset byte contains something vaguely sane. 959 */ 960 961 outb(IO_RTC, NVRAM_RESET); 962 outb(IO_RTC+1, NVRAM_RESET_RST); 963 #endif 964 } 965 966 #ifdef __x86_64__ 967 968 void 969 cpu_init_msrs(struct cpu_info *ci, bool full) 970 { 971 if (full) { 972 HYPERVISOR_set_segment_base (SEGBASE_FS, 0); 973 HYPERVISOR_set_segment_base (SEGBASE_GS_KERNEL, (uint64_t) ci); 974 HYPERVISOR_set_segment_base (SEGBASE_GS_USER, 0); 975 } 976 } 977 #endif /* __x86_64__ */ 978 979 void 980 cpu_offline_md(void) 981 { 982 int s; 983 984 s = splhigh(); 985 #ifdef __i386__ 986 npxsave_cpu(true); 987 #else 988 fpusave_cpu(true); 989 #endif 990 splx(s); 991 } 992 993 #if 0 994 /* XXX joerg restructure and restart CPUs individually */ 995 static bool 996 cpu_suspend(device_t dv PMF_FN_ARGS) 997 { 998 struct cpu_softc *sc = device_private(dv); 999 struct cpu_info *ci = sc->sc_info; 1000 int err; 1001 1002 if (ci->ci_flags & CPUF_PRIMARY) 1003 return true; 1004 if (ci->ci_data.cpu_idlelwp == NULL) 1005 return true; 1006 if ((ci->ci_flags & CPUF_PRESENT) == 0) 1007 return true; 1008 1009 sc->sc_wasonline = !(ci->ci_schedstate.spc_flags & SPCF_OFFLINE); 1010 1011 if (sc->sc_wasonline) { 1012 mutex_enter(&cpu_lock); 1013 err = cpu_setonline(ci, false); 1014 mutex_exit(&cpu_lock); 1015 1016 if (err) 1017 return false; 1018 } 1019 1020 return true; 1021 } 1022 1023 static bool 1024 cpu_resume(device_t dv PMF_FN_ARGS) 1025 { 1026 struct cpu_softc *sc = device_private(dv); 1027 struct cpu_info *ci = sc->sc_info; 1028 int err = 0; 1029 1030 if (ci->ci_flags & CPUF_PRIMARY) 1031 return true; 1032 if (ci->ci_data.cpu_idlelwp == NULL) 1033 return true; 1034 if ((ci->ci_flags & CPUF_PRESENT) == 0) 1035 return true; 1036 1037 if (sc->sc_wasonline) { 1038 mutex_enter(&cpu_lock); 1039 err = cpu_setonline(ci, true); 1040 mutex_exit(&cpu_lock); 1041 } 1042 1043 return err == 0; 1044 } 1045 #endif 1046 1047 void 1048 cpu_get_tsc_freq(struct cpu_info *ci) 1049 { 1050 #ifdef XEN3 1051 const volatile vcpu_time_info_t *tinfo = &ci->ci_vcpu->time; 1052 delay(1000000); 1053 uint64_t freq = 1000000000ULL << 32; 1054 freq = freq / (uint64_t)tinfo->tsc_to_system_mul; 1055 if ( tinfo->tsc_shift < 0 ) 1056 freq = freq << -tinfo->tsc_shift; 1057 else 1058 freq = freq >> tinfo->tsc_shift; 1059 ci->ci_data.cpu_cc_freq = freq; 1060 #else 1061 /* Xen2 */ 1062 /* XXX this needs to read the shared_info of the CPU being probed.. */ 1063 ci->ci_data.cpu_cc_freq = HYPERVISOR_shared_info->cpu_freq; 1064 #endif /* XEN3 */ 1065 } 1066 1067 void 1068 x86_cpu_idle_xen(void) 1069 { 1070 struct cpu_info *ci = curcpu(); 1071 1072 KASSERT(ci->ci_ilevel == IPL_NONE); 1073 1074 x86_disable_intr(); 1075 if (!__predict_false(ci->ci_want_resched)) { 1076 idle_block(); 1077 } else { 1078 x86_enable_intr(); 1079 } 1080 } 1081