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