1 /*- 2 * Copyright (c) 1986, 1988, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94 35 * $FreeBSD: src/sys/kern/subr_prf.c,v 1.61.2.5 2002/08/31 18:22:08 dwmalone Exp $ 36 */ 37 38 #include "opt_ddb.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/msgbuf.h> 44 #include <sys/malloc.h> 45 #include <sys/proc.h> 46 #include <sys/priv.h> 47 #include <sys/tty.h> 48 #include <sys/tprintf.h> 49 #include <sys/stdint.h> 50 #include <sys/syslog.h> 51 #include <sys/cons.h> 52 #include <sys/uio.h> 53 #include <sys/sysctl.h> 54 #include <sys/lock.h> 55 #include <sys/ctype.h> 56 #include <sys/eventhandler.h> 57 #include <sys/kthread.h> 58 #include <sys/cpu_topology.h> 59 60 #include <sys/thread2.h> 61 #include <sys/spinlock2.h> 62 63 #ifdef DDB 64 #include <ddb/ddb.h> 65 #endif 66 67 /* 68 * Note that stdarg.h and the ANSI style va_start macro is used for both 69 * ANSI and traditional C compilers. We use the __ machine version to stay 70 * within the kernel header file set. 71 */ 72 #include <machine/stdarg.h> 73 74 #define TOCONS 0x01 75 #define TOTTY 0x02 76 #define TOLOG 0x04 77 #define TOWAKEUP 0x08 78 #define TONOSPIN 0x10 /* avoid serialization */ 79 80 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */ 81 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1) 82 83 struct putchar_arg { 84 int flags; 85 int pri; 86 struct tty *tty; 87 }; 88 89 struct snprintf_arg { 90 char *str; 91 size_t remain; 92 }; 93 94 extern int log_open; 95 96 struct tty *constty; /* pointer to console "window" tty */ 97 98 static void msglogchar(int c, int pri); 99 static void msgaddchar(int c, void *dummy); 100 static void kputchar (int ch, void *arg); 101 static char *ksprintn (char *nbuf, uintmax_t num, int base, int *lenp, 102 int upper); 103 static void snprintf_func (int ch, void *arg); 104 105 static int consintr = 1; /* Ok to handle console interrupts? */ 106 static int msgbufmapped; /* Set when safe to use msgbuf */ 107 static struct spinlock cons_spin = SPINLOCK_INITIALIZER(cons_spin, "cons_spin"); 108 static thread_t constty_td = NULL; 109 110 int msgbuftrigger; 111 112 static int log_console_output = 1; 113 TUNABLE_INT("kern.log_console_output", &log_console_output); 114 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW, 115 &log_console_output, 0, ""); 116 static int kprintf_logging = TOLOG | TOCONS; 117 SYSCTL_INT(_kern, OID_AUTO, kprintf_logging, CTLFLAG_RW, 118 &kprintf_logging, 0, ""); 119 120 static int unprivileged_read_msgbuf = 1; 121 SYSCTL_INT(_security, OID_AUTO, unprivileged_read_msgbuf, CTLFLAG_RW, 122 &unprivileged_read_msgbuf, 0, 123 "Unprivileged processes may read the kernel message buffer"); 124 125 /* 126 * Warn that a system table is full. 127 */ 128 void 129 tablefull(const char *tab) 130 { 131 132 log(LOG_ERR, "%s: table is full\n", tab); 133 } 134 135 /* 136 * Uprintf prints to the controlling terminal for the current process. 137 */ 138 int 139 uprintf(const char *fmt, ...) 140 { 141 struct proc *p = curproc; 142 __va_list ap; 143 struct putchar_arg pca; 144 int retval = 0; 145 146 if (p && (p->p_flags & P_CONTROLT) && p->p_session->s_ttyvp) { 147 __va_start(ap, fmt); 148 pca.tty = p->p_session->s_ttyp; 149 pca.flags = TOTTY; 150 151 retval = kvcprintf(fmt, kputchar, &pca, 10, ap); 152 __va_end(ap); 153 } 154 return (retval); 155 } 156 157 tpr_t 158 tprintf_open(struct proc *p) 159 { 160 if ((p->p_flags & P_CONTROLT) && p->p_session->s_ttyvp) { 161 sess_hold(p->p_session); 162 return ((tpr_t) p->p_session); 163 } 164 return (NULL); 165 } 166 167 void 168 tprintf_close(tpr_t sess) 169 { 170 if (sess) 171 sess_rele((struct session *) sess); 172 } 173 174 /* 175 * tprintf prints on the controlling terminal associated 176 * with the given session. 177 */ 178 int 179 tprintf(tpr_t tpr, const char *fmt, ...) 180 { 181 struct session *sess = (struct session *)tpr; 182 struct tty *tp = NULL; 183 int flags = TOLOG; 184 __va_list ap; 185 struct putchar_arg pca; 186 int retval; 187 188 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) { 189 flags |= TOTTY; 190 tp = sess->s_ttyp; 191 } 192 __va_start(ap, fmt); 193 pca.tty = tp; 194 pca.flags = flags; 195 pca.pri = LOG_INFO; 196 retval = kvcprintf(fmt, kputchar, &pca, 10, ap); 197 __va_end(ap); 198 msgbuftrigger = 1; 199 return (retval); 200 } 201 202 /* 203 * Ttyprintf displays a message on a tty; it should be used only by 204 * the tty driver, or anything that knows the underlying tty will not 205 * be revoke(2)'d away. Other callers should use tprintf. 206 */ 207 int 208 ttyprintf(struct tty *tp, const char *fmt, ...) 209 { 210 __va_list ap; 211 struct putchar_arg pca; 212 int retval; 213 214 __va_start(ap, fmt); 215 pca.tty = tp; 216 pca.flags = TOTTY; 217 retval = kvcprintf(fmt, kputchar, &pca, 10, ap); 218 __va_end(ap); 219 return (retval); 220 } 221 222 /* 223 * Log writes to the log buffer, and guarantees not to sleep (so can be 224 * called by interrupt routines). If there is no process reading the 225 * log yet, it writes to the console also. 226 */ 227 int 228 log(int level, const char *fmt, ...) 229 { 230 __va_list ap; 231 int retval; 232 struct putchar_arg pca; 233 234 pca.tty = NULL; 235 pca.pri = level; 236 if ((kprintf_logging & TOCONS) == 0 || log_open) 237 pca.flags = TOLOG; 238 else 239 pca.flags = TOCONS; 240 241 __va_start(ap, fmt); 242 retval = kvcprintf(fmt, kputchar, &pca, 10, ap); 243 __va_end(ap); 244 245 msgbuftrigger = 1; 246 return (retval); 247 } 248 249 #define CONSCHUNK 128 250 251 void 252 log_console(struct uio *uio) 253 { 254 int c, i, error, iovlen, nl; 255 struct uio muio; 256 struct iovec *miov = NULL; 257 char *consbuffer; 258 int pri; 259 260 if (!log_console_output) 261 return; 262 263 pri = LOG_INFO | LOG_CONSOLE; 264 muio = *uio; 265 iovlen = uio->uio_iovcnt * sizeof (struct iovec); 266 miov = kmalloc(iovlen, M_TEMP, M_WAITOK); 267 consbuffer = kmalloc(CONSCHUNK, M_TEMP, M_WAITOK); 268 bcopy((caddr_t)muio.uio_iov, (caddr_t)miov, iovlen); 269 muio.uio_iov = miov; 270 uio = &muio; 271 272 nl = 0; 273 while (uio->uio_resid > 0) { 274 c = (int)szmin(uio->uio_resid, CONSCHUNK); 275 error = uiomove(consbuffer, (size_t)c, uio); 276 if (error != 0) 277 break; 278 for (i = 0; i < c; i++) { 279 msglogchar(consbuffer[i], pri); 280 if (consbuffer[i] == '\n') 281 nl = 1; 282 else 283 nl = 0; 284 } 285 } 286 if (!nl) 287 msglogchar('\n', pri); 288 msgbuftrigger = 1; 289 kfree(miov, M_TEMP); 290 kfree(consbuffer, M_TEMP); 291 return; 292 } 293 294 /* 295 * Output to the console. 296 */ 297 int 298 kprintf(const char *fmt, ...) 299 { 300 __va_list ap; 301 int savintr; 302 struct putchar_arg pca; 303 int retval; 304 305 savintr = consintr; /* disable interrupts */ 306 consintr = 0; 307 __va_start(ap, fmt); 308 pca.tty = NULL; 309 pca.flags = kprintf_logging & ~TOTTY; 310 pca.pri = -1; 311 retval = kvcprintf(fmt, kputchar, &pca, 10, ap); 312 __va_end(ap); 313 if (!panicstr) 314 msgbuftrigger = 1; 315 consintr = savintr; /* reenable interrupts */ 316 return (retval); 317 } 318 319 int 320 kvprintf(const char *fmt, __va_list ap) 321 { 322 int savintr; 323 struct putchar_arg pca; 324 int retval; 325 326 savintr = consintr; /* disable interrupts */ 327 consintr = 0; 328 pca.tty = NULL; 329 pca.flags = kprintf_logging & ~TOTTY; 330 pca.pri = -1; 331 retval = kvcprintf(fmt, kputchar, &pca, 10, ap); 332 if (!panicstr) 333 msgbuftrigger = 1; 334 consintr = savintr; /* reenable interrupts */ 335 return (retval); 336 } 337 338 /* 339 * Limited rate kprintf. The passed rate structure must be initialized 340 * with the desired reporting frequency. A frequency of 0 will result in 341 * no output. 342 * 343 * count may be initialized to a negative number to allow an initial 344 * burst. 345 */ 346 void 347 krateprintf(struct krate *rate, const char *fmt, ...) 348 { 349 __va_list ap; 350 351 if (rate->ticks != (int)time_uptime) { 352 rate->ticks = (int)time_uptime; 353 if (rate->count > 0) 354 rate->count = 0; 355 } 356 if (rate->count < rate->freq) { 357 ++rate->count; 358 __va_start(ap, fmt); 359 kvprintf(fmt, ap); 360 __va_end(ap); 361 } 362 } 363 364 /* 365 * Print a character to the dmesg log, the console, and/or the user's 366 * terminal. 367 * 368 * NOTE: TOTTY does not require nonblocking operation, but TOCONS 369 * and TOLOG do. When we have a constty we still output to 370 * the real console but we have a monitoring thread which 371 * we wakeup which tracks the log. 372 */ 373 static void 374 kputchar(int c, void *arg) 375 { 376 struct putchar_arg *ap = (struct putchar_arg*) arg; 377 int flags = ap->flags; 378 struct tty *tp = ap->tty; 379 380 if (panicstr) 381 constty = NULL; 382 if ((flags & TOCONS) && tp == NULL && constty) 383 flags |= TOLOG | TOWAKEUP; 384 if ((flags & TOTTY) && tputchar(c, tp) < 0) 385 ap->flags &= ~TOTTY; 386 if ((flags & TOLOG)) 387 msglogchar(c, ap->pri); 388 if ((flags & TOCONS) && c) 389 cnputc(c); 390 if (flags & TOWAKEUP) 391 wakeup(constty_td); 392 } 393 394 /* 395 * Scaled down version of sprintf(3). 396 */ 397 int 398 ksprintf(char *buf, const char *cfmt, ...) 399 { 400 int retval; 401 __va_list ap; 402 403 __va_start(ap, cfmt); 404 retval = kvcprintf(cfmt, NULL, buf, 10, ap); 405 buf[retval] = '\0'; 406 __va_end(ap); 407 return (retval); 408 } 409 410 /* 411 * Scaled down version of vsprintf(3). 412 */ 413 int 414 kvsprintf(char *buf, const char *cfmt, __va_list ap) 415 { 416 int retval; 417 418 retval = kvcprintf(cfmt, NULL, buf, 10, ap); 419 buf[retval] = '\0'; 420 return (retval); 421 } 422 423 /* 424 * Scaled down version of snprintf(3). 425 */ 426 int 427 ksnprintf(char *str, size_t size, const char *format, ...) 428 { 429 int retval; 430 __va_list ap; 431 432 __va_start(ap, format); 433 retval = kvsnprintf(str, size, format, ap); 434 __va_end(ap); 435 return(retval); 436 } 437 438 /* 439 * Scaled down version of vsnprintf(3). 440 */ 441 int 442 kvsnprintf(char *str, size_t size, const char *format, __va_list ap) 443 { 444 struct snprintf_arg info; 445 int retval; 446 447 info.str = str; 448 info.remain = size; 449 retval = kvcprintf(format, snprintf_func, &info, 10, ap); 450 if (info.remain >= 1) 451 *info.str++ = '\0'; 452 return (retval); 453 } 454 455 int 456 ksnrprintf(char *str, size_t size, int radix, const char *format, ...) 457 { 458 int retval; 459 __va_list ap; 460 461 __va_start(ap, format); 462 retval = kvsnrprintf(str, size, radix, format, ap); 463 __va_end(ap); 464 return(retval); 465 } 466 467 int 468 kvsnrprintf(char *str, size_t size, int radix, const char *format, __va_list ap) 469 { 470 struct snprintf_arg info; 471 int retval; 472 473 info.str = str; 474 info.remain = size; 475 retval = kvcprintf(format, snprintf_func, &info, radix, ap); 476 if (info.remain >= 1) 477 *info.str++ = '\0'; 478 return (retval); 479 } 480 481 int 482 kvasnrprintf(char **strp, size_t size, int radix, 483 const char *format, __va_list ap) 484 { 485 struct snprintf_arg info; 486 int retval; 487 488 *strp = kmalloc(size, M_TEMP, M_WAITOK); 489 info.str = *strp; 490 info.remain = size; 491 retval = kvcprintf(format, snprintf_func, &info, radix, ap); 492 if (info.remain >= 1) 493 *info.str++ = '\0'; 494 return (retval); 495 } 496 497 void 498 kvasfree(char **strp) 499 { 500 if (*strp) { 501 kfree(*strp, M_TEMP); 502 *strp = NULL; 503 } 504 } 505 506 static void 507 snprintf_func(int ch, void *arg) 508 { 509 struct snprintf_arg *const info = arg; 510 511 if (info->remain >= 2) { 512 *info->str++ = ch; 513 info->remain--; 514 } 515 } 516 517 /* 518 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse 519 * order; return an optional length and a pointer to the last character 520 * written in the buffer (i.e., the first character of the string). 521 * The buffer pointed to by `nbuf' must have length >= MAXNBUF. 522 */ 523 static char * 524 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper) 525 { 526 char *p, c; 527 528 p = nbuf; 529 *p = '\0'; 530 do { 531 c = hex2ascii(num % base); 532 *++p = upper ? toupper(c) : c; 533 } while (num /= base); 534 if (lenp) 535 *lenp = p - nbuf; 536 return (p); 537 } 538 539 /* 540 * Scaled down version of printf(3). 541 * 542 * Two additional formats: 543 * 544 * The format %b is supported to decode error registers. 545 * Its usage is: 546 * 547 * kprintf("reg=%b\n", regval, "<base><arg>*"); 548 * 549 * where <base> is the output base expressed as a control character, e.g. 550 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 551 * the first of which gives the bit number to be inspected (origin 1), and 552 * the next characters (up to a control character, i.e. a character <= 32), 553 * give the name of the register. Thus: 554 * 555 * kvcprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 556 * 557 * would produce output: 558 * 559 * reg=3<BITTWO,BITONE> 560 */ 561 562 #define PCHAR(c) {int cc=(c); if(func) (*func)(cc,arg); else *d++=cc; retval++;} 563 564 int 565 kvcprintf(char const *fmt, void (*func)(int, void*), void *arg, 566 int radix, __va_list ap) 567 { 568 char nbuf[MAXNBUF]; 569 char *d; 570 const char *p, *percent, *q; 571 int ch, n; 572 uintmax_t num; 573 int base, tmp, width, ladjust, sharpflag, spaceflag, neg, sign, dot; 574 int cflag, hflag, jflag, lflag, qflag, tflag, zflag; 575 int dwidth, upper; 576 char padc; 577 int retval = 0, stop = 0; 578 int usespin; 579 580 /* 581 * Make a supreme effort to avoid reentrant panics or deadlocks. 582 * 583 * NOTE! Do nothing that would access mycpu/gd/fs unless the 584 * function is the normal kputchar(), which allows us to 585 * use this function for very early debugging with a special 586 * function. 587 */ 588 if (func == kputchar) { 589 if (mycpu->gd_flags & GDF_KPRINTF) 590 return(0); 591 atomic_set_long(&mycpu->gd_flags, GDF_KPRINTF); 592 } 593 594 num = 0; 595 if (!func) 596 d = (char *) arg; 597 else 598 d = NULL; 599 600 if (fmt == NULL) 601 fmt = "(fmt null)\n"; 602 603 if (radix < 2 || radix > 36) 604 radix = 10; 605 606 usespin = (func == kputchar && 607 (kprintf_logging & TONOSPIN) == 0 && 608 panic_cpu_gd != mycpu && 609 (((struct putchar_arg *)arg)->flags & TOTTY) == 0); 610 if (usespin) { 611 crit_enter_hard(); 612 spin_lock(&cons_spin); 613 } 614 615 for (;;) { 616 padc = ' '; 617 width = 0; 618 while ((ch = (u_char)*fmt++) != '%' || stop) { 619 if (ch == '\0') 620 goto done; 621 PCHAR(ch); 622 } 623 percent = fmt - 1; 624 dot = dwidth = ladjust = neg = sharpflag = sign = upper = 0; 625 spaceflag = 0; 626 cflag = hflag = jflag = lflag = qflag = tflag = zflag = 0; 627 628 reswitch: 629 switch (ch = (u_char)*fmt++) { 630 case ' ': 631 spaceflag = 1; 632 goto reswitch; 633 case '.': 634 dot = 1; 635 goto reswitch; 636 case '#': 637 sharpflag = 1; 638 goto reswitch; 639 case '+': 640 sign = 1; 641 goto reswitch; 642 case '-': 643 ladjust = 1; 644 goto reswitch; 645 case '%': 646 PCHAR(ch); 647 break; 648 case '*': 649 if (!dot) { 650 width = __va_arg(ap, int); 651 if (width < 0) { 652 ladjust = !ladjust; 653 width = -width; 654 } 655 } else { 656 dwidth = __va_arg(ap, int); 657 } 658 goto reswitch; 659 case '0': 660 if (!dot) { 661 padc = '0'; 662 goto reswitch; 663 } 664 case '1': case '2': case '3': case '4': 665 case '5': case '6': case '7': case '8': case '9': 666 for (n = 0;; ++fmt) { 667 n = n * 10 + ch - '0'; 668 ch = *fmt; 669 if (ch < '0' || ch > '9') 670 break; 671 } 672 if (dot) 673 dwidth = n; 674 else 675 width = n; 676 goto reswitch; 677 case 'b': 678 num = (u_int)__va_arg(ap, int); 679 p = __va_arg(ap, char *); 680 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;) 681 PCHAR(*q--); 682 683 if (num == 0) 684 break; 685 686 for (tmp = 0; *p;) { 687 n = *p++; 688 if (num & (1 << (n - 1))) { 689 PCHAR(tmp ? ',' : '<'); 690 for (; (n = *p) > ' '; ++p) 691 PCHAR(n); 692 tmp = 1; 693 } else 694 for (; *p > ' '; ++p) 695 continue; 696 } 697 if (tmp) 698 PCHAR('>'); 699 break; 700 case 'c': 701 PCHAR(__va_arg(ap, int)); 702 break; 703 case 'd': 704 case 'i': 705 base = 10; 706 sign = 1; 707 goto handle_sign; 708 case 'h': 709 if (hflag) { 710 hflag = 0; 711 cflag = 1; 712 } else 713 hflag = 1; 714 goto reswitch; 715 case 'j': 716 jflag = 1; 717 goto reswitch; 718 case 'l': 719 if (lflag) { 720 lflag = 0; 721 qflag = 1; 722 } else 723 lflag = 1; 724 goto reswitch; 725 case 'n': 726 if (cflag) 727 *(__va_arg(ap, char *)) = retval; 728 else if (hflag) 729 *(__va_arg(ap, short *)) = retval; 730 else if (jflag) 731 *(__va_arg(ap, intmax_t *)) = retval; 732 else if (lflag) 733 *(__va_arg(ap, long *)) = retval; 734 else if (qflag) 735 *(__va_arg(ap, quad_t *)) = retval; 736 else 737 *(__va_arg(ap, int *)) = retval; 738 break; 739 case 'o': 740 base = 8; 741 goto handle_nosign; 742 case 'p': 743 base = 16; 744 sharpflag = (width == 0); 745 sign = 0; 746 num = (uintptr_t)__va_arg(ap, void *); 747 goto number; 748 case 'q': 749 qflag = 1; 750 goto reswitch; 751 case 'r': 752 base = radix; 753 if (sign) 754 goto handle_sign; 755 goto handle_nosign; 756 case 's': 757 p = __va_arg(ap, char *); 758 if (p == NULL) 759 p = "(null)"; 760 if (!dot) 761 n = strlen (p); 762 else 763 for (n = 0; n < dwidth && p[n]; n++) 764 continue; 765 766 width -= n; 767 768 if (!ladjust && width > 0) 769 while (width--) 770 PCHAR(padc); 771 while (n--) 772 PCHAR(*p++); 773 if (ladjust && width > 0) 774 while (width--) 775 PCHAR(padc); 776 break; 777 case 't': 778 tflag = 1; 779 goto reswitch; 780 case 'u': 781 base = 10; 782 goto handle_nosign; 783 case 'X': 784 upper = 1; 785 /* FALLTHROUGH */ 786 case 'x': 787 base = 16; 788 goto handle_nosign; 789 case 'z': 790 zflag = 1; 791 goto reswitch; 792 handle_nosign: 793 sign = 0; 794 if (cflag) 795 num = (u_char)__va_arg(ap, int); 796 else if (hflag) 797 num = (u_short)__va_arg(ap, int); 798 else if (jflag) 799 num = __va_arg(ap, uintmax_t); 800 else if (lflag) 801 num = __va_arg(ap, u_long); 802 else if (qflag) 803 num = __va_arg(ap, u_quad_t); 804 else if (tflag) 805 num = __va_arg(ap, ptrdiff_t); 806 else if (zflag) 807 num = __va_arg(ap, size_t); 808 else 809 num = __va_arg(ap, u_int); 810 goto number; 811 handle_sign: 812 if (cflag) 813 num = (char)__va_arg(ap, int); 814 else if (hflag) 815 num = (short)__va_arg(ap, int); 816 else if (jflag) 817 num = __va_arg(ap, intmax_t); 818 else if (lflag) 819 num = __va_arg(ap, long); 820 else if (qflag) 821 num = __va_arg(ap, quad_t); 822 else if (tflag) 823 num = __va_arg(ap, ptrdiff_t); 824 else if (zflag) 825 num = __va_arg(ap, ssize_t); 826 else 827 num = __va_arg(ap, int); 828 number: 829 if (sign && (intmax_t)num < 0) { 830 neg = 1; 831 num = -(intmax_t)num; 832 } 833 p = ksprintn(nbuf, num, base, &n, upper); 834 tmp = 0; 835 if (sharpflag && num != 0) { 836 if (base == 8) 837 tmp++; 838 else if (base == 16) 839 tmp += 2; 840 } 841 if (neg || (sign && spaceflag)) 842 tmp++; 843 844 if (!ladjust && padc == '0') 845 dwidth = width - tmp; 846 width -= tmp + imax(dwidth, n); 847 dwidth -= n; 848 if (!ladjust) 849 while (width-- > 0) 850 PCHAR(' '); 851 if (neg) { 852 PCHAR('-'); 853 } else if (sign && spaceflag) { 854 PCHAR(' '); 855 } 856 if (sharpflag && num != 0) { 857 if (base == 8) { 858 PCHAR('0'); 859 } else if (base == 16) { 860 PCHAR('0'); 861 PCHAR('x'); 862 } 863 } 864 while (dwidth-- > 0) 865 PCHAR('0'); 866 867 while (*p) 868 PCHAR(*p--); 869 870 if (ladjust) 871 while (width-- > 0) 872 PCHAR(' '); 873 874 break; 875 default: 876 while (percent < fmt) 877 PCHAR(*percent++); 878 /* 879 * Since we ignore an formatting argument it is no 880 * longer safe to obey the remaining formatting 881 * arguments as the arguments will no longer match 882 * the format specs. 883 */ 884 stop = 1; 885 break; 886 } 887 } 888 done: 889 /* 890 * Cleanup reentrancy issues. 891 */ 892 if (func == kputchar) 893 atomic_clear_long(&mycpu->gd_flags, GDF_KPRINTF); 894 if (usespin) { 895 spin_unlock(&cons_spin); 896 crit_exit_hard(); 897 } 898 return (retval); 899 } 900 901 #undef PCHAR 902 903 /* 904 * Called from the panic code to try to get the console working 905 * again in case we paniced inside a kprintf(). 906 */ 907 void 908 kvcreinitspin(void) 909 { 910 spin_init(&cons_spin, "kvcre"); 911 atomic_clear_long(&mycpu->gd_flags, GDF_KPRINTF); 912 } 913 914 /* 915 * Console support thread for constty intercepts. This is needed because 916 * console tty intercepts can block. Instead of having kputchar() attempt 917 * to directly write to the console intercept we just force it to log 918 * and wakeup this baby to track and dump the log to constty. 919 */ 920 static void 921 constty_daemon(void) 922 { 923 u_int rindex; 924 u_int xindex; 925 u_int n; 926 struct msgbuf *mbp; 927 struct tty *tp; 928 929 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, 930 constty_td, SHUTDOWN_PRI_FIRST); 931 constty_td->td_flags |= TDF_SYSTHREAD; 932 933 mbp = msgbufp; 934 rindex = mbp->msg_bufr; /* persistent loop variable */ 935 xindex = mbp->msg_bufx - 1; /* anything different than bufx */ 936 cpu_ccfence(); 937 938 for (;;) { 939 kproc_suspend_loop(); 940 941 crit_enter(); 942 if (mbp != msgbufp) 943 mbp = msgbufp; 944 if (xindex == mbp->msg_bufx || 945 mbp == NULL || 946 msgbufmapped == 0) { 947 tsleep(constty_td, 0, "waiting", hz*60); 948 crit_exit(); 949 continue; 950 } 951 crit_exit(); 952 953 /* 954 * Get message buf FIFO indices. rindex is tracking. 955 */ 956 xindex = mbp->msg_bufx; 957 cpu_ccfence(); 958 if ((tp = constty) == NULL) { 959 rindex = xindex; 960 continue; 961 } 962 963 /* 964 * Check if the calculated bytes has rolled the whole 965 * message buffer. 966 */ 967 n = xindex - rindex; 968 if (n > mbp->msg_size - 1024) { 969 rindex = xindex - mbp->msg_size + 2048; 970 n = xindex - rindex; 971 } 972 973 /* 974 * And dump it. If constty gets stuck will give up. 975 */ 976 while (rindex != xindex) { 977 u_int ri = rindex % mbp->msg_size; 978 if (tputchar((uint8_t)mbp->msg_ptr[ri], tp) < 0) { 979 constty = NULL; 980 rindex = xindex; 981 break; 982 } 983 if (tp->t_outq.c_cc >= tp->t_ohiwat) { 984 tsleep(constty_daemon, 0, "blocked", hz / 10); 985 if (tp->t_outq.c_cc >= tp->t_ohiwat) { 986 rindex = xindex; 987 break; 988 } 989 } 990 ++rindex; 991 } 992 } 993 } 994 995 static struct kproc_desc constty_kp = { 996 "consttyd", 997 constty_daemon, 998 &constty_td 999 }; 1000 SYSINIT(bufdaemon, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, 1001 kproc_start, &constty_kp); 1002 1003 /* 1004 * Put character in log buffer with a particular priority. 1005 * 1006 * MPSAFE 1007 */ 1008 static void 1009 msglogchar(int c, int pri) 1010 { 1011 static int lastpri = -1; 1012 static int dangling; 1013 char nbuf[MAXNBUF]; 1014 char *p; 1015 1016 if (!msgbufmapped) 1017 return; 1018 if (c == '\0' || c == '\r') 1019 return; 1020 if (pri != -1 && pri != lastpri) { 1021 if (dangling) { 1022 msgaddchar('\n', NULL); 1023 dangling = 0; 1024 } 1025 msgaddchar('<', NULL); 1026 for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL, 0); *p;) 1027 msgaddchar(*p--, NULL); 1028 msgaddchar('>', NULL); 1029 lastpri = pri; 1030 } 1031 msgaddchar(c, NULL); 1032 if (c == '\n') { 1033 dangling = 0; 1034 lastpri = -1; 1035 } else { 1036 dangling = 1; 1037 } 1038 } 1039 1040 /* 1041 * Put char in log buffer. Make sure nothing blows up beyond repair if 1042 * we have an MP race. 1043 * 1044 * MPSAFE. 1045 */ 1046 static void 1047 msgaddchar(int c, void *dummy) 1048 { 1049 struct msgbuf *mbp; 1050 u_int lindex; 1051 u_int rindex; 1052 u_int xindex; 1053 u_int n; 1054 1055 if (!msgbufmapped) 1056 return; 1057 mbp = msgbufp; 1058 lindex = mbp->msg_bufl; 1059 rindex = mbp->msg_bufr; 1060 xindex = mbp->msg_bufx++; /* Allow SMP race */ 1061 cpu_ccfence(); 1062 1063 mbp->msg_ptr[xindex % mbp->msg_size] = c; 1064 n = xindex - lindex; 1065 if (n > mbp->msg_size - 1024) { 1066 lindex = xindex - mbp->msg_size + 2048; 1067 cpu_ccfence(); 1068 mbp->msg_bufl = lindex; 1069 } 1070 n = xindex - rindex; 1071 if (n > mbp->msg_size - 1024) { 1072 rindex = xindex - mbp->msg_size + 2048; 1073 cpu_ccfence(); 1074 mbp->msg_bufr = rindex; 1075 } 1076 } 1077 1078 static void 1079 msgbufcopy(struct msgbuf *oldp) 1080 { 1081 u_int rindex; 1082 u_int xindex; 1083 u_int n; 1084 1085 rindex = oldp->msg_bufr; 1086 xindex = oldp->msg_bufx; 1087 cpu_ccfence(); 1088 1089 n = xindex - rindex; 1090 if (n > oldp->msg_size - 1024) 1091 rindex = xindex - oldp->msg_size + 2048; 1092 while (rindex != xindex) { 1093 msglogchar(oldp->msg_ptr[rindex % oldp->msg_size], -1); 1094 ++rindex; 1095 } 1096 } 1097 1098 void 1099 msgbufinit(void *ptr, size_t size) 1100 { 1101 char *cp; 1102 static struct msgbuf *oldp = NULL; 1103 1104 size -= sizeof(*msgbufp); 1105 cp = (char *)ptr; 1106 msgbufp = (struct msgbuf *) (cp + size); 1107 if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size) { 1108 bzero(cp, size); 1109 bzero(msgbufp, sizeof(*msgbufp)); 1110 msgbufp->msg_magic = MSG_MAGIC; 1111 msgbufp->msg_size = (char *)msgbufp - cp; 1112 } 1113 msgbufp->msg_ptr = cp; 1114 if (msgbufmapped && oldp != msgbufp) 1115 msgbufcopy(oldp); 1116 cpu_mfence(); 1117 msgbufmapped = 1; 1118 oldp = msgbufp; 1119 } 1120 1121 /* Sysctls for accessing/clearing the msgbuf */ 1122 1123 static int 1124 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS) 1125 { 1126 struct msgbuf *mbp; 1127 struct ucred *cred; 1128 int error; 1129 u_int rindex_modulo; 1130 u_int xindex_modulo; 1131 u_int rindex; 1132 u_int xindex; 1133 u_int n; 1134 1135 /* 1136 * Only wheel or root can access the message log. 1137 */ 1138 if (unprivileged_read_msgbuf == 0) { 1139 KKASSERT(req->td->td_proc); 1140 cred = req->td->td_proc->p_ucred; 1141 1142 if ((cred->cr_prison || groupmember(0, cred) == 0) && 1143 priv_check(req->td, PRIV_ROOT) != 0 1144 ) { 1145 return (EPERM); 1146 } 1147 } 1148 1149 /* 1150 * Unwind the buffer, so that it's linear (possibly starting with 1151 * some initial nulls). 1152 * 1153 * We don't push the entire buffer like we did before because 1154 * bufr (and bufl) now advance in chunks when the fifo is full, 1155 * rather than one character. 1156 */ 1157 mbp = msgbufp; 1158 rindex = mbp->msg_bufr; 1159 xindex = mbp->msg_bufx; 1160 n = xindex - rindex; 1161 if (n > mbp->msg_size - 1024) { 1162 rindex = xindex - mbp->msg_size + 2048; 1163 n = xindex - rindex; 1164 } 1165 rindex_modulo = rindex % mbp->msg_size; 1166 xindex_modulo = xindex % mbp->msg_size; 1167 1168 if (rindex_modulo < xindex_modulo) { 1169 /* 1170 * Can handle in one linear section. 1171 */ 1172 error = sysctl_handle_opaque(oidp, 1173 mbp->msg_ptr + rindex_modulo, 1174 xindex_modulo - rindex_modulo, 1175 req); 1176 } else if (rindex_modulo == xindex_modulo) { 1177 /* 1178 * Empty buffer, just return a single newline 1179 */ 1180 error = sysctl_handle_opaque(oidp, "\n", 1, req); 1181 } else if (n <= mbp->msg_size - rindex_modulo) { 1182 /* 1183 * Can handle in one linear section. 1184 */ 1185 error = sysctl_handle_opaque(oidp, 1186 mbp->msg_ptr + rindex_modulo, 1187 n - rindex_modulo, 1188 req); 1189 } else { 1190 /* 1191 * Glue together two linear sections into one contiguous 1192 * output. 1193 */ 1194 error = sysctl_handle_opaque(oidp, 1195 mbp->msg_ptr + rindex_modulo, 1196 mbp->msg_size - rindex_modulo, 1197 req); 1198 n -= mbp->msg_size - rindex_modulo; 1199 if (error == 0) 1200 error = sysctl_handle_opaque(oidp, mbp->msg_ptr, 1201 n, req); 1202 } 1203 if (error) 1204 return (error); 1205 return (error); 1206 } 1207 1208 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD, 1209 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer"); 1210 1211 static int msgbuf_clear; 1212 1213 static int 1214 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS) 1215 { 1216 int error; 1217 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); 1218 if (!error && req->newptr) { 1219 /* Clear the buffer and reset write pointer */ 1220 msgbufp->msg_bufr = msgbufp->msg_bufx; 1221 msgbufp->msg_bufl = msgbufp->msg_bufx; 1222 bzero(msgbufp->msg_ptr, msgbufp->msg_size); 1223 msgbuf_clear = 0; 1224 } 1225 return (error); 1226 } 1227 1228 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear, 1229 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0, 1230 sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer"); 1231 1232 #ifdef DDB 1233 1234 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf) 1235 { 1236 u_int rindex; 1237 u_int i; 1238 u_int j; 1239 1240 if (!msgbufmapped) { 1241 db_printf("msgbuf not mapped yet\n"); 1242 return; 1243 } 1244 db_printf("msgbufp = %p\n", msgbufp); 1245 db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n", 1246 msgbufp->msg_magic, msgbufp->msg_size, 1247 msgbufp->msg_bufr % msgbufp->msg_size, 1248 msgbufp->msg_bufx % msgbufp->msg_size, 1249 msgbufp->msg_ptr); 1250 1251 rindex = msgbufp->msg_bufr; 1252 for (i = 0; i < msgbufp->msg_size; i++) { 1253 j = (i + rindex) % msgbufp->msg_size; 1254 db_printf("%c", msgbufp->msg_ptr[j]); 1255 } 1256 db_printf("\n"); 1257 } 1258 1259 #endif /* DDB */ 1260 1261 1262 void 1263 hexdump(const void *ptr, int length, const char *hdr, int flags) 1264 { 1265 int i, j, k; 1266 int cols; 1267 const unsigned char *cp; 1268 char delim; 1269 1270 if ((flags & HD_DELIM_MASK) != 0) 1271 delim = (flags & HD_DELIM_MASK) >> 8; 1272 else 1273 delim = ' '; 1274 1275 if ((flags & HD_COLUMN_MASK) != 0) 1276 cols = flags & HD_COLUMN_MASK; 1277 else 1278 cols = 16; 1279 1280 cp = ptr; 1281 for (i = 0; i < length; i+= cols) { 1282 if (hdr != NULL) 1283 kprintf("%s", hdr); 1284 1285 if ((flags & HD_OMIT_COUNT) == 0) 1286 kprintf("%04x ", i); 1287 1288 if ((flags & HD_OMIT_HEX) == 0) { 1289 for (j = 0; j < cols; j++) { 1290 k = i + j; 1291 if (k < length) 1292 kprintf("%c%02x", delim, cp[k]); 1293 else 1294 kprintf(" "); 1295 } 1296 } 1297 1298 if ((flags & HD_OMIT_CHARS) == 0) { 1299 kprintf(" |"); 1300 for (j = 0; j < cols; j++) { 1301 k = i + j; 1302 if (k >= length) 1303 kprintf(" "); 1304 else if (cp[k] >= ' ' && cp[k] <= '~') 1305 kprintf("%c", cp[k]); 1306 else 1307 kprintf("."); 1308 } 1309 kprintf("|"); 1310 } 1311 kprintf("\n"); 1312 } 1313 } 1314 1315 void 1316 kprint_cpuset(cpumask_t *mask) 1317 { 1318 int i; 1319 int b = -1; 1320 int e = -1; 1321 int more = 0; 1322 1323 kprintf("cpus("); 1324 CPUSET_FOREACH(i, *mask) { 1325 if (b < 0) { 1326 b = i; 1327 e = b + 1; 1328 continue; 1329 } 1330 if (e == i) { 1331 ++e; 1332 continue; 1333 } 1334 if (more) 1335 kprintf(", "); 1336 if (b == e - 1) { 1337 kprintf("%d", b); 1338 } else { 1339 kprintf("%d-%d", b, e - 1); 1340 } 1341 more = 1; 1342 b = i; 1343 e = b + 1; 1344 } 1345 if (more) 1346 kprintf(", "); 1347 if (b >= 0) { 1348 if (b == e - 1) { 1349 kprintf("%d", b); 1350 } else { 1351 kprintf("%d-%d", b, e - 1); 1352 } 1353 } 1354 kprintf(") "); 1355 } 1356