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