1 /* $NetBSD: subr_prf.c,v 1.154 2014/08/10 16:44:36 tls 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.154 2014/08/10 16:44:36 tls 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 } 165 mutex_exit(&kprintf_mtx); 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 struct timespec ts; 1204 1205 if (oflags == TOBUFONLY && (vp != NULL)) 1206 tailp = *(char **)vp; 1207 else 1208 tailp = NULL; 1209 1210 cp = NULL; /* XXX: shutup gcc */ 1211 size = 0; /* XXX: shutup gcc */ 1212 1213 fmt = fmt0; 1214 ret = 0; 1215 1216 xdigs = NULL; /* XXX: shut up gcc warning */ 1217 1218 /* 1219 * Scan the format for conversions (`%' character). 1220 */ 1221 for (;;) { 1222 for (; *fmt != '%' && *fmt; fmt++) { 1223 ret++; 1224 KPRINTF_PUTCHAR(*fmt); 1225 } 1226 if (*fmt == 0) 1227 goto done; 1228 1229 fmt++; /* skip over '%' */ 1230 1231 flags = 0; 1232 dprec = 0; 1233 width = 0; 1234 prec = -1; 1235 sign = '\0'; 1236 1237 rflag: ch = *fmt++; 1238 reswitch: switch (ch) { 1239 case ' ': 1240 /* 1241 * ``If the space and + flags both appear, the space 1242 * flag will be ignored.'' 1243 * -- ANSI X3J11 1244 */ 1245 if (!sign) 1246 sign = ' '; 1247 goto rflag; 1248 case '#': 1249 flags |= ALT; 1250 goto rflag; 1251 case '*': 1252 /* 1253 * ``A negative field width argument is taken as a 1254 * - flag followed by a positive field width.'' 1255 * -- ANSI X3J11 1256 * They don't exclude field widths read from args. 1257 */ 1258 if ((width = va_arg(ap, int)) >= 0) 1259 goto rflag; 1260 width = -width; 1261 /* FALLTHROUGH */ 1262 case '-': 1263 flags |= LADJUST; 1264 goto rflag; 1265 case '+': 1266 sign = '+'; 1267 goto rflag; 1268 case '.': 1269 if ((ch = *fmt++) == '*') { 1270 n = va_arg(ap, int); 1271 prec = n < 0 ? -1 : n; 1272 goto rflag; 1273 } 1274 n = 0; 1275 while (is_digit(ch)) { 1276 n = 10 * n + to_digit(ch); 1277 ch = *fmt++; 1278 } 1279 prec = n < 0 ? -1 : n; 1280 goto reswitch; 1281 case '0': 1282 /* 1283 * ``Note that 0 is taken as a flag, not as the 1284 * beginning of a field width.'' 1285 * -- ANSI X3J11 1286 */ 1287 flags |= ZEROPAD; 1288 goto rflag; 1289 case '1': case '2': case '3': case '4': 1290 case '5': case '6': case '7': case '8': case '9': 1291 n = 0; 1292 do { 1293 n = 10 * n + to_digit(ch); 1294 ch = *fmt++; 1295 } while (is_digit(ch)); 1296 width = n; 1297 goto reswitch; 1298 case 'h': 1299 flags |= SHORTINT; 1300 goto rflag; 1301 case 'j': 1302 flags |= MAXINT; 1303 goto rflag; 1304 case 'l': 1305 if (*fmt == 'l') { 1306 fmt++; 1307 flags |= QUADINT; 1308 } else { 1309 flags |= LONGINT; 1310 } 1311 goto rflag; 1312 case 'q': 1313 flags |= QUADINT; 1314 goto rflag; 1315 case 't': 1316 flags |= PTRINT; 1317 goto rflag; 1318 case 'z': 1319 flags |= SIZEINT; 1320 goto rflag; 1321 case 'c': 1322 *(cp = bf) = va_arg(ap, int); 1323 size = 1; 1324 sign = '\0'; 1325 break; 1326 case 'D': 1327 flags |= LONGINT; 1328 /*FALLTHROUGH*/ 1329 case 'd': 1330 case 'i': 1331 _uquad = SARG(); 1332 if ((quad_t)_uquad < 0) { 1333 _uquad = -_uquad; 1334 sign = '-'; 1335 } 1336 base = DEC; 1337 goto number; 1338 case 'n': 1339 if (flags & MAXINT) 1340 *va_arg(ap, intmax_t *) = ret; 1341 else if (flags & PTRINT) 1342 *va_arg(ap, intptr_t *) = ret; 1343 else if (flags & SIZEINT) 1344 *va_arg(ap, ssize_t *) = ret; 1345 else if (flags & QUADINT) 1346 *va_arg(ap, quad_t *) = ret; 1347 else if (flags & LONGINT) 1348 *va_arg(ap, long *) = ret; 1349 else if (flags & SHORTINT) 1350 *va_arg(ap, short *) = ret; 1351 else 1352 *va_arg(ap, int *) = ret; 1353 continue; /* no output */ 1354 case 'O': 1355 flags |= LONGINT; 1356 /*FALLTHROUGH*/ 1357 case 'o': 1358 _uquad = UARG(); 1359 base = OCT; 1360 goto nosign; 1361 case 'p': 1362 /* 1363 * ``The argument shall be a pointer to void. The 1364 * value of the pointer is converted to a sequence 1365 * of printable characters, in an implementation- 1366 * defined manner.'' 1367 * -- ANSI X3J11 1368 */ 1369 /* NOSTRICT */ 1370 _uquad = (u_long)va_arg(ap, void *); 1371 base = HEX; 1372 xdigs = hexdigits; 1373 flags |= HEXPREFIX; 1374 ch = 'x'; 1375 goto nosign; 1376 case 's': 1377 if ((cp = va_arg(ap, char *)) == NULL) 1378 /*XXXUNCONST*/ 1379 cp = __UNCONST("(null)"); 1380 if (prec >= 0) { 1381 /* 1382 * can't use strlen; can only look for the 1383 * NUL in the first `prec' characters, and 1384 * strlen() will go further. 1385 */ 1386 char *p = memchr(cp, 0, prec); 1387 1388 if (p != NULL) { 1389 size = p - cp; 1390 if (size > prec) 1391 size = prec; 1392 } else 1393 size = prec; 1394 } else 1395 size = strlen(cp); 1396 sign = '\0'; 1397 break; 1398 case 'U': 1399 flags |= LONGINT; 1400 /*FALLTHROUGH*/ 1401 case 'u': 1402 _uquad = UARG(); 1403 base = DEC; 1404 goto nosign; 1405 case 'X': 1406 xdigs = HEXDIGITS; 1407 goto hex; 1408 case 'x': 1409 xdigs = hexdigits; 1410 hex: _uquad = UARG(); 1411 base = HEX; 1412 /* leading 0x/X only if non-zero */ 1413 if (flags & ALT && _uquad != 0) 1414 flags |= HEXPREFIX; 1415 1416 /* unsigned conversions */ 1417 nosign: sign = '\0'; 1418 /* 1419 * ``... diouXx conversions ... if a precision is 1420 * specified, the 0 flag will be ignored.'' 1421 * -- ANSI X3J11 1422 */ 1423 number: if ((dprec = prec) >= 0) 1424 flags &= ~ZEROPAD; 1425 1426 /* 1427 * ``The result of converting a zero value with an 1428 * explicit precision of zero is no characters.'' 1429 * -- ANSI X3J11 1430 */ 1431 cp = bf + KPRINTF_BUFSIZE; 1432 if (_uquad != 0 || prec != 0) { 1433 /* 1434 * Unsigned mod is hard, and unsigned mod 1435 * by a constant is easier than that by 1436 * a variable; hence this switch. 1437 */ 1438 switch (base) { 1439 case OCT: 1440 do { 1441 *--cp = to_char(_uquad & 7); 1442 _uquad >>= 3; 1443 } while (_uquad); 1444 /* handle octal leading 0 */ 1445 if (flags & ALT && *cp != '0') 1446 *--cp = '0'; 1447 break; 1448 1449 case DEC: 1450 /* many numbers are 1 digit */ 1451 while (_uquad >= 10) { 1452 *--cp = to_char(_uquad % 10); 1453 _uquad /= 10; 1454 } 1455 *--cp = to_char(_uquad); 1456 break; 1457 1458 case HEX: 1459 do { 1460 *--cp = xdigs[_uquad & 15]; 1461 _uquad >>= 4; 1462 } while (_uquad); 1463 break; 1464 1465 default: 1466 /*XXXUNCONST*/ 1467 cp = __UNCONST("bug in kprintf: bad base"); 1468 size = strlen(cp); 1469 goto skipsize; 1470 } 1471 } 1472 size = bf + KPRINTF_BUFSIZE - cp; 1473 skipsize: 1474 break; 1475 default: /* "%?" prints ?, unless ? is NUL */ 1476 if (ch == '\0') 1477 goto done; 1478 /* pretend it was %c with argument ch */ 1479 cp = bf; 1480 *cp = ch; 1481 size = 1; 1482 sign = '\0'; 1483 break; 1484 } 1485 1486 /* 1487 * All reasonable formats wind up here. At this point, `cp' 1488 * points to a string which (if not flags&LADJUST) should be 1489 * padded out to `width' places. If flags&ZEROPAD, it should 1490 * first be prefixed by any sign or other prefix; otherwise, 1491 * it should be blank padded before the prefix is emitted. 1492 * After any left-hand padding and prefixing, emit zeroes 1493 * required by a decimal [diouxX] precision, then print the 1494 * string proper, then emit zeroes required by any leftover 1495 * floating precision; finally, if LADJUST, pad with blanks. 1496 * 1497 * Compute actual size, so we know how much to pad. 1498 * size excludes decimal prec; realsz includes it. 1499 */ 1500 realsz = dprec > size ? dprec : size; 1501 if (sign) 1502 realsz++; 1503 else if (flags & HEXPREFIX) 1504 realsz+= 2; 1505 1506 /* adjust ret */ 1507 ret += width > realsz ? width : realsz; 1508 1509 /* right-adjusting blank padding */ 1510 if ((flags & (LADJUST|ZEROPAD)) == 0) { 1511 n = width - realsz; 1512 while (n-- > 0) 1513 KPRINTF_PUTCHAR(' '); 1514 } 1515 1516 /* prefix */ 1517 if (sign) { 1518 KPRINTF_PUTCHAR(sign); 1519 } else if (flags & HEXPREFIX) { 1520 KPRINTF_PUTCHAR('0'); 1521 KPRINTF_PUTCHAR(ch); 1522 } 1523 1524 /* right-adjusting zero padding */ 1525 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) { 1526 n = width - realsz; 1527 while (n-- > 0) 1528 KPRINTF_PUTCHAR('0'); 1529 } 1530 1531 /* leading zeroes from decimal precision */ 1532 n = dprec - size; 1533 while (n-- > 0) 1534 KPRINTF_PUTCHAR('0'); 1535 1536 /* the string or number proper */ 1537 for (; size--; cp++) 1538 KPRINTF_PUTCHAR(*cp); 1539 /* left-adjusting padding (always blank) */ 1540 if (flags & LADJUST) { 1541 n = width - realsz; 1542 while (n-- > 0) 1543 KPRINTF_PUTCHAR(' '); 1544 } 1545 } 1546 1547 done: 1548 if ((oflags == TOBUFONLY) && (vp != NULL)) 1549 *(char **)vp = sbuf; 1550 (*v_flush)(); 1551 1552 (void)nanotime(&ts); 1553 #ifdef RND_PRINTF 1554 SHA512_Update(&kprnd_sha, (char *)&ts, sizeof(ts)); 1555 #endif 1556 return ret; 1557 } 1558