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