kernel: Remove the FFS_ROOT option. It was a no-op since 4.9.
[dragonfly.git] / sys / kern / subr_prf.c
blob7fd941065dc685bf98e330a3460760b411ff6b95
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.
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.
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.
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 $
38 #include "opt_ddb.h"
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>
60 #include <sys/thread2.h>
61 #include <sys/spinlock2.h>
63 #ifdef DDB
64 #include <ddb/ddb.h>
65 #endif
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.
72 #include <machine/stdarg.h>
74 #define TOCONS 0x01
75 #define TOTTY 0x02
76 #define TOLOG 0x04
77 #define TOWAKEUP 0x08
78 #define TONOSPIN 0x10 /* avoid serialization */
80 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
81 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
83 struct putchar_arg {
84 int flags;
85 int pri;
86 struct tty *tty;
89 struct snprintf_arg {
90 char *str;
91 size_t remain;
94 extern int log_open;
96 struct tty *constty; /* pointer to console "window" tty */
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);
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;
110 int msgbuftrigger;
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, "");
120 static int ptr_restrict = 0;
121 TUNABLE_INT("security.ptr_restrict", &ptr_restrict);
122 SYSCTL_INT(_security, OID_AUTO, ptr_restrict, CTLFLAG_RW, &ptr_restrict, 0,
123 "Prevent leaking the kernel pointers back to userland");
125 static int unprivileged_read_msgbuf = 1;
126 SYSCTL_INT(_security, OID_AUTO, unprivileged_read_msgbuf, CTLFLAG_RW,
127 &unprivileged_read_msgbuf, 0,
128 "Unprivileged processes may read the kernel message buffer");
131 * Warn that a system table is full.
133 void
134 tablefull(const char *tab)
137 log(LOG_ERR, "%s: table is full\n", tab);
141 * Uprintf prints to the controlling terminal for the current process.
144 uprintf(const char *fmt, ...)
146 struct proc *p = curproc;
147 __va_list ap;
148 struct putchar_arg pca;
149 int retval = 0;
151 if (p && (p->p_flags & P_CONTROLT) && p->p_session->s_ttyvp) {
152 __va_start(ap, fmt);
153 pca.tty = p->p_session->s_ttyp;
154 pca.flags = TOTTY;
156 retval = kvcprintf(fmt, kputchar, &pca, ap);
157 __va_end(ap);
159 return (retval);
162 tpr_t
163 tprintf_open(struct proc *p)
165 if ((p->p_flags & P_CONTROLT) && p->p_session->s_ttyvp) {
166 sess_hold(p->p_session);
167 return ((tpr_t) p->p_session);
169 return (NULL);
172 void
173 tprintf_close(tpr_t sess)
175 if (sess)
176 sess_rele((struct session *) sess);
180 * tprintf prints on the controlling terminal associated
181 * with the given session.
184 tprintf(tpr_t tpr, const char *fmt, ...)
186 struct session *sess = (struct session *)tpr;
187 struct tty *tp = NULL;
188 int flags = TOLOG;
189 __va_list ap;
190 struct putchar_arg pca;
191 int retval;
193 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
194 flags |= TOTTY;
195 tp = sess->s_ttyp;
197 __va_start(ap, fmt);
198 pca.tty = tp;
199 pca.flags = flags;
200 pca.pri = LOG_INFO;
201 retval = kvcprintf(fmt, kputchar, &pca, ap);
202 __va_end(ap);
203 msgbuftrigger = 1;
204 return (retval);
208 * Ttyprintf displays a message on a tty; it should be used only by
209 * the tty driver, or anything that knows the underlying tty will not
210 * be revoke(2)'d away. Other callers should use tprintf.
213 ttyprintf(struct tty *tp, const char *fmt, ...)
215 __va_list ap;
216 struct putchar_arg pca;
217 int retval;
219 __va_start(ap, fmt);
220 pca.tty = tp;
221 pca.flags = TOTTY;
222 retval = kvcprintf(fmt, kputchar, &pca, ap);
223 __va_end(ap);
224 return (retval);
228 * Log writes to the log buffer, and guarantees not to sleep (so can be
229 * called by interrupt routines). If there is no process reading the
230 * log yet, it writes to the console also.
233 log(int level, const char *fmt, ...)
235 __va_list ap;
236 int retval;
237 struct putchar_arg pca;
239 pca.tty = NULL;
240 pca.pri = level;
241 if ((kprintf_logging & TOCONS) == 0 || log_open)
242 pca.flags = TOLOG;
243 else
244 pca.flags = TOCONS;
246 __va_start(ap, fmt);
247 retval = kvcprintf(fmt, kputchar, &pca, ap);
248 __va_end(ap);
250 msgbuftrigger = 1;
251 return (retval);
254 #define CONSCHUNK 128
256 void
257 log_console(struct uio *uio)
259 int c, i, error, iovlen, nl;
260 struct uio muio;
261 struct iovec *miov = NULL;
262 char *consbuffer;
263 int pri;
265 if (!log_console_output)
266 return;
268 pri = LOG_INFO | LOG_CONSOLE;
269 muio = *uio;
270 iovlen = uio->uio_iovcnt * sizeof (struct iovec);
271 miov = kmalloc(iovlen, M_TEMP, M_WAITOK);
272 consbuffer = kmalloc(CONSCHUNK, M_TEMP, M_WAITOK);
273 bcopy((caddr_t)muio.uio_iov, (caddr_t)miov, iovlen);
274 muio.uio_iov = miov;
275 uio = &muio;
277 nl = 0;
278 while (uio->uio_resid > 0) {
279 c = (int)szmin(uio->uio_resid, CONSCHUNK);
280 error = uiomove(consbuffer, (size_t)c, uio);
281 if (error != 0)
282 break;
283 for (i = 0; i < c; i++) {
284 msglogchar(consbuffer[i], pri);
285 if (consbuffer[i] == '\n')
286 nl = 1;
287 else
288 nl = 0;
291 if (!nl)
292 msglogchar('\n', pri);
293 msgbuftrigger = 1;
294 kfree(miov, M_TEMP);
295 kfree(consbuffer, M_TEMP);
296 return;
300 * Output to the console.
303 kprintf(const char *fmt, ...)
305 __va_list ap;
306 int savintr;
307 struct putchar_arg pca;
308 int retval;
310 savintr = consintr; /* disable interrupts */
311 consintr = 0;
312 __va_start(ap, fmt);
313 pca.tty = NULL;
314 pca.flags = kprintf_logging & ~TOTTY;
315 pca.pri = -1;
316 retval = kvcprintf(fmt, kputchar, &pca, ap);
317 __va_end(ap);
318 if (!panicstr)
319 msgbuftrigger = 1;
320 consintr = savintr; /* reenable interrupts */
321 return (retval);
325 kvprintf(const char *fmt, __va_list ap)
327 int savintr;
328 struct putchar_arg pca;
329 int retval;
331 savintr = consintr; /* disable interrupts */
332 consintr = 0;
333 pca.tty = NULL;
334 pca.flags = kprintf_logging & ~TOTTY;
335 pca.pri = -1;
336 retval = kvcprintf(fmt, kputchar, &pca, ap);
337 if (!panicstr)
338 msgbuftrigger = 1;
339 consintr = savintr; /* reenable interrupts */
340 return (retval);
344 * Limited rate kprintf. The passed rate structure must be initialized
345 * with the desired reporting frequency. A frequency of 0 will result in
346 * no output.
348 * count may be initialized to a negative number to allow an initial
349 * burst.
351 void
352 krateprintf(struct krate *rate, const char *fmt, ...)
354 __va_list ap;
356 if (rate->ticks != (int)time_uptime) {
357 rate->ticks = (int)time_uptime;
358 if (rate->count > 0)
359 rate->count = 0;
361 if (rate->count < rate->freq) {
362 ++rate->count;
363 __va_start(ap, fmt);
364 kvprintf(fmt, ap);
365 __va_end(ap);
370 * Print a character to the dmesg log, the console, and/or the user's
371 * terminal.
373 * NOTE: TOTTY does not require nonblocking operation, but TOCONS
374 * and TOLOG do. When we have a constty we still output to
375 * the real console but we have a monitoring thread which
376 * we wakeup which tracks the log.
378 static void
379 kputchar(int c, void *arg)
381 struct putchar_arg *ap = (struct putchar_arg*) arg;
382 int flags = ap->flags;
383 struct tty *tp = ap->tty;
385 if (panicstr)
386 constty = NULL;
387 if ((flags & TOCONS) && tp == NULL && constty)
388 flags |= TOLOG | TOWAKEUP;
389 if ((flags & TOTTY) && tputchar(c, tp) < 0)
390 ap->flags &= ~TOTTY;
391 if ((flags & TOLOG))
392 msglogchar(c, ap->pri);
393 if ((flags & TOCONS) && c)
394 cnputc(c);
395 if (flags & TOWAKEUP)
396 wakeup(constty_td);
400 * Scaled down version of sprintf(3).
403 ksprintf(char *buf, const char *cfmt, ...)
405 int retval;
406 __va_list ap;
408 __va_start(ap, cfmt);
409 retval = kvcprintf(cfmt, NULL, buf, ap);
410 buf[retval] = '\0';
411 __va_end(ap);
412 return (retval);
416 * Scaled down version of vsprintf(3).
419 kvsprintf(char *buf, const char *cfmt, __va_list ap)
421 int retval;
423 retval = kvcprintf(cfmt, NULL, buf, ap);
424 buf[retval] = '\0';
425 return (retval);
429 * Scaled down version of snprintf(3).
432 ksnprintf(char *str, size_t size, const char *format, ...)
434 int retval;
435 __va_list ap;
437 __va_start(ap, format);
438 retval = kvsnprintf(str, size, format, ap);
439 __va_end(ap);
440 return(retval);
444 * Scaled down version of vsnprintf(3).
447 kvsnprintf(char *str, size_t size, const char *format, __va_list ap)
449 struct snprintf_arg info;
450 int retval;
452 info.str = str;
453 info.remain = size;
454 retval = kvcprintf(format, snprintf_func, &info, ap);
455 if (info.remain >= 1)
456 *info.str++ = '\0';
457 return (retval);
461 kvasnprintf(char **strp, size_t size, const char *format, __va_list ap)
463 struct snprintf_arg info;
464 int retval;
466 *strp = kmalloc(size, M_TEMP, M_WAITOK);
467 info.str = *strp;
468 info.remain = size;
469 retval = kvcprintf(format, snprintf_func, &info, ap);
470 if (info.remain >= 1)
471 *info.str++ = '\0';
472 return (retval);
475 void
476 kvasfree(char **strp)
478 if (*strp) {
479 kfree(*strp, M_TEMP);
480 *strp = NULL;
484 static void
485 snprintf_func(int ch, void *arg)
487 struct snprintf_arg *const info = arg;
489 if (info->remain >= 2) {
490 *info->str++ = ch;
491 info->remain--;
496 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
497 * order; return an optional length and a pointer to the last character
498 * written in the buffer (i.e., the first character of the string).
499 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
501 static char *
502 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
504 char *p, c;
506 p = nbuf;
507 *p = '\0';
508 do {
509 c = hex2ascii(num % base);
510 *++p = upper ? toupper(c) : c;
511 } while (num /= base);
512 if (lenp)
513 *lenp = p - nbuf;
514 return (p);
518 * Scaled down version of printf(3).
520 * Two additional formats:
522 * The format %pb%i is supported to decode error registers.
523 * Its usage is:
525 * kprintf("reg=%pb%i\n", "<base><arg>*", regval);
527 * where <base> is the output base expressed as a control character, e.g.
528 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
529 * the first of which gives the bit number to be inspected (origin 1), and
530 * the next characters (up to a control character, i.e. a character <= 32),
531 * give the name of the register. Thus:
533 * kvcprintf("reg=%pb%i\n", "\10\2BITTWO\1BITONE\n", 3);
535 * would produce output:
537 * reg=3<BITTWO,BITONE>
540 #define PCHAR(c) {int cc=(c); if(func) (*func)(cc,arg); else *d++=cc; retval++;}
543 kvcprintf(char const *fmt, void (*func)(int, void*), void *arg, __va_list ap)
545 char nbuf[MAXNBUF];
546 char *d;
547 const char *p, *percent, *q;
548 int ch, n;
549 uintmax_t num;
550 int base, tmp, width, ladjust, sharpflag, spaceflag, neg, sign, dot;
551 int cflag, hflag, jflag, lflag, qflag, tflag, zflag;
552 int dwidth, upper;
553 char padc;
554 int retval = 0, stop = 0;
555 int usespin;
556 int ddb_active;
559 * Make a supreme effort to avoid reentrant panics or deadlocks.
561 * NOTE! Do nothing that would access mycpu/gd/fs unless the
562 * function is the normal kputchar(), which allows us to
563 * use this function for very early debugging with a special
564 * function.
566 if (func == kputchar) {
567 if (mycpu->gd_flags & GDF_KPRINTF)
568 return(0);
569 atomic_set_long(&mycpu->gd_flags, GDF_KPRINTF);
572 #ifdef DDB
573 ddb_active = db_active;
574 #else
575 ddb_active = 0;
576 #endif
578 num = 0;
579 if (!func)
580 d = (char *) arg;
581 else
582 d = NULL;
584 if (fmt == NULL)
585 fmt = "(fmt null)\n";
587 usespin = (func == kputchar &&
588 (kprintf_logging & TONOSPIN) == 0 &&
589 panic_cpu_gd != mycpu &&
590 (((struct putchar_arg *)arg)->flags & TOTTY) == 0);
591 if (usespin) {
592 crit_enter_hard();
593 spin_lock(&cons_spin);
596 for (;;) {
597 padc = ' ';
598 width = 0;
599 while ((ch = (u_char)*fmt++) != '%' || stop) {
600 if (ch == '\0')
601 goto done;
602 PCHAR(ch);
604 percent = fmt - 1;
605 dot = dwidth = ladjust = neg = sharpflag = sign = upper = 0;
606 spaceflag = 0;
607 cflag = hflag = jflag = lflag = qflag = tflag = zflag = 0;
609 reswitch:
610 switch (ch = (u_char)*fmt++) {
611 case ' ':
612 spaceflag = 1;
613 goto reswitch;
614 case '.':
615 dot = 1;
616 goto reswitch;
617 case '#':
618 sharpflag = 1;
619 goto reswitch;
620 case '+':
621 sign = 1;
622 goto reswitch;
623 case '-':
624 ladjust = 1;
625 goto reswitch;
626 case '%':
627 PCHAR(ch);
628 break;
629 case '*':
630 if (!dot) {
631 width = __va_arg(ap, int);
632 if (width < 0) {
633 ladjust = !ladjust;
634 width = -width;
636 } else {
637 dwidth = __va_arg(ap, int);
639 goto reswitch;
640 case '0':
641 if (!dot) {
642 padc = '0';
643 goto reswitch;
645 case '1': case '2': case '3': case '4':
646 case '5': case '6': case '7': case '8': case '9':
647 for (n = 0;; ++fmt) {
648 n = n * 10 + ch - '0';
649 ch = *fmt;
650 if (ch < '0' || ch > '9')
651 break;
653 if (dot)
654 dwidth = n;
655 else
656 width = n;
657 goto reswitch;
658 case 'c':
659 PCHAR(__va_arg(ap, int));
660 break;
661 case 'd':
662 case 'i':
663 base = 10;
664 sign = 1;
665 goto handle_sign;
666 case 'h':
667 if (hflag) {
668 hflag = 0;
669 cflag = 1;
670 } else
671 hflag = 1;
672 goto reswitch;
673 case 'j':
674 jflag = 1;
675 goto reswitch;
676 case 'l':
677 if (lflag) {
678 lflag = 0;
679 qflag = 1;
680 } else
681 lflag = 1;
682 goto reswitch;
683 case 'n':
684 if (cflag)
685 *(__va_arg(ap, char *)) = retval;
686 else if (hflag)
687 *(__va_arg(ap, short *)) = retval;
688 else if (jflag)
689 *(__va_arg(ap, intmax_t *)) = retval;
690 else if (lflag)
691 *(__va_arg(ap, long *)) = retval;
692 else if (qflag)
693 *(__va_arg(ap, quad_t *)) = retval;
694 else
695 *(__va_arg(ap, int *)) = retval;
696 break;
697 case 'o':
698 base = 8;
699 goto handle_nosign;
700 case 'p':
701 /* peek if this is a /b/ hiding as /p/ or not */
702 if (fmt[0] == 'b' && fmt[1] == '%' && fmt[2] == 'i') {
703 fmt += 3; /* consume "b%i" */
704 p = __va_arg(ap, char *);
705 num = (u_int)__va_arg(ap, int);
706 for (q = ksprintn(nbuf, num, *p++, NULL, 0);*q;)
707 PCHAR(*q--);
709 if (num == 0)
710 break;
712 for (tmp = 0; *p;) {
713 n = *p++;
714 if (num & (1 << (n - 1))) {
715 PCHAR(tmp ? ',' : '<');
716 for (; (n = *p) > ' '; ++p)
717 PCHAR(n);
718 tmp = 1;
719 } else {
720 for (; *p > ' '; ++p)
721 continue;
724 if (tmp)
725 PCHAR('>');
726 break;
728 base = 16;
729 sharpflag = (width == 0);
730 sign = 0;
731 num = (uintptr_t)__va_arg(ap, void *);
732 if (ptr_restrict && fmt[0] != 'x' &&
733 !(panicstr || dumping || ddb_active)) {
734 if (ptr_restrict == 1) {
735 /* zero out upper bits */
736 num &= 0xffffffUL;
737 } else {
738 num = 0xc0ffee;
741 goto number;
742 case 'q':
743 qflag = 1;
744 goto reswitch;
745 case 's':
746 p = __va_arg(ap, char *);
747 if (p == NULL)
748 p = "(null)";
749 if (!dot)
750 n = strlen (p);
751 else
752 for (n = 0; n < dwidth && p[n]; n++)
753 continue;
755 width -= n;
757 if (!ladjust && width > 0)
758 while (width--)
759 PCHAR(padc);
760 while (n--)
761 PCHAR(*p++);
762 if (ladjust && width > 0)
763 while (width--)
764 PCHAR(padc);
765 break;
766 case 't':
767 tflag = 1;
768 goto reswitch;
769 case 'u':
770 base = 10;
771 goto handle_nosign;
772 case 'X':
773 upper = 1;
774 /* FALLTHROUGH */
775 case 'x':
776 base = 16;
777 goto handle_nosign;
778 case 'z':
779 zflag = 1;
780 goto reswitch;
781 handle_nosign:
782 sign = 0;
783 if (cflag)
784 num = (u_char)__va_arg(ap, int);
785 else if (hflag)
786 num = (u_short)__va_arg(ap, int);
787 else if (jflag)
788 num = __va_arg(ap, uintmax_t);
789 else if (lflag)
790 num = __va_arg(ap, u_long);
791 else if (qflag)
792 num = __va_arg(ap, u_quad_t);
793 else if (tflag)
794 num = __va_arg(ap, ptrdiff_t);
795 else if (zflag)
796 num = __va_arg(ap, size_t);
797 else
798 num = __va_arg(ap, u_int);
799 goto number;
800 handle_sign:
801 if (cflag)
802 num = (char)__va_arg(ap, int);
803 else if (hflag)
804 num = (short)__va_arg(ap, int);
805 else if (jflag)
806 num = __va_arg(ap, intmax_t);
807 else if (lflag)
808 num = __va_arg(ap, long);
809 else if (qflag)
810 num = __va_arg(ap, quad_t);
811 else if (tflag)
812 num = __va_arg(ap, ptrdiff_t);
813 else if (zflag)
814 num = __va_arg(ap, ssize_t);
815 else
816 num = __va_arg(ap, int);
817 number:
818 if (sign && (intmax_t)num < 0) {
819 neg = 1;
820 num = -(intmax_t)num;
822 p = ksprintn(nbuf, num, base, &n, upper);
823 tmp = 0;
824 if (sharpflag && num != 0) {
825 if (base == 8)
826 tmp++;
827 else if (base == 16)
828 tmp += 2;
830 if (neg || (sign && spaceflag))
831 tmp++;
833 if (!ladjust && padc == '0')
834 dwidth = width - tmp;
835 width -= tmp + imax(dwidth, n);
836 dwidth -= n;
837 if (!ladjust)
838 while (width-- > 0)
839 PCHAR(' ');
840 if (neg) {
841 PCHAR('-');
842 } else if (sign && spaceflag) {
843 PCHAR(' ');
845 if (sharpflag && num != 0) {
846 if (base == 8) {
847 PCHAR('0');
848 } else if (base == 16) {
849 PCHAR('0');
850 PCHAR('x');
853 while (dwidth-- > 0)
854 PCHAR('0');
856 while (*p)
857 PCHAR(*p--);
859 if (ladjust)
860 while (width-- > 0)
861 PCHAR(' ');
863 break;
864 default:
865 while (percent < fmt)
866 PCHAR(*percent++);
868 * Since we ignore an formatting argument it is no
869 * longer safe to obey the remaining formatting
870 * arguments as the arguments will no longer match
871 * the format specs.
873 stop = 1;
874 break;
877 done:
879 * Cleanup reentrancy issues.
881 if (func == kputchar)
882 atomic_clear_long(&mycpu->gd_flags, GDF_KPRINTF);
883 if (usespin) {
884 spin_unlock(&cons_spin);
885 crit_exit_hard();
887 return (retval);
890 #undef PCHAR
893 * Called from the panic code to try to get the console working
894 * again in case we paniced inside a kprintf().
896 void
897 kvcreinitspin(void)
899 spin_init(&cons_spin, "kvcre");
900 atomic_clear_long(&mycpu->gd_flags, GDF_KPRINTF);
904 * Console support thread for constty intercepts. This is needed because
905 * console tty intercepts can block. Instead of having kputchar() attempt
906 * to directly write to the console intercept we just force it to log
907 * and wakeup this baby to track and dump the log to constty.
909 static void
910 constty_daemon(void)
912 u_int rindex;
913 u_int xindex;
914 u_int n;
915 struct msgbuf *mbp;
916 struct tty *tp;
918 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc,
919 constty_td, SHUTDOWN_PRI_FIRST);
920 constty_td->td_flags |= TDF_SYSTHREAD;
922 mbp = msgbufp;
923 rindex = mbp->msg_bufr; /* persistent loop variable */
924 xindex = mbp->msg_bufx - 1; /* anything different than bufx */
925 cpu_ccfence();
927 for (;;) {
928 kproc_suspend_loop();
930 crit_enter();
931 if (mbp != msgbufp)
932 mbp = msgbufp;
933 if (xindex == mbp->msg_bufx ||
934 mbp == NULL ||
935 msgbufmapped == 0) {
936 tsleep(constty_td, 0, "waiting", hz*60);
937 crit_exit();
938 continue;
940 crit_exit();
943 * Get message buf FIFO indices. rindex is tracking.
945 xindex = mbp->msg_bufx;
946 cpu_ccfence();
947 if ((tp = constty) == NULL) {
948 rindex = xindex;
949 continue;
953 * Check if the calculated bytes has rolled the whole
954 * message buffer.
956 n = xindex - rindex;
957 if (n > mbp->msg_size - 1024) {
958 rindex = xindex - mbp->msg_size + 2048;
959 n = xindex - rindex;
963 * And dump it. If constty gets stuck will give up.
965 while (rindex != xindex) {
966 u_int ri = rindex % mbp->msg_size;
967 if (tputchar((uint8_t)mbp->msg_ptr[ri], tp) < 0) {
968 constty = NULL;
969 rindex = xindex;
970 break;
972 if (tp->t_outq.c_cc >= tp->t_ohiwat) {
973 tsleep(constty_daemon, 0, "blocked", hz / 10);
974 if (tp->t_outq.c_cc >= tp->t_ohiwat) {
975 rindex = xindex;
976 break;
979 ++rindex;
984 static struct kproc_desc constty_kp = {
985 "consttyd",
986 constty_daemon,
987 &constty_td
989 SYSINIT(bufdaemon, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY,
990 kproc_start, &constty_kp);
993 * Put character in log buffer with a particular priority.
995 * MPSAFE
997 static void
998 msglogchar(int c, int pri)
1000 static int lastpri = -1;
1001 static int dangling;
1002 char nbuf[MAXNBUF];
1003 char *p;
1005 if (!msgbufmapped)
1006 return;
1007 if (c == '\0' || c == '\r')
1008 return;
1009 if (pri != -1 && pri != lastpri) {
1010 if (dangling) {
1011 msgaddchar('\n', NULL);
1012 dangling = 0;
1014 msgaddchar('<', NULL);
1015 for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL, 0); *p;)
1016 msgaddchar(*p--, NULL);
1017 msgaddchar('>', NULL);
1018 lastpri = pri;
1020 msgaddchar(c, NULL);
1021 if (c == '\n') {
1022 dangling = 0;
1023 lastpri = -1;
1024 } else {
1025 dangling = 1;
1030 * Put char in log buffer. Make sure nothing blows up beyond repair if
1031 * we have an MP race.
1033 * MPSAFE.
1035 static void
1036 msgaddchar(int c, void *dummy)
1038 struct msgbuf *mbp;
1039 u_int lindex;
1040 u_int rindex;
1041 u_int xindex;
1042 u_int n;
1044 if (!msgbufmapped)
1045 return;
1046 mbp = msgbufp;
1047 lindex = mbp->msg_bufl;
1048 rindex = mbp->msg_bufr;
1049 xindex = mbp->msg_bufx++; /* Allow SMP race */
1050 cpu_ccfence();
1052 mbp->msg_ptr[xindex % mbp->msg_size] = c;
1053 n = xindex - lindex;
1054 if (n > mbp->msg_size - 1024) {
1055 lindex = xindex - mbp->msg_size + 2048;
1056 cpu_ccfence();
1057 mbp->msg_bufl = lindex;
1059 n = xindex - rindex;
1060 if (n > mbp->msg_size - 1024) {
1061 rindex = xindex - mbp->msg_size + 2048;
1062 cpu_ccfence();
1063 mbp->msg_bufr = rindex;
1067 static void
1068 msgbufcopy(struct msgbuf *oldp)
1070 u_int rindex;
1071 u_int xindex;
1072 u_int n;
1074 rindex = oldp->msg_bufr;
1075 xindex = oldp->msg_bufx;
1076 cpu_ccfence();
1078 n = xindex - rindex;
1079 if (n > oldp->msg_size - 1024)
1080 rindex = xindex - oldp->msg_size + 2048;
1081 while (rindex != xindex) {
1082 msglogchar(oldp->msg_ptr[rindex % oldp->msg_size], -1);
1083 ++rindex;
1087 void
1088 msgbufinit(void *ptr, size_t size)
1090 char *cp;
1091 static struct msgbuf *oldp = NULL;
1093 size -= sizeof(*msgbufp);
1094 cp = (char *)ptr;
1095 msgbufp = (struct msgbuf *) (cp + size);
1096 if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size) {
1097 bzero(cp, size);
1098 bzero(msgbufp, sizeof(*msgbufp));
1099 msgbufp->msg_magic = MSG_MAGIC;
1100 msgbufp->msg_size = (char *)msgbufp - cp;
1102 msgbufp->msg_ptr = cp;
1103 if (msgbufmapped && oldp != msgbufp)
1104 msgbufcopy(oldp);
1105 cpu_mfence();
1106 msgbufmapped = 1;
1107 oldp = msgbufp;
1110 /* Sysctls for accessing/clearing the msgbuf */
1112 static int
1113 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
1115 struct msgbuf *mbp;
1116 struct ucred *cred;
1117 int error;
1118 u_int rindex_modulo;
1119 u_int xindex_modulo;
1120 u_int rindex;
1121 u_int xindex;
1122 u_int n;
1125 * Only wheel or root can access the message log.
1127 if (unprivileged_read_msgbuf == 0) {
1128 KKASSERT(req->td->td_proc);
1129 cred = req->td->td_proc->p_ucred;
1131 if ((cred->cr_prison || groupmember(0, cred) == 0) &&
1132 priv_check(req->td, PRIV_ROOT) != 0
1134 return (EPERM);
1139 * Unwind the buffer, so that it's linear (possibly starting with
1140 * some initial nulls).
1142 * We don't push the entire buffer like we did before because
1143 * bufr (and bufl) now advance in chunks when the fifo is full,
1144 * rather than one character.
1146 mbp = msgbufp;
1147 rindex = mbp->msg_bufr;
1148 xindex = mbp->msg_bufx;
1149 n = xindex - rindex;
1150 if (n > mbp->msg_size - 1024) {
1151 rindex = xindex - mbp->msg_size + 2048;
1152 n = xindex - rindex;
1154 rindex_modulo = rindex % mbp->msg_size;
1155 xindex_modulo = xindex % mbp->msg_size;
1157 if (rindex_modulo < xindex_modulo) {
1159 * Can handle in one linear section.
1161 error = sysctl_handle_opaque(oidp,
1162 mbp->msg_ptr + rindex_modulo,
1163 xindex_modulo - rindex_modulo,
1164 req);
1165 } else if (rindex_modulo == xindex_modulo) {
1167 * Empty buffer, just return a single newline
1169 error = sysctl_handle_opaque(oidp, "\n", 1, req);
1170 } else if (n <= mbp->msg_size - rindex_modulo) {
1172 * Can handle in one linear section.
1174 error = sysctl_handle_opaque(oidp,
1175 mbp->msg_ptr + rindex_modulo,
1176 n - rindex_modulo,
1177 req);
1178 } else {
1180 * Glue together two linear sections into one contiguous
1181 * output.
1183 error = sysctl_handle_opaque(oidp,
1184 mbp->msg_ptr + rindex_modulo,
1185 mbp->msg_size - rindex_modulo,
1186 req);
1187 n -= mbp->msg_size - rindex_modulo;
1188 if (error == 0)
1189 error = sysctl_handle_opaque(oidp, mbp->msg_ptr,
1190 n, req);
1192 return (error);
1195 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
1196 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
1198 static int msgbuf_clear;
1200 static int
1201 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
1203 int error;
1204 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
1205 if (!error && req->newptr) {
1206 /* Clear the buffer and reset write pointer */
1207 msgbufp->msg_bufr = msgbufp->msg_bufx;
1208 msgbufp->msg_bufl = msgbufp->msg_bufx;
1209 bzero(msgbufp->msg_ptr, msgbufp->msg_size);
1210 msgbuf_clear = 0;
1212 return (error);
1215 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
1216 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0,
1217 sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
1219 #ifdef DDB
1221 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
1223 u_int rindex;
1224 u_int i;
1225 u_int j;
1227 if (!msgbufmapped) {
1228 db_printf("msgbuf not mapped yet\n");
1229 return;
1231 db_printf("msgbufp = %p\n", msgbufp);
1232 db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n",
1233 msgbufp->msg_magic, msgbufp->msg_size,
1234 msgbufp->msg_bufr % msgbufp->msg_size,
1235 msgbufp->msg_bufx % msgbufp->msg_size,
1236 msgbufp->msg_ptr);
1238 rindex = msgbufp->msg_bufr;
1239 for (i = 0; i < msgbufp->msg_size; i++) {
1240 j = (i + rindex) % msgbufp->msg_size;
1241 db_printf("%c", msgbufp->msg_ptr[j]);
1243 db_printf("\n");
1246 #endif /* DDB */
1249 void
1250 hexdump(const void *ptr, int length, const char *hdr, int flags)
1252 int i, j, k;
1253 int cols;
1254 const unsigned char *cp;
1255 char delim;
1257 if ((flags & HD_DELIM_MASK) != 0)
1258 delim = (flags & HD_DELIM_MASK) >> 8;
1259 else
1260 delim = ' ';
1262 if ((flags & HD_COLUMN_MASK) != 0)
1263 cols = flags & HD_COLUMN_MASK;
1264 else
1265 cols = 16;
1267 cp = ptr;
1268 for (i = 0; i < length; i+= cols) {
1269 if (hdr != NULL)
1270 kprintf("%s", hdr);
1272 if ((flags & HD_OMIT_COUNT) == 0)
1273 kprintf("%04x ", i);
1275 if ((flags & HD_OMIT_HEX) == 0) {
1276 for (j = 0; j < cols; j++) {
1277 k = i + j;
1278 if (k < length)
1279 kprintf("%c%02x", delim, cp[k]);
1280 else
1281 kprintf(" ");
1285 if ((flags & HD_OMIT_CHARS) == 0) {
1286 kprintf(" |");
1287 for (j = 0; j < cols; j++) {
1288 k = i + j;
1289 if (k >= length)
1290 kprintf(" ");
1291 else if (cp[k] >= ' ' && cp[k] <= '~')
1292 kprintf("%c", cp[k]);
1293 else
1294 kprintf(".");
1296 kprintf("|");
1298 kprintf("\n");
1302 void
1303 kprint_cpuset(cpumask_t *mask)
1305 int i;
1306 int b = -1;
1307 int e = -1;
1308 int more = 0;
1310 kprintf("cpus(");
1311 CPUSET_FOREACH(i, *mask) {
1312 if (b < 0) {
1313 b = i;
1314 e = b + 1;
1315 continue;
1317 if (e == i) {
1318 ++e;
1319 continue;
1321 if (more)
1322 kprintf(", ");
1323 if (b == e - 1) {
1324 kprintf("%d", b);
1325 } else {
1326 kprintf("%d-%d", b, e - 1);
1328 more = 1;
1329 b = i;
1330 e = b + 1;
1332 if (more)
1333 kprintf(", ");
1334 if (b >= 0) {
1335 if (b == e - 1) {
1336 kprintf("%d", b);
1337 } else {
1338 kprintf("%d-%d", b, e - 1);
1341 kprintf(") ");