1 /* $NetBSD: subr_prf.c,v 1.156 2014/08/15 11:05:35 apb Exp $ */ 2 3 /*- 4 * Copyright (c) 1986, 1988, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: subr_prf.c,v 1.156 2014/08/15 11:05:35 apb Exp $"); 41 42 #include "opt_ddb.h" 43 #include "opt_ipkdb.h" 44 #include "opt_kgdb.h" 45 #include "opt_dump.h" 46 #include "opt_rnd_printf.h" 47 48 #include <sys/param.h> 49 #include <sys/stdint.h> 50 #include <sys/systm.h> 51 #include <sys/buf.h> 52 #include <sys/device.h> 53 #include <sys/reboot.h> 54 #include <sys/msgbuf.h> 55 #include <sys/proc.h> 56 #include <sys/ioctl.h> 57 #include <sys/vnode.h> 58 #include <sys/file.h> 59 #include <sys/tty.h> 60 #include <sys/tprintf.h> 61 #include <sys/spldebug.h> 62 #include <sys/syslog.h> 63 #include <sys/kprintf.h> 64 #include <sys/atomic.h> 65 #include <sys/kernel.h> 66 #include <sys/cpu.h> 67 #include <sys/sha2.h> 68 #include <sys/rnd.h> 69 70 #include <dev/cons.h> 71 72 #include <net/if.h> 73 74 #ifdef IPKDB 75 #include <ipkdb/ipkdb.h> 76 #endif 77 78 static kmutex_t kprintf_mtx; 79 static bool kprintf_inited = false; 80 81 #ifdef KGDB 82 #include <sys/kgdb.h> 83 #endif 84 85 #ifdef DDB 86 #include <ddb/ddbvar.h> /* db_panic */ 87 #include <ddb/db_output.h> /* db_printf, db_putchar prototypes */ 88 #endif 89 90 91 /* 92 * defines 93 */ 94 95 96 /* 97 * local prototypes 98 */ 99 100 static void putchar(int, int, struct tty *); 101 102 103 /* 104 * globals 105 */ 106 107 extern struct tty *constty; /* pointer to console "window" tty */ 108 extern int log_open; /* subr_log: is /dev/klog open? */ 109 extern krndsource_t rnd_printf_source; 110 const char *panicstr; /* arg to first call to panic (used as a flag 111 to indicate that panic has already been called). */ 112 struct cpu_info *paniccpu; /* cpu that first paniced */ 113 long panicstart, panicend; /* position in the msgbuf of the start and 114 end of the formatted panicstr. */ 115 int doing_shutdown; /* set to indicate shutdown in progress */ 116 117 #ifdef RND_PRINTF 118 static bool kprintf_inited_callout = false; 119 static SHA512_CTX kprnd_sha; 120 static uint8_t kprnd_accum[SHA512_DIGEST_LENGTH]; 121 static int kprnd_added; 122 123 static struct callout kprnd_callout; 124 #endif 125 126 #ifndef DUMP_ON_PANIC 127 #define DUMP_ON_PANIC 1 128 #endif 129 int dumponpanic = DUMP_ON_PANIC; 130 131 /* 132 * v_putc: routine to putc on virtual console 133 * 134 * the v_putc pointer can be used to redirect the console cnputc elsewhere 135 * [e.g. to a "virtual console"]. 136 */ 137 138 void (*v_putc)(int) = cnputc; /* start with cnputc (normal cons) */ 139 void (*v_flush)(void) = cnflush; /* start with cnflush (normal cons) */ 140 141 const char hexdigits[] = "0123456789abcdef"; 142 const char HEXDIGITS[] = "0123456789ABCDEF"; 143 144 145 /* 146 * functions 147 */ 148 149 #ifdef RND_PRINTF 150 static void kprintf_rnd_get(size_t bytes, void *priv) 151 { 152 if (kprnd_added) { 153 KASSERT(kprintf_inited); 154 if (mutex_tryenter(&kprintf_mtx)) { 155 SHA512_Final(kprnd_accum, &kprnd_sha); 156 rnd_add_data(&rnd_printf_source, 157 kprnd_accum, sizeof(kprnd_accum), 0); 158 kprnd_added = 0; 159 /* This, we must do, since we called _Final. */ 160 SHA512_Init(&kprnd_sha); 161 /* This is optional but seems useful. */ 162 SHA512_Update(&kprnd_sha, kprnd_accum, 163 sizeof(kprnd_accum)); 164 mutex_exit(&kprintf_mtx); 165 } 166 } 167 } 168 169 static void kprintf_rnd_callout(void *arg) 170 { 171 kprintf_rnd_get(0, NULL); 172 callout_schedule(&kprnd_callout, hz); 173 } 174 175 #endif 176 177 /* 178 * Locking is inited fairly early in MI bootstrap. Before that 179 * prints are done unlocked. But that doesn't really matter, 180 * since nothing can preempt us before interrupts are enabled. 181 */ 182 void 183 kprintf_init(void) 184 { 185 186 KASSERT(!kprintf_inited && cold); /* not foolproof, but ... */ 187 #ifdef RND_PRINTF 188 SHA512_Init(&kprnd_sha); 189 #endif 190 mutex_init(&kprintf_mtx, MUTEX_DEFAULT, IPL_HIGH); 191 kprintf_inited = true; 192 } 193 194 #ifdef RND_PRINTF 195 void 196 kprintf_init_callout(void) 197 { 198 KASSERT(!kprintf_inited_callout); 199 callout_init(&kprnd_callout, CALLOUT_MPSAFE); 200 callout_setfunc(&kprnd_callout, kprintf_rnd_callout, NULL); 201 callout_schedule(&kprnd_callout, hz); 202 kprintf_inited_callout = true; 203 } 204 #endif 205 206 void 207 kprintf_lock(void) 208 { 209 210 if (__predict_true(kprintf_inited)) 211 mutex_enter(&kprintf_mtx); 212 } 213 214 void 215 kprintf_unlock(void) 216 { 217 218 if (__predict_true(kprintf_inited)) { 219 /* assert kprintf wasn't somehow inited while we were in */ 220 KASSERT(mutex_owned(&kprintf_mtx)); 221 mutex_exit(&kprintf_mtx); 222 } 223 } 224 225 /* 226 * twiddle: spin a little propellor on the console. 227 */ 228 229 void 230 twiddle(void) 231 { 232 static const char twiddle_chars[] = "|/-\\"; 233 static int pos; 234 235 kprintf_lock(); 236 237 putchar(twiddle_chars[pos++ & 3], TOCONS, NULL); 238 putchar('\b', TOCONS, NULL); 239 240 kprintf_unlock(); 241 } 242 243 /* 244 * panic: handle an unresolvable fatal error 245 * 246 * prints "panic: <message>" and reboots. if called twice (i.e. recursive 247 * call) we avoid trying to dump and just reboot (to avoid recursive panics). 248 */ 249 250 void 251 panic(const char *fmt, ...) 252 { 253 va_list ap; 254 255 va_start(ap, fmt); 256 vpanic(fmt, ap); 257 va_end(ap); 258 } 259 260 void 261 vpanic(const char *fmt, va_list ap) 262 { 263 CPU_INFO_ITERATOR cii; 264 struct cpu_info *ci, *oci; 265 int bootopt; 266 static char scratchstr[256]; /* stores panic message */ 267 268 spldebug_stop(); 269 270 if (lwp0.l_cpu && curlwp) { 271 /* 272 * Disable preemption. If already panicing on another CPU, sit 273 * here and spin until the system is rebooted. Allow the CPU that 274 * first paniced to panic again. 275 */ 276 kpreempt_disable(); 277 ci = curcpu(); 278 oci = atomic_cas_ptr((void *)&paniccpu, NULL, ci); 279 if (oci != NULL && oci != ci) { 280 /* Give interrupts a chance to try and prevent deadlock. */ 281 for (;;) { 282 #ifndef _RUMPKERNEL /* XXXpooka: temporary build fix, see kern/40505 */ 283 DELAY(10); 284 #endif /* _RUMPKERNEL */ 285 } 286 } 287 288 /* 289 * Convert the current thread to a bound thread and prevent all 290 * CPUs from scheduling unbound jobs. Do so without taking any 291 * locks. 292 */ 293 curlwp->l_pflag |= LP_BOUND; 294 for (CPU_INFO_FOREACH(cii, ci)) { 295 ci->ci_schedstate.spc_flags |= SPCF_OFFLINE; 296 } 297 } 298 299 bootopt = RB_AUTOBOOT | RB_NOSYNC; 300 if (!doing_shutdown) { 301 if (dumponpanic) 302 bootopt |= RB_DUMP; 303 } else 304 printf("Skipping crash dump on recursive panic\n"); 305 306 doing_shutdown = 1; 307 308 if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC) 309 panicstart = msgbufp->msg_bufx; 310 311 printf("panic: "); 312 if (panicstr == NULL) { 313 /* first time in panic - store fmt first for precaution */ 314 panicstr = fmt; 315 316 vsnprintf(scratchstr, sizeof(scratchstr), fmt, ap); 317 printf("%s", scratchstr); 318 panicstr = scratchstr; 319 } else { 320 vprintf(fmt, ap); 321 } 322 printf("\n"); 323 324 if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC) 325 panicend = msgbufp->msg_bufx; 326 327 #ifdef IPKDB 328 ipkdb_panic(); 329 #endif 330 #ifdef KGDB 331 kgdb_panic(); 332 #endif 333 #ifdef KADB 334 if (boothowto & RB_KDB) 335 kdbpanic(); 336 #endif 337 #ifdef DDB 338 db_panic(); 339 #endif 340 cpu_reboot(bootopt, NULL); 341 } 342 343 /* 344 * kernel logging functions: log, logpri, addlog 345 */ 346 347 /* 348 * log: write to the log buffer 349 * 350 * => will not sleep [so safe to call from interrupt] 351 * => will log to console if /dev/klog isn't open 352 */ 353 354 void 355 log(int level, const char *fmt, ...) 356 { 357 va_list ap; 358 359 kprintf_lock(); 360 361 klogpri(level); /* log the level first */ 362 va_start(ap, fmt); 363 kprintf(fmt, TOLOG, NULL, NULL, ap); 364 va_end(ap); 365 if (!log_open) { 366 va_start(ap, fmt); 367 kprintf(fmt, TOCONS, NULL, NULL, ap); 368 va_end(ap); 369 } 370 371 kprintf_unlock(); 372 373 logwakeup(); /* wake up anyone waiting for log msgs */ 374 } 375 376 /* 377 * vlog: write to the log buffer [already have va_list] 378 */ 379 380 void 381 vlog(int level, const char *fmt, va_list ap) 382 { 383 va_list cap; 384 385 va_copy(cap, ap); 386 kprintf_lock(); 387 388 klogpri(level); /* log the level first */ 389 kprintf(fmt, TOLOG, NULL, NULL, ap); 390 if (!log_open) 391 kprintf(fmt, TOCONS, NULL, NULL, cap); 392 393 kprintf_unlock(); 394 va_end(cap); 395 396 logwakeup(); /* wake up anyone waiting for log msgs */ 397 } 398 399 /* 400 * logpri: log the priority level to the klog 401 */ 402 403 void 404 logpri(int level) 405 { 406 407 kprintf_lock(); 408 klogpri(level); 409 kprintf_unlock(); 410 } 411 412 /* 413 * Note: we must be in the mutex here! 414 */ 415 void 416 klogpri(int level) 417 { 418 char *p; 419 char snbuf[KPRINTF_BUFSIZE]; 420 421 putchar('<', TOLOG, NULL); 422 snprintf(snbuf, sizeof(snbuf), "%d", level); 423 for (p = snbuf ; *p ; p++) 424 putchar(*p, TOLOG, NULL); 425 putchar('>', TOLOG, NULL); 426 } 427 428 /* 429 * addlog: add info to previous log message 430 */ 431 432 void 433 addlog(const char *fmt, ...) 434 { 435 va_list ap; 436 437 kprintf_lock(); 438 439 va_start(ap, fmt); 440 kprintf(fmt, TOLOG, NULL, NULL, ap); 441 va_end(ap); 442 if (!log_open) { 443 va_start(ap, fmt); 444 kprintf(fmt, TOCONS, NULL, NULL, ap); 445 va_end(ap); 446 } 447 448 kprintf_unlock(); 449 450 logwakeup(); 451 } 452 453 454 /* 455 * putchar: print a single character on console or user terminal. 456 * 457 * => if console, then the last MSGBUFS chars are saved in msgbuf 458 * for inspection later (e.g. dmesg/syslog) 459 * => we must already be in the mutex! 460 */ 461 static void 462 putchar(int c, int flags, struct tty *tp) 463 { 464 #ifdef RND_PRINTF 465 uint8_t rbuf[SHA512_BLOCK_LENGTH]; 466 static int cursor; 467 #endif 468 if (panicstr) 469 constty = NULL; 470 if ((flags & TOCONS) && tp == NULL && constty) { 471 tp = constty; 472 flags |= TOTTY; 473 } 474 if ((flags & TOTTY) && tp && 475 tputchar(c, flags, tp) < 0 && 476 (flags & TOCONS) && tp == constty) 477 constty = NULL; 478 if ((flags & TOLOG) && 479 c != '\0' && c != '\r' && c != 0177) 480 logputchar(c); 481 if ((flags & TOCONS) && constty == NULL && c != '\0') 482 (*v_putc)(c); 483 #ifdef DDB 484 if (flags & TODDB) { 485 db_putchar(c); 486 return; 487 } 488 #endif 489 490 #ifdef RND_PRINTF 491 if (__predict_true(kprintf_inited)) { 492 rbuf[cursor] = c; 493 if (cursor == sizeof(rbuf) - 1) { 494 SHA512_Update(&kprnd_sha, rbuf, sizeof(rbuf)); 495 kprnd_added++; 496 cursor = 0; 497 } else { 498 cursor++; 499 } 500 } 501 #endif 502 } 503 504 /* 505 * tablefull: warn that a system table is full 506 */ 507 508 void 509 tablefull(const char *tab, const char *hint) 510 { 511 if (hint) 512 log(LOG_ERR, "%s: table is full - %s\n", tab, hint); 513 else 514 log(LOG_ERR, "%s: table is full\n", tab); 515 } 516 517 518 /* 519 * uprintf: print to the controlling tty of the current process 520 * 521 * => we may block if the tty queue is full 522 * => no message is printed if the queue doesn't clear in a reasonable 523 * time 524 */ 525 526 void 527 uprintf(const char *fmt, ...) 528 { 529 struct proc *p = curproc; 530 va_list ap; 531 532 /* mutex_enter(proc_lock); XXXSMP */ 533 534 if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) { 535 /* No mutex needed; going to process TTY. */ 536 va_start(ap, fmt); 537 kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap); 538 va_end(ap); 539 } 540 541 /* mutex_exit(proc_lock); XXXSMP */ 542 } 543 544 void 545 uprintf_locked(const char *fmt, ...) 546 { 547 struct proc *p = curproc; 548 va_list ap; 549 550 if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) { 551 /* No mutex needed; going to process TTY. */ 552 va_start(ap, fmt); 553 kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap); 554 va_end(ap); 555 } 556 } 557 558 /* 559 * tprintf functions: used to send messages to a specific process 560 * 561 * usage: 562 * get a tpr_t handle on a process "p" by using "tprintf_open(p)" 563 * use the handle when calling "tprintf" 564 * when done, do a "tprintf_close" to drop the handle 565 */ 566 567 /* 568 * tprintf_open: get a tprintf handle on a process "p" 569 * 570 * => returns NULL if process can't be printed to 571 */ 572 573 tpr_t 574 tprintf_open(struct proc *p) 575 { 576 tpr_t cookie; 577 578 cookie = NULL; 579 580 mutex_enter(proc_lock); 581 if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) { 582 proc_sesshold(p->p_session); 583 cookie = (tpr_t)p->p_session; 584 } 585 mutex_exit(proc_lock); 586 587 return cookie; 588 } 589 590 /* 591 * tprintf_close: dispose of a tprintf handle obtained with tprintf_open 592 */ 593 594 void 595 tprintf_close(tpr_t sess) 596 { 597 598 if (sess) { 599 mutex_enter(proc_lock); 600 /* Releases proc_lock. */ 601 proc_sessrele((struct session *)sess); 602 } 603 } 604 605 /* 606 * tprintf: given tprintf handle to a process [obtained with tprintf_open], 607 * send a message to the controlling tty for that process. 608 * 609 * => also sends message to /dev/klog 610 */ 611 void 612 tprintf(tpr_t tpr, const char *fmt, ...) 613 { 614 struct session *sess = (struct session *)tpr; 615 struct tty *tp = NULL; 616 int flags = TOLOG; 617 va_list ap; 618 619 /* mutex_enter(proc_lock); XXXSMP */ 620 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) { 621 flags |= TOTTY; 622 tp = sess->s_ttyp; 623 } 624 625 kprintf_lock(); 626 627 klogpri(LOG_INFO); 628 va_start(ap, fmt); 629 kprintf(fmt, flags, tp, NULL, ap); 630 va_end(ap); 631 632 kprintf_unlock(); 633 /* mutex_exit(proc_lock); XXXSMP */ 634 635 logwakeup(); 636 } 637 638 639 /* 640 * ttyprintf: send a message to a specific tty 641 * 642 * => should be used only by tty driver or anything that knows the 643 * underlying tty will not be revoked(2)'d away. [otherwise, 644 * use tprintf] 645 */ 646 void 647 ttyprintf(struct tty *tp, const char *fmt, ...) 648 { 649 va_list ap; 650 651 /* No mutex needed; going to process TTY. */ 652 va_start(ap, fmt); 653 kprintf(fmt, TOTTY, tp, NULL, ap); 654 va_end(ap); 655 } 656 657 #ifdef DDB 658 659 /* 660 * db_printf: printf for DDB (via db_putchar) 661 */ 662 663 void 664 db_printf(const char *fmt, ...) 665 { 666 va_list ap; 667 668 /* No mutex needed; DDB pauses all processors. */ 669 va_start(ap, fmt); 670 kprintf(fmt, TODDB, NULL, NULL, ap); 671 va_end(ap); 672 673 if (db_tee_msgbuf) { 674 va_start(ap, fmt); 675 kprintf(fmt, TOLOG, NULL, NULL, ap); 676 va_end(ap); 677 }; 678 } 679 680 void 681 db_vprintf(const char *fmt, va_list ap) 682 { 683 va_list cap; 684 685 va_copy(cap, ap); 686 /* No mutex needed; DDB pauses all processors. */ 687 kprintf(fmt, TODDB, NULL, NULL, ap); 688 if (db_tee_msgbuf) 689 kprintf(fmt, TOLOG, NULL, NULL, cap); 690 va_end(cap); 691 } 692 693 #endif /* DDB */ 694 695 static void 696 kprintf_internal(const char *fmt, int oflags, void *vp, char *sbuf, ...) 697 { 698 va_list ap; 699 700 va_start(ap, sbuf); 701 (void)kprintf(fmt, oflags, vp, sbuf, ap); 702 va_end(ap); 703 } 704 705 /* 706 * Device autoconfiguration printf routines. These change their 707 * behavior based on the AB_* flags in boothowto. If AB_SILENT 708 * is set, messages never go to the console (but they still always 709 * go to the log). AB_VERBOSE overrides AB_SILENT. 710 */ 711 712 /* 713 * aprint_normal: Send to console unless AB_QUIET. Always goes 714 * to the log. 715 */ 716 static void 717 aprint_normal_internal(const char *prefix, const char *fmt, va_list ap) 718 { 719 int flags = TOLOG; 720 721 if ((boothowto & (AB_SILENT|AB_QUIET)) == 0 || 722 (boothowto & AB_VERBOSE) != 0) 723 flags |= TOCONS; 724 725 kprintf_lock(); 726 727 if (prefix) 728 kprintf_internal("%s: ", flags, NULL, NULL, prefix); 729 kprintf(fmt, flags, NULL, NULL, ap); 730 731 kprintf_unlock(); 732 733 if (!panicstr) 734 logwakeup(); 735 } 736 737 void 738 aprint_normal(const char *fmt, ...) 739 { 740 va_list ap; 741 742 va_start(ap, fmt); 743 aprint_normal_internal(NULL, fmt, ap); 744 va_end(ap); 745 } 746 747 void 748 aprint_normal_dev(device_t dv, const char *fmt, ...) 749 { 750 va_list ap; 751 752 va_start(ap, fmt); 753 aprint_normal_internal(device_xname(dv), fmt, ap); 754 va_end(ap); 755 } 756 757 void 758 aprint_normal_ifnet(struct ifnet *ifp, const char *fmt, ...) 759 { 760 va_list ap; 761 762 va_start(ap, fmt); 763 aprint_normal_internal(ifp->if_xname, fmt, ap); 764 va_end(ap); 765 } 766 767 /* 768 * aprint_error: Send to console unless AB_QUIET. Always goes 769 * to the log. Also counts the number of times called so other 770 * parts of the kernel can report the number of errors during a 771 * given phase of system startup. 772 */ 773 static int aprint_error_count; 774 775 int 776 aprint_get_error_count(void) 777 { 778 int count; 779 780 kprintf_lock(); 781 782 count = aprint_error_count; 783 aprint_error_count = 0; 784 785 kprintf_unlock(); 786 787 return (count); 788 } 789 790 static void 791 aprint_error_internal(const char *prefix, const char *fmt, va_list ap) 792 { 793 int flags = TOLOG; 794 795 if ((boothowto & (AB_SILENT|AB_QUIET)) == 0 || 796 (boothowto & AB_VERBOSE) != 0) 797 flags |= TOCONS; 798 799 kprintf_lock(); 800 801 aprint_error_count++; 802 803 if (prefix) 804 kprintf_internal("%s: ", flags, NULL, NULL, prefix); 805 kprintf(fmt, flags, NULL, NULL, ap); 806 807 kprintf_unlock(); 808 809 if (!panicstr) 810 logwakeup(); 811 } 812 813 void 814 aprint_error(const char *fmt, ...) 815 { 816 va_list ap; 817 818 va_start(ap, fmt); 819 aprint_error_internal(NULL, fmt, ap); 820 va_end(ap); 821 } 822 823 void 824 aprint_error_dev(device_t dv, const char *fmt, ...) 825 { 826 va_list ap; 827 828 va_start(ap, fmt); 829 aprint_error_internal(device_xname(dv), fmt, ap); 830 va_end(ap); 831 } 832 833 void 834 aprint_error_ifnet(struct ifnet *ifp, const char *fmt, ...) 835 { 836 va_list ap; 837 838 va_start(ap, fmt); 839 aprint_error_internal(ifp->if_xname, fmt, ap); 840 va_end(ap); 841 } 842 843 /* 844 * aprint_naive: Send to console only if AB_QUIET. Never goes 845 * to the log. 846 */ 847 static void 848 aprint_naive_internal(const char *prefix, const char *fmt, va_list ap) 849 { 850 if ((boothowto & (AB_QUIET|AB_SILENT|AB_VERBOSE)) != AB_QUIET) 851 return; 852 853 kprintf_lock(); 854 855 if (prefix) 856 kprintf_internal("%s: ", TOCONS, NULL, NULL, prefix); 857 kprintf(fmt, TOCONS, NULL, NULL, ap); 858 859 kprintf_unlock(); 860 } 861 862 void 863 aprint_naive(const char *fmt, ...) 864 { 865 va_list ap; 866 867 va_start(ap, fmt); 868 aprint_naive_internal(NULL, fmt, ap); 869 va_end(ap); 870 } 871 872 void 873 aprint_naive_dev(device_t dv, const char *fmt, ...) 874 { 875 va_list ap; 876 877 va_start(ap, fmt); 878 aprint_naive_internal(device_xname(dv), fmt, ap); 879 va_end(ap); 880 } 881 882 void 883 aprint_naive_ifnet(struct ifnet *ifp, const char *fmt, ...) 884 { 885 va_list ap; 886 887 va_start(ap, fmt); 888 aprint_naive_internal(ifp->if_xname, fmt, ap); 889 va_end(ap); 890 } 891 892 /* 893 * aprint_verbose: Send to console only if AB_VERBOSE. Always 894 * goes to the log. 895 */ 896 static void 897 aprint_verbose_internal(const char *prefix, const char *fmt, va_list ap) 898 { 899 int flags = TOLOG; 900 901 if (boothowto & AB_VERBOSE) 902 flags |= TOCONS; 903 904 kprintf_lock(); 905 906 if (prefix) 907 kprintf_internal("%s: ", flags, NULL, NULL, prefix); 908 kprintf(fmt, flags, NULL, NULL, ap); 909 910 kprintf_unlock(); 911 912 if (!panicstr) 913 logwakeup(); 914 } 915 916 void 917 aprint_verbose(const char *fmt, ...) 918 { 919 va_list ap; 920 921 va_start(ap, fmt); 922 aprint_verbose_internal(NULL, fmt, ap); 923 va_end(ap); 924 } 925 926 void 927 aprint_verbose_dev(device_t dv, const char *fmt, ...) 928 { 929 va_list ap; 930 931 va_start(ap, fmt); 932 aprint_verbose_internal(device_xname(dv), fmt, ap); 933 va_end(ap); 934 } 935 936 void 937 aprint_verbose_ifnet(struct ifnet *ifp, const char *fmt, ...) 938 { 939 va_list ap; 940 941 va_start(ap, fmt); 942 aprint_verbose_internal(ifp->if_xname, fmt, ap); 943 va_end(ap); 944 } 945 946 /* 947 * aprint_debug: Send to console and log only if AB_DEBUG. 948 */ 949 static void 950 aprint_debug_internal(const char *prefix, const char *fmt, va_list ap) 951 { 952 if ((boothowto & AB_DEBUG) == 0) 953 return; 954 955 kprintf_lock(); 956 957 if (prefix) 958 kprintf_internal("%s: ", TOCONS | TOLOG, NULL, NULL, prefix); 959 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap); 960 961 kprintf_unlock(); 962 } 963 964 void 965 aprint_debug(const char *fmt, ...) 966 { 967 va_list ap; 968 969 va_start(ap, fmt); 970 aprint_debug_internal(NULL, fmt, ap); 971 va_end(ap); 972 } 973 974 void 975 aprint_debug_dev(device_t dv, const char *fmt, ...) 976 { 977 va_list ap; 978 979 va_start(ap, fmt); 980 aprint_debug_internal(device_xname(dv), fmt, ap); 981 va_end(ap); 982 } 983 984 void 985 aprint_debug_ifnet(struct ifnet *ifp, const char *fmt, ...) 986 { 987 va_list ap; 988 989 va_start(ap, fmt); 990 aprint_debug_internal(ifp->if_xname, fmt, ap); 991 va_end(ap); 992 } 993 994 void 995 printf_tolog(const char *fmt, ...) 996 { 997 va_list ap; 998 999 kprintf_lock(); 1000 1001 va_start(ap, fmt); 1002 kprintf(fmt, TOLOG, NULL, NULL, ap); 1003 va_end(ap); 1004 1005 kprintf_unlock(); 1006 } 1007 1008 /* 1009 * printf_nolog: Like printf(), but does not send message to the log. 1010 */ 1011 1012 void 1013 printf_nolog(const char *fmt, ...) 1014 { 1015 va_list ap; 1016 1017 kprintf_lock(); 1018 1019 va_start(ap, fmt); 1020 kprintf(fmt, TOCONS, NULL, NULL, ap); 1021 va_end(ap); 1022 1023 kprintf_unlock(); 1024 } 1025 1026 /* 1027 * normal kernel printf functions: printf, vprintf, snprintf, vsnprintf 1028 */ 1029 1030 /* 1031 * printf: print a message to the console and the log 1032 */ 1033 void 1034 printf(const char *fmt, ...) 1035 { 1036 va_list ap; 1037 1038 kprintf_lock(); 1039 1040 va_start(ap, fmt); 1041 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap); 1042 va_end(ap); 1043 1044 kprintf_unlock(); 1045 1046 if (!panicstr) 1047 logwakeup(); 1048 } 1049 1050 /* 1051 * vprintf: print a message to the console and the log [already have 1052 * va_list] 1053 */ 1054 1055 void 1056 vprintf(const char *fmt, va_list ap) 1057 { 1058 kprintf_lock(); 1059 1060 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap); 1061 1062 kprintf_unlock(); 1063 1064 if (!panicstr) 1065 logwakeup(); 1066 } 1067 1068 /* 1069 * snprintf: print a message to a buffer 1070 */ 1071 int 1072 snprintf(char *bf, size_t size, const char *fmt, ...) 1073 { 1074 int retval; 1075 va_list ap; 1076 1077 va_start(ap, fmt); 1078 retval = vsnprintf(bf, size, fmt, ap); 1079 va_end(ap); 1080 1081 return retval; 1082 } 1083 1084 /* 1085 * vsnprintf: print a message to a buffer [already have va_list] 1086 */ 1087 int 1088 vsnprintf(char *bf, size_t size, const char *fmt, va_list ap) 1089 { 1090 int retval; 1091 char *p; 1092 1093 p = bf + size; 1094 retval = kprintf(fmt, TOBUFONLY, &p, bf, ap); 1095 if (bf && size > 0) { 1096 /* nul terminate */ 1097 if (size <= (size_t)retval) 1098 bf[size - 1] = '\0'; 1099 else 1100 bf[retval] = '\0'; 1101 } 1102 return retval; 1103 } 1104 1105 /* 1106 * kprintf: scaled down version of printf(3). 1107 * 1108 * this version based on vfprintf() from libc which was derived from 1109 * software contributed to Berkeley by Chris Torek. 1110 * 1111 * NOTE: The kprintf mutex must be held if we're going TOBUF or TOCONS! 1112 */ 1113 1114 /* 1115 * macros for converting digits to letters and vice versa 1116 */ 1117 #define to_digit(c) ((c) - '0') 1118 #define is_digit(c) ((unsigned)to_digit(c) <= 9) 1119 #define to_char(n) ((n) + '0') 1120 1121 /* 1122 * flags used during conversion. 1123 */ 1124 #define ALT 0x001 /* alternate form */ 1125 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */ 1126 #define LADJUST 0x004 /* left adjustment */ 1127 #define LONGDBL 0x008 /* long double; unimplemented */ 1128 #define LONGINT 0x010 /* long integer */ 1129 #define QUADINT 0x020 /* quad integer */ 1130 #define SHORTINT 0x040 /* short integer */ 1131 #define MAXINT 0x080 /* intmax_t */ 1132 #define PTRINT 0x100 /* intptr_t */ 1133 #define SIZEINT 0x200 /* size_t */ 1134 #define ZEROPAD 0x400 /* zero (as opposed to blank) pad */ 1135 #define FPT 0x800 /* Floating point number */ 1136 1137 /* 1138 * To extend shorts properly, we need both signed and unsigned 1139 * argument extraction methods. 1140 */ 1141 #define SARG() \ 1142 (flags&MAXINT ? va_arg(ap, intmax_t) : \ 1143 flags&PTRINT ? va_arg(ap, intptr_t) : \ 1144 flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \ 1145 flags&QUADINT ? va_arg(ap, quad_t) : \ 1146 flags&LONGINT ? va_arg(ap, long) : \ 1147 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \ 1148 (long)va_arg(ap, int)) 1149 #define UARG() \ 1150 (flags&MAXINT ? va_arg(ap, uintmax_t) : \ 1151 flags&PTRINT ? va_arg(ap, uintptr_t) : \ 1152 flags&SIZEINT ? va_arg(ap, size_t) : \ 1153 flags&QUADINT ? va_arg(ap, u_quad_t) : \ 1154 flags&LONGINT ? va_arg(ap, u_long) : \ 1155 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \ 1156 (u_long)va_arg(ap, u_int)) 1157 1158 #define KPRINTF_PUTCHAR(C) { \ 1159 if (oflags == TOBUFONLY) { \ 1160 if (sbuf && ((vp == NULL) || (sbuf < tailp))) \ 1161 *sbuf++ = (C); \ 1162 } else { \ 1163 putchar((C), oflags, vp); \ 1164 } \ 1165 } 1166 1167 void 1168 device_printf(device_t dev, const char *fmt, ...) 1169 { 1170 va_list ap; 1171 1172 va_start(ap, fmt); 1173 printf("%s: ", device_xname(dev)); 1174 vprintf(fmt, ap); 1175 va_end(ap); 1176 return; 1177 } 1178 1179 /* 1180 * Guts of kernel printf. Note, we already expect to be in a mutex! 1181 */ 1182 int 1183 kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap) 1184 { 1185 const char *fmt; /* format string */ 1186 int ch; /* character from fmt */ 1187 int n; /* handy integer (short term usage) */ 1188 char *cp; /* handy char pointer (short term usage) */ 1189 int flags; /* flags as above */ 1190 int ret; /* return value accumulator */ 1191 int width; /* width from format (%8d), or 0 */ 1192 int prec; /* precision from format (%.3d), or -1 */ 1193 char sign; /* sign prefix (' ', '+', '-', or \0) */ 1194 1195 u_quad_t _uquad; /* integer arguments %[diouxX] */ 1196 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */ 1197 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 1198 int realsz; /* field size expanded by dprec */ 1199 int size; /* size of converted field or string */ 1200 const char *xdigs; /* digits for [xX] conversion */ 1201 char bf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */ 1202 char *tailp; /* tail pointer for snprintf */ 1203 1204 if (oflags == TOBUFONLY && (vp != NULL)) 1205 tailp = *(char **)vp; 1206 else 1207 tailp = NULL; 1208 1209 cp = NULL; /* XXX: shutup gcc */ 1210 size = 0; /* XXX: shutup gcc */ 1211 1212 fmt = fmt0; 1213 ret = 0; 1214 1215 xdigs = NULL; /* XXX: shut up gcc warning */ 1216 1217 /* 1218 * Scan the format for conversions (`%' character). 1219 */ 1220 for (;;) { 1221 for (; *fmt != '%' && *fmt; fmt++) { 1222 ret++; 1223 KPRINTF_PUTCHAR(*fmt); 1224 } 1225 if (*fmt == 0) 1226 goto done; 1227 1228 fmt++; /* skip over '%' */ 1229 1230 flags = 0; 1231 dprec = 0; 1232 width = 0; 1233 prec = -1; 1234 sign = '\0'; 1235 1236 rflag: ch = *fmt++; 1237 reswitch: switch (ch) { 1238 case ' ': 1239 /* 1240 * ``If the space and + flags both appear, the space 1241 * flag will be ignored.'' 1242 * -- ANSI X3J11 1243 */ 1244 if (!sign) 1245 sign = ' '; 1246 goto rflag; 1247 case '#': 1248 flags |= ALT; 1249 goto rflag; 1250 case '*': 1251 /* 1252 * ``A negative field width argument is taken as a 1253 * - flag followed by a positive field width.'' 1254 * -- ANSI X3J11 1255 * They don't exclude field widths read from args. 1256 */ 1257 if ((width = va_arg(ap, int)) >= 0) 1258 goto rflag; 1259 width = -width; 1260 /* FALLTHROUGH */ 1261 case '-': 1262 flags |= LADJUST; 1263 goto rflag; 1264 case '+': 1265 sign = '+'; 1266 goto rflag; 1267 case '.': 1268 if ((ch = *fmt++) == '*') { 1269 n = va_arg(ap, int); 1270 prec = n < 0 ? -1 : n; 1271 goto rflag; 1272 } 1273 n = 0; 1274 while (is_digit(ch)) { 1275 n = 10 * n + to_digit(ch); 1276 ch = *fmt++; 1277 } 1278 prec = n < 0 ? -1 : n; 1279 goto reswitch; 1280 case '0': 1281 /* 1282 * ``Note that 0 is taken as a flag, not as the 1283 * beginning of a field width.'' 1284 * -- ANSI X3J11 1285 */ 1286 flags |= ZEROPAD; 1287 goto rflag; 1288 case '1': case '2': case '3': case '4': 1289 case '5': case '6': case '7': case '8': case '9': 1290 n = 0; 1291 do { 1292 n = 10 * n + to_digit(ch); 1293 ch = *fmt++; 1294 } while (is_digit(ch)); 1295 width = n; 1296 goto reswitch; 1297 case 'h': 1298 flags |= SHORTINT; 1299 goto rflag; 1300 case 'j': 1301 flags |= MAXINT; 1302 goto rflag; 1303 case 'l': 1304 if (*fmt == 'l') { 1305 fmt++; 1306 flags |= QUADINT; 1307 } else { 1308 flags |= LONGINT; 1309 } 1310 goto rflag; 1311 case 'q': 1312 flags |= QUADINT; 1313 goto rflag; 1314 case 't': 1315 flags |= PTRINT; 1316 goto rflag; 1317 case 'z': 1318 flags |= SIZEINT; 1319 goto rflag; 1320 case 'c': 1321 *(cp = bf) = va_arg(ap, int); 1322 size = 1; 1323 sign = '\0'; 1324 break; 1325 case 'D': 1326 flags |= LONGINT; 1327 /*FALLTHROUGH*/ 1328 case 'd': 1329 case 'i': 1330 _uquad = SARG(); 1331 if ((quad_t)_uquad < 0) { 1332 _uquad = -_uquad; 1333 sign = '-'; 1334 } 1335 base = DEC; 1336 goto number; 1337 case 'n': 1338 if (flags & MAXINT) 1339 *va_arg(ap, intmax_t *) = ret; 1340 else if (flags & PTRINT) 1341 *va_arg(ap, intptr_t *) = ret; 1342 else if (flags & SIZEINT) 1343 *va_arg(ap, ssize_t *) = ret; 1344 else if (flags & QUADINT) 1345 *va_arg(ap, quad_t *) = ret; 1346 else if (flags & LONGINT) 1347 *va_arg(ap, long *) = ret; 1348 else if (flags & SHORTINT) 1349 *va_arg(ap, short *) = ret; 1350 else 1351 *va_arg(ap, int *) = ret; 1352 continue; /* no output */ 1353 case 'O': 1354 flags |= LONGINT; 1355 /*FALLTHROUGH*/ 1356 case 'o': 1357 _uquad = UARG(); 1358 base = OCT; 1359 goto nosign; 1360 case 'p': 1361 /* 1362 * ``The argument shall be a pointer to void. The 1363 * value of the pointer is converted to a sequence 1364 * of printable characters, in an implementation- 1365 * defined manner.'' 1366 * -- ANSI X3J11 1367 */ 1368 /* NOSTRICT */ 1369 _uquad = (u_long)va_arg(ap, void *); 1370 base = HEX; 1371 xdigs = hexdigits; 1372 flags |= HEXPREFIX; 1373 ch = 'x'; 1374 goto nosign; 1375 case 's': 1376 if ((cp = va_arg(ap, char *)) == NULL) 1377 /*XXXUNCONST*/ 1378 cp = __UNCONST("(null)"); 1379 if (prec >= 0) { 1380 /* 1381 * can't use strlen; can only look for the 1382 * NUL in the first `prec' characters, and 1383 * strlen() will go further. 1384 */ 1385 char *p = memchr(cp, 0, prec); 1386 1387 if (p != NULL) { 1388 size = p - cp; 1389 if (size > prec) 1390 size = prec; 1391 } else 1392 size = prec; 1393 } else 1394 size = strlen(cp); 1395 sign = '\0'; 1396 break; 1397 case 'U': 1398 flags |= LONGINT; 1399 /*FALLTHROUGH*/ 1400 case 'u': 1401 _uquad = UARG(); 1402 base = DEC; 1403 goto nosign; 1404 case 'X': 1405 xdigs = HEXDIGITS; 1406 goto hex; 1407 case 'x': 1408 xdigs = hexdigits; 1409 hex: _uquad = UARG(); 1410 base = HEX; 1411 /* leading 0x/X only if non-zero */ 1412 if (flags & ALT && _uquad != 0) 1413 flags |= HEXPREFIX; 1414 1415 /* unsigned conversions */ 1416 nosign: sign = '\0'; 1417 /* 1418 * ``... diouXx conversions ... if a precision is 1419 * specified, the 0 flag will be ignored.'' 1420 * -- ANSI X3J11 1421 */ 1422 number: if ((dprec = prec) >= 0) 1423 flags &= ~ZEROPAD; 1424 1425 /* 1426 * ``The result of converting a zero value with an 1427 * explicit precision of zero is no characters.'' 1428 * -- ANSI X3J11 1429 */ 1430 cp = bf + KPRINTF_BUFSIZE; 1431 if (_uquad != 0 || prec != 0) { 1432 /* 1433 * Unsigned mod is hard, and unsigned mod 1434 * by a constant is easier than that by 1435 * a variable; hence this switch. 1436 */ 1437 switch (base) { 1438 case OCT: 1439 do { 1440 *--cp = to_char(_uquad & 7); 1441 _uquad >>= 3; 1442 } while (_uquad); 1443 /* handle octal leading 0 */ 1444 if (flags & ALT && *cp != '0') 1445 *--cp = '0'; 1446 break; 1447 1448 case DEC: 1449 /* many numbers are 1 digit */ 1450 while (_uquad >= 10) { 1451 *--cp = to_char(_uquad % 10); 1452 _uquad /= 10; 1453 } 1454 *--cp = to_char(_uquad); 1455 break; 1456 1457 case HEX: 1458 do { 1459 *--cp = xdigs[_uquad & 15]; 1460 _uquad >>= 4; 1461 } while (_uquad); 1462 break; 1463 1464 default: 1465 /*XXXUNCONST*/ 1466 cp = __UNCONST("bug in kprintf: bad base"); 1467 size = strlen(cp); 1468 goto skipsize; 1469 } 1470 } 1471 size = bf + KPRINTF_BUFSIZE - cp; 1472 skipsize: 1473 break; 1474 default: /* "%?" prints ?, unless ? is NUL */ 1475 if (ch == '\0') 1476 goto done; 1477 /* pretend it was %c with argument ch */ 1478 cp = bf; 1479 *cp = ch; 1480 size = 1; 1481 sign = '\0'; 1482 break; 1483 } 1484 1485 /* 1486 * All reasonable formats wind up here. At this point, `cp' 1487 * points to a string which (if not flags&LADJUST) should be 1488 * padded out to `width' places. If flags&ZEROPAD, it should 1489 * first be prefixed by any sign or other prefix; otherwise, 1490 * it should be blank padded before the prefix is emitted. 1491 * After any left-hand padding and prefixing, emit zeroes 1492 * required by a decimal [diouxX] precision, then print the 1493 * string proper, then emit zeroes required by any leftover 1494 * floating precision; finally, if LADJUST, pad with blanks. 1495 * 1496 * Compute actual size, so we know how much to pad. 1497 * size excludes decimal prec; realsz includes it. 1498 */ 1499 realsz = dprec > size ? dprec : size; 1500 if (sign) 1501 realsz++; 1502 else if (flags & HEXPREFIX) 1503 realsz+= 2; 1504 1505 /* adjust ret */ 1506 ret += width > realsz ? width : realsz; 1507 1508 /* right-adjusting blank padding */ 1509 if ((flags & (LADJUST|ZEROPAD)) == 0) { 1510 n = width - realsz; 1511 while (n-- > 0) 1512 KPRINTF_PUTCHAR(' '); 1513 } 1514 1515 /* prefix */ 1516 if (sign) { 1517 KPRINTF_PUTCHAR(sign); 1518 } else if (flags & HEXPREFIX) { 1519 KPRINTF_PUTCHAR('0'); 1520 KPRINTF_PUTCHAR(ch); 1521 } 1522 1523 /* right-adjusting zero padding */ 1524 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) { 1525 n = width - realsz; 1526 while (n-- > 0) 1527 KPRINTF_PUTCHAR('0'); 1528 } 1529 1530 /* leading zeroes from decimal precision */ 1531 n = dprec - size; 1532 while (n-- > 0) 1533 KPRINTF_PUTCHAR('0'); 1534 1535 /* the string or number proper */ 1536 for (; size--; cp++) 1537 KPRINTF_PUTCHAR(*cp); 1538 /* left-adjusting padding (always blank) */ 1539 if (flags & LADJUST) { 1540 n = width - realsz; 1541 while (n-- > 0) 1542 KPRINTF_PUTCHAR(' '); 1543 } 1544 } 1545 1546 done: 1547 if ((oflags == TOBUFONLY) && (vp != NULL)) 1548 *(char **)vp = sbuf; 1549 (*v_flush)(); 1550 1551 #ifdef RND_PRINTF 1552 if (!cold) { 1553 struct timespec ts; 1554 (void)nanotime(&ts); 1555 SHA512_Update(&kprnd_sha, (char *)&ts, sizeof(ts)); 1556 } 1557 #endif 1558 return ret; 1559 } 1560