1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 /* 27 * Copyright (c) 2010, Intel Corporation. 28 * All rights reserved. 29 */ 30 31 #include <sys/types.h> 32 #include <sys/t_lock.h> 33 #include <sys/param.h> 34 #include <sys/segments.h> 35 #include <sys/sysmacros.h> 36 #include <sys/signal.h> 37 #include <sys/systm.h> 38 #include <sys/user.h> 39 #include <sys/mman.h> 40 #include <sys/vm.h> 41 42 #include <sys/disp.h> 43 #include <sys/class.h> 44 45 #include <sys/proc.h> 46 #include <sys/buf.h> 47 #include <sys/kmem.h> 48 49 #include <sys/reboot.h> 50 #include <sys/uadmin.h> 51 #include <sys/callb.h> 52 53 #include <sys/cred.h> 54 #include <sys/vnode.h> 55 #include <sys/file.h> 56 57 #include <sys/procfs.h> 58 #include <sys/acct.h> 59 60 #include <sys/vfs.h> 61 #include <sys/dnlc.h> 62 #include <sys/var.h> 63 #include <sys/cmn_err.h> 64 #include <sys/utsname.h> 65 #include <sys/debug.h> 66 67 #include <sys/dumphdr.h> 68 #include <sys/bootconf.h> 69 #include <sys/varargs.h> 70 #include <sys/promif.h> 71 #include <sys/modctl.h> 72 73 #include <sys/consdev.h> 74 #include <sys/frame.h> 75 76 #include <sys/sunddi.h> 77 #include <sys/ddidmareq.h> 78 #include <sys/psw.h> 79 #include <sys/regset.h> 80 #include <sys/privregs.h> 81 #include <sys/clock.h> 82 #include <sys/tss.h> 83 #include <sys/cpu.h> 84 #include <sys/stack.h> 85 #include <sys/trap.h> 86 #include <sys/pic.h> 87 #include <vm/hat.h> 88 #include <vm/anon.h> 89 #include <vm/as.h> 90 #include <vm/page.h> 91 #include <vm/seg.h> 92 #include <vm/seg_kmem.h> 93 #include <vm/seg_map.h> 94 #include <vm/seg_vn.h> 95 #include <vm/seg_kp.h> 96 #include <vm/hat_i86.h> 97 #include <sys/swap.h> 98 #include <sys/thread.h> 99 #include <sys/sysconf.h> 100 #include <sys/vm_machparam.h> 101 #include <sys/archsystm.h> 102 #include <sys/machsystm.h> 103 #include <sys/machlock.h> 104 #include <sys/x_call.h> 105 #include <sys/instance.h> 106 107 #include <sys/time.h> 108 #include <sys/smp_impldefs.h> 109 #include <sys/psm_types.h> 110 #include <sys/atomic.h> 111 #include <sys/panic.h> 112 #include <sys/cpuvar.h> 113 #include <sys/dtrace.h> 114 #include <sys/bl.h> 115 #include <sys/nvpair.h> 116 #include <sys/x86_archext.h> 117 #include <sys/pool_pset.h> 118 #include <sys/autoconf.h> 119 #include <sys/mem.h> 120 #include <sys/dumphdr.h> 121 #include <sys/compress.h> 122 #include <sys/cpu_module.h> 123 #if defined(__xpv) 124 #include <sys/hypervisor.h> 125 #include <sys/xpv_panic.h> 126 #endif 127 128 #include <sys/fastboot.h> 129 #include <sys/machelf.h> 130 #include <sys/kobj.h> 131 #include <sys/multiboot.h> 132 133 #ifdef TRAPTRACE 134 #include <sys/traptrace.h> 135 #endif /* TRAPTRACE */ 136 137 #include <c2/audit.h> 138 #include <sys/clock_impl.h> 139 140 extern void audit_enterprom(int); 141 extern void audit_exitprom(int); 142 143 /* 144 * Occassionally the kernel knows better whether to power-off or reboot. 145 */ 146 int force_shutdown_method = AD_UNKNOWN; 147 148 /* 149 * The panicbuf array is used to record messages and state: 150 */ 151 char panicbuf[PANICBUFSIZE]; 152 153 /* 154 * Flags to control Dynamic Reconfiguration features. 155 */ 156 uint64_t plat_dr_options; 157 158 /* 159 * Maximum physical address for memory DR operations. 160 */ 161 uint64_t plat_dr_physmax; 162 163 /* 164 * maxphys - used during physio 165 * klustsize - used for klustering by swapfs and specfs 166 */ 167 int maxphys = 56 * 1024; /* XXX See vm_subr.c - max b_count in physio */ 168 int klustsize = 56 * 1024; 169 170 caddr_t p0_va; /* Virtual address for accessing physical page 0 */ 171 172 /* 173 * defined here, though unused on x86, 174 * to make kstat_fr.c happy. 175 */ 176 int vac; 177 178 void debug_enter(char *); 179 180 extern void pm_cfb_check_and_powerup(void); 181 extern void pm_cfb_rele(void); 182 183 extern fastboot_info_t newkernel; 184 185 /* 186 * Machine dependent code to reboot. 187 * "mdep" is interpreted as a character pointer; if non-null, it is a pointer 188 * to a string to be used as the argument string when rebooting. 189 * 190 * "invoke_cb" is a boolean. It is set to true when mdboot() can safely 191 * invoke CB_CL_MDBOOT callbacks before shutting the system down, i.e. when 192 * we are in a normal shutdown sequence (interrupts are not blocked, the 193 * system is not panic'ing or being suspended). 194 */ 195 /*ARGSUSED*/ 196 void 197 mdboot(int cmd, int fcn, char *mdep, boolean_t invoke_cb) 198 { 199 processorid_t bootcpuid = 0; 200 static int is_first_quiesce = 1; 201 static int is_first_reset = 1; 202 int reset_status = 0; 203 static char fallback_str[] = "Falling back to regular reboot.\n"; 204 205 if (fcn == AD_FASTREBOOT && !newkernel.fi_valid) 206 fcn = AD_BOOT; 207 208 if (!panicstr) { 209 kpreempt_disable(); 210 if (fcn == AD_FASTREBOOT) { 211 mutex_enter(&cpu_lock); 212 if (CPU_ACTIVE(cpu_get(bootcpuid))) { 213 affinity_set(bootcpuid); 214 } 215 mutex_exit(&cpu_lock); 216 } else { 217 affinity_set(CPU_CURRENT); 218 } 219 } 220 221 if (force_shutdown_method != AD_UNKNOWN) 222 fcn = force_shutdown_method; 223 224 /* 225 * XXX - rconsvp is set to NULL to ensure that output messages 226 * are sent to the underlying "hardware" device using the 227 * monitor's printf routine since we are in the process of 228 * either rebooting or halting the machine. 229 */ 230 rconsvp = NULL; 231 232 /* 233 * Print the reboot message now, before pausing other cpus. 234 * There is a race condition in the printing support that 235 * can deadlock multiprocessor machines. 236 */ 237 if (!(fcn == AD_HALT || fcn == AD_POWEROFF)) 238 prom_printf("rebooting...\n"); 239 240 if (IN_XPV_PANIC()) 241 reset(); 242 243 /* 244 * We can't bring up the console from above lock level, so do it now 245 */ 246 pm_cfb_check_and_powerup(); 247 248 /* make sure there are no more changes to the device tree */ 249 devtree_freeze(); 250 251 if (invoke_cb) 252 (void) callb_execute_class(CB_CL_MDBOOT, NULL); 253 254 /* 255 * Clear any unresolved UEs from memory. 256 */ 257 page_retire_mdboot(); 258 259 #if defined(__xpv) 260 /* 261 * XXPV Should probably think some more about how we deal 262 * with panicing before it's really safe to panic. 263 * On hypervisors, we reboot very quickly.. Perhaps panic 264 * should only attempt to recover by rebooting if, 265 * say, we were able to mount the root filesystem, 266 * or if we successfully launched init(1m). 267 */ 268 if (panicstr && proc_init == NULL) 269 (void) HYPERVISOR_shutdown(SHUTDOWN_poweroff); 270 #endif 271 /* 272 * stop other cpus and raise our priority. since there is only 273 * one active cpu after this, and our priority will be too high 274 * for us to be preempted, we're essentially single threaded 275 * from here on out. 276 */ 277 (void) spl6(); 278 if (!panicstr) { 279 mutex_enter(&cpu_lock); 280 pause_cpus(NULL); 281 mutex_exit(&cpu_lock); 282 } 283 284 /* 285 * If the system is panicking, the preloaded kernel is valid, and 286 * fastreboot_onpanic has been set, and the system has been up for 287 * longer than fastreboot_onpanic_uptime (default to 10 minutes), 288 * choose Fast Reboot. 289 */ 290 if (fcn == AD_BOOT && panicstr && newkernel.fi_valid && 291 fastreboot_onpanic && 292 (panic_lbolt - lbolt_at_boot) > fastreboot_onpanic_uptime) { 293 fcn = AD_FASTREBOOT; 294 } 295 296 /* 297 * Try to quiesce devices. 298 */ 299 if (is_first_quiesce) { 300 /* 301 * Clear is_first_quiesce before calling quiesce_devices() 302 * so that if quiesce_devices() causes panics, it will not 303 * be invoked again. 304 */ 305 is_first_quiesce = 0; 306 307 quiesce_active = 1; 308 quiesce_devices(ddi_root_node(), &reset_status); 309 if (reset_status == -1) { 310 if (fcn == AD_FASTREBOOT && !force_fastreboot) { 311 prom_printf("Driver(s) not capable of fast " 312 "reboot.\n"); 313 prom_printf(fallback_str); 314 fastreboot_capable = 0; 315 fcn = AD_BOOT; 316 } else if (fcn != AD_FASTREBOOT) 317 fastreboot_capable = 0; 318 } 319 quiesce_active = 0; 320 } 321 322 /* 323 * Try to reset devices. reset_leaves() should only be called 324 * a) when there are no other threads that could be accessing devices, 325 * and 326 * b) on a system that's not capable of fast reboot (fastreboot_capable 327 * being 0), or on a system where quiesce_devices() failed to 328 * complete (quiesce_active being 1). 329 */ 330 if (is_first_reset && (!fastreboot_capable || quiesce_active)) { 331 /* 332 * Clear is_first_reset before calling reset_devices() 333 * so that if reset_devices() causes panics, it will not 334 * be invoked again. 335 */ 336 is_first_reset = 0; 337 reset_leaves(); 338 } 339 340 /* Verify newkernel checksum */ 341 if (fastreboot_capable && fcn == AD_FASTREBOOT && 342 fastboot_cksum_verify(&newkernel) != 0) { 343 fastreboot_capable = 0; 344 prom_printf("Fast reboot: checksum failed for the new " 345 "kernel.\n"); 346 prom_printf(fallback_str); 347 } 348 349 (void) spl8(); 350 351 if (fastreboot_capable && fcn == AD_FASTREBOOT) { 352 /* 353 * psm_shutdown is called within fast_reboot() 354 */ 355 fast_reboot(); 356 } else { 357 (*psm_shutdownf)(cmd, fcn); 358 359 if (fcn == AD_HALT || fcn == AD_POWEROFF) 360 halt((char *)NULL); 361 else 362 prom_reboot(""); 363 } 364 /*NOTREACHED*/ 365 } 366 367 /* mdpreboot - may be called prior to mdboot while root fs still mounted */ 368 /*ARGSUSED*/ 369 void 370 mdpreboot(int cmd, int fcn, char *mdep) 371 { 372 if (fcn == AD_FASTREBOOT && !fastreboot_capable) { 373 fcn = AD_BOOT; 374 #ifdef __xpv 375 cmn_err(CE_WARN, "Fast reboot is not supported on xVM"); 376 #else 377 cmn_err(CE_WARN, 378 "Fast reboot is not supported on this platform%s", 379 fastreboot_nosup_message()); 380 #endif 381 } 382 383 if (fcn == AD_FASTREBOOT) { 384 fastboot_load_kernel(mdep); 385 if (!newkernel.fi_valid) 386 fcn = AD_BOOT; 387 } 388 389 (*psm_preshutdownf)(cmd, fcn); 390 } 391 392 static void 393 stop_other_cpus(void) 394 { 395 ulong_t s = clear_int_flag(); /* fast way to keep CPU from changing */ 396 cpuset_t xcset; 397 398 CPUSET_ALL_BUT(xcset, CPU->cpu_id); 399 xc_priority(0, 0, 0, CPUSET2BV(xcset), (xc_func_t)mach_cpu_halt); 400 restore_int_flag(s); 401 } 402 403 /* 404 * Machine dependent abort sequence handling 405 */ 406 void 407 abort_sequence_enter(char *msg) 408 { 409 if (abort_enable == 0) { 410 if (AU_ZONE_AUDITING(GET_KCTX_GZ)) 411 audit_enterprom(0); 412 return; 413 } 414 if (AU_ZONE_AUDITING(GET_KCTX_GZ)) 415 audit_enterprom(1); 416 debug_enter(msg); 417 if (AU_ZONE_AUDITING(GET_KCTX_GZ)) 418 audit_exitprom(1); 419 } 420 421 /* 422 * Enter debugger. Called when the user types ctrl-alt-d or whenever 423 * code wants to enter the debugger and possibly resume later. 424 */ 425 void 426 debug_enter( 427 char *msg) /* message to print, possibly NULL */ 428 { 429 if (dtrace_debugger_init != NULL) 430 (*dtrace_debugger_init)(); 431 432 if (msg) 433 prom_printf("%s\n", msg); 434 435 if (boothowto & RB_DEBUG) 436 kmdb_enter(); 437 438 if (dtrace_debugger_fini != NULL) 439 (*dtrace_debugger_fini)(); 440 } 441 442 void 443 reset(void) 444 { 445 extern void acpi_reset_system(); 446 #if !defined(__xpv) 447 ushort_t *bios_memchk; 448 449 /* 450 * Can't use psm_map_phys or acpi_reset_system before the hat is 451 * initialized. 452 */ 453 if (khat_running) { 454 bios_memchk = (ushort_t *)psm_map_phys(0x472, 455 sizeof (ushort_t), PROT_READ | PROT_WRITE); 456 if (bios_memchk) 457 *bios_memchk = 0x1234; /* bios memory check disable */ 458 459 if (options_dip != NULL && 460 ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(), 0, 461 "efi-systab")) { 462 efi_reset(); 463 } 464 465 /* 466 * The problem with using stubs is that we can call 467 * acpi_reset_system only after the kernel is up and running. 468 * 469 * We should create a global state to keep track of how far 470 * up the kernel is but for the time being we will depend on 471 * bootops. bootops cleared in startup_end(). 472 */ 473 if (bootops == NULL) 474 acpi_reset_system(); 475 } 476 477 pc_reset(); 478 #else 479 if (IN_XPV_PANIC()) { 480 if (khat_running && bootops == NULL) { 481 acpi_reset_system(); 482 } 483 484 pc_reset(); 485 } 486 487 (void) HYPERVISOR_shutdown(SHUTDOWN_reboot); 488 panic("HYPERVISOR_shutdown() failed"); 489 #endif 490 /*NOTREACHED*/ 491 } 492 493 /* 494 * Halt the machine and return to the monitor 495 */ 496 void 497 halt(char *s) 498 { 499 stop_other_cpus(); /* send stop signal to other CPUs */ 500 if (s) 501 prom_printf("(%s) \n", s); 502 prom_exit_to_mon(); 503 /*NOTREACHED*/ 504 } 505 506 /* 507 * Initiate interrupt redistribution. 508 */ 509 void 510 i_ddi_intr_redist_all_cpus() 511 { 512 } 513 514 /* 515 * XXX These probably ought to live somewhere else 516 * XXX They are called from mem.c 517 */ 518 519 /* 520 * Convert page frame number to an OBMEM page frame number 521 * (i.e. put in the type bits -- zero for this implementation) 522 */ 523 pfn_t 524 impl_obmem_pfnum(pfn_t pf) 525 { 526 return (pf); 527 } 528 529 #ifdef NM_DEBUG 530 int nmi_test = 0; /* checked in intentry.s during clock int */ 531 int nmtest = -1; 532 nmfunc1(arg, rp) 533 int arg; 534 struct regs *rp; 535 { 536 printf("nmi called with arg = %x, regs = %x\n", arg, rp); 537 nmtest += 50; 538 if (arg == nmtest) { 539 printf("ip = %x\n", rp->r_pc); 540 return (1); 541 } 542 return (0); 543 } 544 545 #endif 546 547 #include <sys/bootsvcs.h> 548 549 /* Hacked up initialization for initial kernel check out is HERE. */ 550 /* The basic steps are: */ 551 /* kernel bootfuncs definition/initialization for KADB */ 552 /* kadb bootfuncs pointer initialization */ 553 /* putchar/getchar (interrupts disabled) */ 554 555 /* kadb bootfuncs pointer initialization */ 556 557 int 558 sysp_getchar() 559 { 560 int i; 561 ulong_t s; 562 563 if (cons_polledio == NULL) { 564 /* Uh oh */ 565 prom_printf("getchar called with no console\n"); 566 for (;;) 567 /* LOOP FOREVER */; 568 } 569 570 s = clear_int_flag(); 571 i = cons_polledio->cons_polledio_getchar( 572 cons_polledio->cons_polledio_argument); 573 restore_int_flag(s); 574 return (i); 575 } 576 577 void 578 sysp_putchar(int c) 579 { 580 ulong_t s; 581 582 /* 583 * We have no alternative but to drop the output on the floor. 584 */ 585 if (cons_polledio == NULL || 586 cons_polledio->cons_polledio_putchar == NULL) 587 return; 588 589 s = clear_int_flag(); 590 cons_polledio->cons_polledio_putchar( 591 cons_polledio->cons_polledio_argument, c); 592 restore_int_flag(s); 593 } 594 595 int 596 sysp_ischar() 597 { 598 int i; 599 ulong_t s; 600 601 if (cons_polledio == NULL || 602 cons_polledio->cons_polledio_ischar == NULL) 603 return (0); 604 605 s = clear_int_flag(); 606 i = cons_polledio->cons_polledio_ischar( 607 cons_polledio->cons_polledio_argument); 608 restore_int_flag(s); 609 return (i); 610 } 611 612 int 613 goany(void) 614 { 615 prom_printf("Type any key to continue "); 616 (void) prom_getchar(); 617 prom_printf("\n"); 618 return (1); 619 } 620 621 static struct boot_syscalls kern_sysp = { 622 sysp_getchar, /* unchar (*getchar)(); 7 */ 623 sysp_putchar, /* int (*putchar)(); 8 */ 624 sysp_ischar, /* int (*ischar)(); 9 */ 625 }; 626 627 #if defined(__xpv) 628 int using_kern_polledio; 629 #endif 630 631 void 632 kadb_uses_kernel() 633 { 634 /* 635 * This routine is now totally misnamed, since it does not in fact 636 * control kadb's I/O; it only controls the kernel's prom_* I/O. 637 */ 638 sysp = &kern_sysp; 639 #if defined(__xpv) 640 using_kern_polledio = 1; 641 #endif 642 } 643 644 /* 645 * the interface to the outside world 646 */ 647 648 /* 649 * poll_port -- wait for a register to achieve a 650 * specific state. Arguments are a mask of bits we care about, 651 * and two sub-masks. To return normally, all the bits in the 652 * first sub-mask must be ON, all the bits in the second sub- 653 * mask must be OFF. If about seconds pass without the register 654 * achieving the desired bit configuration, we return 1, else 655 * 0. 656 */ 657 int 658 poll_port(ushort_t port, ushort_t mask, ushort_t onbits, ushort_t offbits) 659 { 660 int i; 661 ushort_t maskval; 662 663 for (i = 500000; i; i--) { 664 maskval = inb(port) & mask; 665 if (((maskval & onbits) == onbits) && 666 ((maskval & offbits) == 0)) 667 return (0); 668 drv_usecwait(10); 669 } 670 return (1); 671 } 672 673 /* 674 * set_idle_cpu is called from idle() when a CPU becomes idle. 675 */ 676 /*LINTED: static unused */ 677 static uint_t last_idle_cpu; 678 679 /*ARGSUSED*/ 680 void 681 set_idle_cpu(int cpun) 682 { 683 last_idle_cpu = cpun; 684 (*psm_set_idle_cpuf)(cpun); 685 } 686 687 /* 688 * unset_idle_cpu is called from idle() when a CPU is no longer idle. 689 */ 690 /*ARGSUSED*/ 691 void 692 unset_idle_cpu(int cpun) 693 { 694 (*psm_unset_idle_cpuf)(cpun); 695 } 696 697 /* 698 * This routine is almost correct now, but not quite. It still needs the 699 * equivalent concept of "hres_last_tick", just like on the sparc side. 700 * The idea is to take a snapshot of the hi-res timer while doing the 701 * hrestime_adj updates under hres_lock in locore, so that the small 702 * interval between interrupt assertion and interrupt processing is 703 * accounted for correctly. Once we have this, the code below should 704 * be modified to subtract off hres_last_tick rather than hrtime_base. 705 * 706 * I'd have done this myself, but I don't have source to all of the 707 * vendor-specific hi-res timer routines (grrr...). The generic hook I 708 * need is something like "gethrtime_unlocked()", which would be just like 709 * gethrtime() but would assume that you're already holding CLOCK_LOCK(). 710 * This is what the GET_HRTIME() macro is for on sparc (although it also 711 * serves the function of making time available without a function call 712 * so you don't take a register window overflow while traps are disabled). 713 */ 714 void 715 pc_gethrestime(timestruc_t *tp) 716 { 717 int lock_prev; 718 timestruc_t now; 719 int nslt; /* nsec since last tick */ 720 int adj; /* amount of adjustment to apply */ 721 722 loop: 723 lock_prev = hres_lock; 724 now = hrestime; 725 nslt = (int)(gethrtime() - hres_last_tick); 726 if (nslt < 0) { 727 /* 728 * nslt < 0 means a tick came between sampling 729 * gethrtime() and hres_last_tick; restart the loop 730 */ 731 732 goto loop; 733 } 734 now.tv_nsec += nslt; 735 if (hrestime_adj != 0) { 736 if (hrestime_adj > 0) { 737 adj = (nslt >> ADJ_SHIFT); 738 if (adj > hrestime_adj) 739 adj = (int)hrestime_adj; 740 } else { 741 adj = -(nslt >> ADJ_SHIFT); 742 if (adj < hrestime_adj) 743 adj = (int)hrestime_adj; 744 } 745 now.tv_nsec += adj; 746 } 747 while ((unsigned long)now.tv_nsec >= NANOSEC) { 748 749 /* 750 * We might have a large adjustment or have been in the 751 * debugger for a long time; take care of (at most) four 752 * of those missed seconds (tv_nsec is 32 bits, so 753 * anything >4s will be wrapping around). However, 754 * anything more than 2 seconds out of sync will trigger 755 * timedelta from clock() to go correct the time anyway, 756 * so do what we can, and let the big crowbar do the 757 * rest. A similar correction while loop exists inside 758 * hres_tick(); in all cases we'd like tv_nsec to 759 * satisfy 0 <= tv_nsec < NANOSEC to avoid confusing 760 * user processes, but if tv_sec's a little behind for a 761 * little while, that's OK; time still monotonically 762 * increases. 763 */ 764 765 now.tv_nsec -= NANOSEC; 766 now.tv_sec++; 767 } 768 if ((hres_lock & ~1) != lock_prev) 769 goto loop; 770 771 *tp = now; 772 } 773 774 void 775 gethrestime_lasttick(timespec_t *tp) 776 { 777 int s; 778 779 s = hr_clock_lock(); 780 *tp = hrestime; 781 hr_clock_unlock(s); 782 } 783 784 time_t 785 gethrestime_sec(void) 786 { 787 timestruc_t now; 788 789 gethrestime(&now); 790 return (now.tv_sec); 791 } 792 793 /* 794 * Initialize a kernel thread's stack 795 */ 796 797 caddr_t 798 thread_stk_init(caddr_t stk) 799 { 800 ASSERT(((uintptr_t)stk & (STACK_ALIGN - 1)) == 0); 801 return (stk - SA(MINFRAME)); 802 } 803 804 /* 805 * Initialize lwp's kernel stack. 806 */ 807 808 #ifdef TRAPTRACE 809 /* 810 * There's a tricky interdependency here between use of sysenter and 811 * TRAPTRACE which needs recording to avoid future confusion (this is 812 * about the third time I've re-figured this out ..) 813 * 814 * Here's how debugging lcall works with TRAPTRACE. 815 * 816 * 1 We're in userland with a breakpoint on the lcall instruction. 817 * 2 We execute the instruction - the instruction pushes the userland 818 * %ss, %esp, %efl, %cs, %eip on the stack and zips into the kernel 819 * via the call gate. 820 * 3 The hardware raises a debug trap in kernel mode, the hardware 821 * pushes %efl, %cs, %eip and gets to dbgtrap via the idt. 822 * 4 dbgtrap pushes the error code and trapno and calls cmntrap 823 * 5 cmntrap finishes building a trap frame 824 * 6 The TRACE_REGS macros in cmntrap copy a REGSIZE worth chunk 825 * off the stack into the traptrace buffer. 826 * 827 * This means that the traptrace buffer contains the wrong values in 828 * %esp and %ss, but everything else in there is correct. 829 * 830 * Here's how debugging sysenter works with TRAPTRACE. 831 * 832 * a We're in userland with a breakpoint on the sysenter instruction. 833 * b We execute the instruction - the instruction pushes -nothing- 834 * on the stack, but sets %cs, %eip, %ss, %esp to prearranged 835 * values to take us to sys_sysenter, at the top of the lwp's 836 * stack. 837 * c goto 3 838 * 839 * At this point, because we got into the kernel without the requisite 840 * five pushes on the stack, if we didn't make extra room, we'd 841 * end up with the TRACE_REGS macro fetching the saved %ss and %esp 842 * values from negative (unmapped) stack addresses -- which really bites. 843 * That's why we do the '-= 8' below. 844 * 845 * XXX Note that reading "up" lwp0's stack works because t0 is declared 846 * right next to t0stack in locore.s 847 */ 848 #endif 849 850 caddr_t 851 lwp_stk_init(klwp_t *lwp, caddr_t stk) 852 { 853 caddr_t oldstk; 854 struct pcb *pcb = &lwp->lwp_pcb; 855 856 oldstk = stk; 857 stk -= SA(sizeof (struct regs) + SA(MINFRAME)); 858 #ifdef TRAPTRACE 859 stk -= 2 * sizeof (greg_t); /* space for phony %ss:%sp (see above) */ 860 #endif 861 stk = (caddr_t)((uintptr_t)stk & ~(STACK_ALIGN - 1ul)); 862 bzero(stk, oldstk - stk); 863 lwp->lwp_regs = (void *)(stk + SA(MINFRAME)); 864 865 /* 866 * Arrange that the virtualized %fs and %gs GDT descriptors 867 * have a well-defined initial state (present, ring 3 868 * and of type data). 869 */ 870 #if defined(__amd64) 871 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) 872 pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc; 873 else 874 pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc; 875 #elif defined(__i386) 876 pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc; 877 #endif /* __i386 */ 878 lwp_installctx(lwp); 879 return (stk); 880 } 881 882 /*ARGSUSED*/ 883 void 884 lwp_stk_fini(klwp_t *lwp) 885 {} 886 887 /* 888 * If we're not the panic CPU, we wait in panic_idle for reboot. 889 */ 890 void 891 panic_idle(void) 892 { 893 splx(ipltospl(CLOCK_LEVEL)); 894 (void) setjmp(&curthread->t_pcb); 895 896 dumpsys_helper(); 897 898 #ifndef __xpv 899 for (;;) 900 i86_halt(); 901 #else 902 for (;;) 903 ; 904 #endif 905 } 906 907 /* 908 * Stop the other CPUs by cross-calling them and forcing them to enter 909 * the panic_idle() loop above. 910 */ 911 /*ARGSUSED*/ 912 void 913 panic_stopcpus(cpu_t *cp, kthread_t *t, int spl) 914 { 915 processorid_t i; 916 cpuset_t xcset; 917 918 /* 919 * In the case of a Xen panic, the hypervisor has already stopped 920 * all of the CPUs. 921 */ 922 if (!IN_XPV_PANIC()) { 923 (void) splzs(); 924 925 CPUSET_ALL_BUT(xcset, cp->cpu_id); 926 xc_priority(0, 0, 0, CPUSET2BV(xcset), (xc_func_t)panic_idle); 927 } 928 929 for (i = 0; i < NCPU; i++) { 930 if (i != cp->cpu_id && cpu[i] != NULL && 931 (cpu[i]->cpu_flags & CPU_EXISTS)) 932 cpu[i]->cpu_flags |= CPU_QUIESCED; 933 } 934 } 935 936 /* 937 * Platform callback following each entry to panicsys(). 938 */ 939 /*ARGSUSED*/ 940 void 941 panic_enter_hw(int spl) 942 { 943 /* Nothing to do here */ 944 } 945 946 /* 947 * Platform-specific code to execute after panicstr is set: we invoke 948 * the PSM entry point to indicate that a panic has occurred. 949 */ 950 /*ARGSUSED*/ 951 void 952 panic_quiesce_hw(panic_data_t *pdp) 953 { 954 psm_notifyf(PSM_PANIC_ENTER); 955 956 cmi_panic_callback(); 957 958 #ifdef TRAPTRACE 959 /* 960 * Turn off TRAPTRACE 961 */ 962 TRAPTRACE_FREEZE; 963 #endif /* TRAPTRACE */ 964 } 965 966 /* 967 * Platform callback prior to writing crash dump. 968 */ 969 /*ARGSUSED*/ 970 void 971 panic_dump_hw(int spl) 972 { 973 /* Nothing to do here */ 974 } 975 976 void * 977 plat_traceback(void *fpreg) 978 { 979 #ifdef __xpv 980 if (IN_XPV_PANIC()) 981 return (xpv_traceback(fpreg)); 982 #endif 983 return (fpreg); 984 } 985 986 /*ARGSUSED*/ 987 void 988 plat_tod_fault(enum tod_fault_type tod_bad) 989 {} 990 991 /*ARGSUSED*/ 992 int 993 blacklist(int cmd, const char *scheme, nvlist_t *fmri, const char *class) 994 { 995 return (ENOTSUP); 996 } 997 998 /* 999 * The underlying console output routines are protected by raising IPL in case 1000 * we are still calling into the early boot services. Once we start calling 1001 * the kernel console emulator, it will disable interrupts completely during 1002 * character rendering (see sysp_putchar, for example). Refer to the comments 1003 * and code in common/os/console.c for more information on these callbacks. 1004 */ 1005 /*ARGSUSED*/ 1006 int 1007 console_enter(int busy) 1008 { 1009 return (splzs()); 1010 } 1011 1012 /*ARGSUSED*/ 1013 void 1014 console_exit(int busy, int spl) 1015 { 1016 splx(spl); 1017 } 1018 1019 /* 1020 * Allocate a region of virtual address space, unmapped. 1021 * Stubbed out except on sparc, at least for now. 1022 */ 1023 /*ARGSUSED*/ 1024 void * 1025 boot_virt_alloc(void *addr, size_t size) 1026 { 1027 return (addr); 1028 } 1029 1030 volatile unsigned long tenmicrodata; 1031 1032 void 1033 tenmicrosec(void) 1034 { 1035 extern int gethrtime_hires; 1036 1037 if (gethrtime_hires) { 1038 hrtime_t start, end; 1039 start = end = gethrtime(); 1040 while ((end - start) < (10 * (NANOSEC / MICROSEC))) { 1041 SMT_PAUSE(); 1042 end = gethrtime(); 1043 } 1044 } else { 1045 #if defined(__xpv) 1046 hrtime_t newtime; 1047 1048 newtime = xpv_gethrtime() + 10000; /* now + 10 us */ 1049 while (xpv_gethrtime() < newtime) 1050 SMT_PAUSE(); 1051 #else /* __xpv */ 1052 int i; 1053 1054 /* 1055 * Artificial loop to induce delay. 1056 */ 1057 for (i = 0; i < microdata; i++) 1058 tenmicrodata = microdata; 1059 #endif /* __xpv */ 1060 } 1061 } 1062 1063 /* 1064 * get_cpu_mstate() is passed an array of timestamps, NCMSTATES 1065 * long, and it fills in the array with the time spent on cpu in 1066 * each of the mstates, where time is returned in nsec. 1067 * 1068 * No guarantee is made that the returned values in times[] will 1069 * monotonically increase on sequential calls, although this will 1070 * be true in the long run. Any such guarantee must be handled by 1071 * the caller, if needed. This can happen if we fail to account 1072 * for elapsed time due to a generation counter conflict, yet we 1073 * did account for it on a prior call (see below). 1074 * 1075 * The complication is that the cpu in question may be updating 1076 * its microstate at the same time that we are reading it. 1077 * Because the microstate is only updated when the CPU's state 1078 * changes, the values in cpu_intracct[] can be indefinitely out 1079 * of date. To determine true current values, it is necessary to 1080 * compare the current time with cpu_mstate_start, and add the 1081 * difference to times[cpu_mstate]. 1082 * 1083 * This can be a problem if those values are changing out from 1084 * under us. Because the code path in new_cpu_mstate() is 1085 * performance critical, we have not added a lock to it. Instead, 1086 * we have added a generation counter. Before beginning 1087 * modifications, the counter is set to 0. After modifications, 1088 * it is set to the old value plus one. 1089 * 1090 * get_cpu_mstate() will not consider the values of cpu_mstate 1091 * and cpu_mstate_start to be usable unless the value of 1092 * cpu_mstate_gen is both non-zero and unchanged, both before and 1093 * after reading the mstate information. Note that we must 1094 * protect against out-of-order loads around accesses to the 1095 * generation counter. Also, this is a best effort approach in 1096 * that we do not retry should the counter be found to have 1097 * changed. 1098 * 1099 * cpu_intracct[] is used to identify time spent in each CPU 1100 * mstate while handling interrupts. Such time should be reported 1101 * against system time, and so is subtracted out from its 1102 * corresponding cpu_acct[] time and added to 1103 * cpu_acct[CMS_SYSTEM]. 1104 */ 1105 1106 void 1107 get_cpu_mstate(cpu_t *cpu, hrtime_t *times) 1108 { 1109 int i; 1110 hrtime_t now, start; 1111 uint16_t gen; 1112 uint16_t state; 1113 hrtime_t intracct[NCMSTATES]; 1114 1115 /* 1116 * Load all volatile state under the protection of membar. 1117 * cpu_acct[cpu_mstate] must be loaded to avoid double counting 1118 * of (now - cpu_mstate_start) by a change in CPU mstate that 1119 * arrives after we make our last check of cpu_mstate_gen. 1120 */ 1121 1122 now = gethrtime_unscaled(); 1123 gen = cpu->cpu_mstate_gen; 1124 1125 membar_consumer(); /* guarantee load ordering */ 1126 start = cpu->cpu_mstate_start; 1127 state = cpu->cpu_mstate; 1128 for (i = 0; i < NCMSTATES; i++) { 1129 intracct[i] = cpu->cpu_intracct[i]; 1130 times[i] = cpu->cpu_acct[i]; 1131 } 1132 membar_consumer(); /* guarantee load ordering */ 1133 1134 if (gen != 0 && gen == cpu->cpu_mstate_gen && now > start) 1135 times[state] += now - start; 1136 1137 for (i = 0; i < NCMSTATES; i++) { 1138 if (i == CMS_SYSTEM) 1139 continue; 1140 times[i] -= intracct[i]; 1141 if (times[i] < 0) { 1142 intracct[i] += times[i]; 1143 times[i] = 0; 1144 } 1145 times[CMS_SYSTEM] += intracct[i]; 1146 scalehrtime(×[i]); 1147 } 1148 scalehrtime(×[CMS_SYSTEM]); 1149 } 1150 1151 /* 1152 * This is a version of the rdmsr instruction that allows 1153 * an error code to be returned in the case of failure. 1154 */ 1155 int 1156 checked_rdmsr(uint_t msr, uint64_t *value) 1157 { 1158 if ((x86_feature & X86_MSR) == 0) 1159 return (ENOTSUP); 1160 *value = rdmsr(msr); 1161 return (0); 1162 } 1163 1164 /* 1165 * This is a version of the wrmsr instruction that allows 1166 * an error code to be returned in the case of failure. 1167 */ 1168 int 1169 checked_wrmsr(uint_t msr, uint64_t value) 1170 { 1171 if ((x86_feature & X86_MSR) == 0) 1172 return (ENOTSUP); 1173 wrmsr(msr, value); 1174 return (0); 1175 } 1176 1177 /* 1178 * The mem driver's usual method of using hat_devload() to establish a 1179 * temporary mapping will not work for foreign pages mapped into this 1180 * domain or for the special hypervisor-provided pages. For the foreign 1181 * pages, we often don't know which domain owns them, so we can't ask the 1182 * hypervisor to set up a new mapping. For the other pages, we don't have 1183 * a pfn, so we can't create a new PTE. For these special cases, we do a 1184 * direct uiomove() from the existing kernel virtual address. 1185 */ 1186 /*ARGSUSED*/ 1187 int 1188 plat_mem_do_mmio(struct uio *uio, enum uio_rw rw) 1189 { 1190 #if defined(__xpv) 1191 void *va = (void *)(uintptr_t)uio->uio_loffset; 1192 off_t pageoff = uio->uio_loffset & PAGEOFFSET; 1193 size_t nbytes = MIN((size_t)(PAGESIZE - pageoff), 1194 (size_t)uio->uio_iov->iov_len); 1195 1196 if ((rw == UIO_READ && 1197 (va == HYPERVISOR_shared_info || va == xen_info)) || 1198 (pfn_is_foreign(hat_getpfnum(kas.a_hat, va)))) 1199 return (uiomove(va, nbytes, rw, uio)); 1200 #endif 1201 return (ENOTSUP); 1202 } 1203 1204 pgcnt_t 1205 num_phys_pages() 1206 { 1207 pgcnt_t npages = 0; 1208 struct memlist *mp; 1209 1210 #if defined(__xpv) 1211 if (DOMAIN_IS_INITDOMAIN(xen_info)) 1212 return (xpv_nr_phys_pages()); 1213 #endif /* __xpv */ 1214 1215 for (mp = phys_install; mp != NULL; mp = mp->ml_next) 1216 npages += mp->ml_size >> PAGESHIFT; 1217 1218 return (npages); 1219 } 1220 1221 /* cpu threshold for compressed dumps */ 1222 #ifdef _LP64 1223 uint_t dump_plat_mincpu = DUMP_PLAT_X86_64_MINCPU; 1224 #else 1225 uint_t dump_plat_mincpu = DUMP_PLAT_X86_32_MINCPU; 1226 #endif 1227 1228 int 1229 dump_plat_addr() 1230 { 1231 #ifdef __xpv 1232 pfn_t pfn = mmu_btop(xen_info->shared_info) | PFN_IS_FOREIGN_MFN; 1233 mem_vtop_t mem_vtop; 1234 int cnt; 1235 1236 /* 1237 * On the hypervisor, we want to dump the page with shared_info on it. 1238 */ 1239 if (!IN_XPV_PANIC()) { 1240 mem_vtop.m_as = &kas; 1241 mem_vtop.m_va = HYPERVISOR_shared_info; 1242 mem_vtop.m_pfn = pfn; 1243 dumpvp_write(&mem_vtop, sizeof (mem_vtop_t)); 1244 cnt = 1; 1245 } else { 1246 cnt = dump_xpv_addr(); 1247 } 1248 return (cnt); 1249 #else 1250 return (0); 1251 #endif 1252 } 1253 1254 void 1255 dump_plat_pfn() 1256 { 1257 #ifdef __xpv 1258 pfn_t pfn = mmu_btop(xen_info->shared_info) | PFN_IS_FOREIGN_MFN; 1259 1260 if (!IN_XPV_PANIC()) 1261 dumpvp_write(&pfn, sizeof (pfn)); 1262 else 1263 dump_xpv_pfn(); 1264 #endif 1265 } 1266 1267 /*ARGSUSED*/ 1268 int 1269 dump_plat_data(void *dump_cbuf) 1270 { 1271 #ifdef __xpv 1272 uint32_t csize; 1273 int cnt; 1274 1275 if (!IN_XPV_PANIC()) { 1276 csize = (uint32_t)compress(HYPERVISOR_shared_info, dump_cbuf, 1277 PAGESIZE); 1278 dumpvp_write(&csize, sizeof (uint32_t)); 1279 dumpvp_write(dump_cbuf, csize); 1280 cnt = 1; 1281 } else { 1282 cnt = dump_xpv_data(dump_cbuf); 1283 } 1284 return (cnt); 1285 #else 1286 return (0); 1287 #endif 1288 } 1289 1290 /* 1291 * Calculates a linear address, given the CS selector and PC values 1292 * by looking up the %cs selector process's LDT or the CPU's GDT. 1293 * proc->p_ldtlock must be held across this call. 1294 */ 1295 int 1296 linear_pc(struct regs *rp, proc_t *p, caddr_t *linearp) 1297 { 1298 user_desc_t *descrp; 1299 caddr_t baseaddr; 1300 uint16_t idx = SELTOIDX(rp->r_cs); 1301 1302 ASSERT(rp->r_cs <= 0xFFFF); 1303 ASSERT(MUTEX_HELD(&p->p_ldtlock)); 1304 1305 if (SELISLDT(rp->r_cs)) { 1306 /* 1307 * Currently 64 bit processes cannot have private LDTs. 1308 */ 1309 ASSERT(p->p_model != DATAMODEL_LP64); 1310 1311 if (p->p_ldt == NULL) 1312 return (-1); 1313 1314 descrp = &p->p_ldt[idx]; 1315 baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp); 1316 1317 /* 1318 * Calculate the linear address (wraparound is not only ok, 1319 * it's expected behavior). The cast to uint32_t is because 1320 * LDT selectors are only allowed in 32-bit processes. 1321 */ 1322 *linearp = (caddr_t)(uintptr_t)(uint32_t)((uintptr_t)baseaddr + 1323 rp->r_pc); 1324 } else { 1325 #ifdef DEBUG 1326 descrp = &CPU->cpu_gdt[idx]; 1327 baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp); 1328 /* GDT-based descriptors' base addresses should always be 0 */ 1329 ASSERT(baseaddr == 0); 1330 #endif 1331 *linearp = (caddr_t)(uintptr_t)rp->r_pc; 1332 } 1333 1334 return (0); 1335 } 1336 1337 /* 1338 * The implementation of dtrace_linear_pc is similar to the that of 1339 * linear_pc, above, but here we acquire p_ldtlock before accessing 1340 * p_ldt. This implementation is used by the pid provider; we prefix 1341 * it with "dtrace_" to avoid inducing spurious tracing events. 1342 */ 1343 int 1344 dtrace_linear_pc(struct regs *rp, proc_t *p, caddr_t *linearp) 1345 { 1346 user_desc_t *descrp; 1347 caddr_t baseaddr; 1348 uint16_t idx = SELTOIDX(rp->r_cs); 1349 1350 ASSERT(rp->r_cs <= 0xFFFF); 1351 1352 if (SELISLDT(rp->r_cs)) { 1353 /* 1354 * Currently 64 bit processes cannot have private LDTs. 1355 */ 1356 ASSERT(p->p_model != DATAMODEL_LP64); 1357 1358 mutex_enter(&p->p_ldtlock); 1359 if (p->p_ldt == NULL) { 1360 mutex_exit(&p->p_ldtlock); 1361 return (-1); 1362 } 1363 descrp = &p->p_ldt[idx]; 1364 baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp); 1365 mutex_exit(&p->p_ldtlock); 1366 1367 /* 1368 * Calculate the linear address (wraparound is not only ok, 1369 * it's expected behavior). The cast to uint32_t is because 1370 * LDT selectors are only allowed in 32-bit processes. 1371 */ 1372 *linearp = (caddr_t)(uintptr_t)(uint32_t)((uintptr_t)baseaddr + 1373 rp->r_pc); 1374 } else { 1375 #ifdef DEBUG 1376 descrp = &CPU->cpu_gdt[idx]; 1377 baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp); 1378 /* GDT-based descriptors' base addresses should always be 0 */ 1379 ASSERT(baseaddr == 0); 1380 #endif 1381 *linearp = (caddr_t)(uintptr_t)rp->r_pc; 1382 } 1383 1384 return (0); 1385 } 1386 1387 /* 1388 * We need to post a soft interrupt to reprogram the lbolt cyclic when 1389 * switching from event to cyclic driven lbolt. The following code adds 1390 * and posts the softint for x86. 1391 */ 1392 static ddi_softint_hdl_impl_t lbolt_softint_hdl = 1393 {0, NULL, NULL, NULL, 0, NULL, NULL, NULL}; 1394 1395 void 1396 lbolt_softint_add(void) 1397 { 1398 (void) add_avsoftintr((void *)&lbolt_softint_hdl, LOCK_LEVEL, 1399 (avfunc)lbolt_ev_to_cyclic, "lbolt_ev_to_cyclic", NULL, NULL); 1400 } 1401 1402 void 1403 lbolt_softint_post(void) 1404 { 1405 (*setsoftint)(CBE_LOCK_PIL, lbolt_softint_hdl.ih_pending); 1406 } 1407 1408 boolean_t 1409 plat_dr_check_capability(uint64_t features) 1410 { 1411 return ((plat_dr_options & features) == features); 1412 } 1413 1414 boolean_t 1415 plat_dr_support_cpu(void) 1416 { 1417 return (plat_dr_options & PLAT_DR_FEATURE_CPU); 1418 } 1419 1420 boolean_t 1421 plat_dr_support_memory(void) 1422 { 1423 return (plat_dr_options & PLAT_DR_FEATURE_MEMORY); 1424 } 1425 1426 void 1427 plat_dr_enable_capability(uint64_t features) 1428 { 1429 atomic_or_64(&plat_dr_options, features); 1430 } 1431 1432 void 1433 plat_dr_disable_capability(uint64_t features) 1434 { 1435 atomic_and_64(&plat_dr_options, ~features); 1436 } 1437