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