Home | History | Annotate | Line # | Download | only in kern
subr_prf.c revision 1.167
      1 /*	$NetBSD: subr_prf.c,v 1.167 2018/04/11 23:20:15 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1986, 1988, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)subr_prf.c	8.4 (Berkeley) 5/4/95
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: subr_prf.c,v 1.167 2018/04/11 23:20:15 christos Exp $");
     41 
     42 #ifdef _KERNEL_OPT
     43 #include "opt_ddb.h"
     44 #include "opt_ipkdb.h"
     45 #include "opt_kgdb.h"
     46 #include "opt_dump.h"
     47 #include "opt_rnd_printf.h"
     48 #endif
     49 
     50 #include <sys/param.h>
     51 #include <sys/stdint.h>
     52 #include <sys/systm.h>
     53 #include <sys/buf.h>
     54 #include <sys/device.h>
     55 #include <sys/reboot.h>
     56 #include <sys/msgbuf.h>
     57 #include <sys/proc.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/vnode.h>
     60 #include <sys/file.h>
     61 #include <sys/tty.h>
     62 #include <sys/tprintf.h>
     63 #include <sys/spldebug.h>
     64 #include <sys/syslog.h>
     65 #include <sys/kprintf.h>
     66 #include <sys/atomic.h>
     67 #include <sys/kernel.h>
     68 #include <sys/cpu.h>
     69 #include <sys/sha2.h>
     70 #include <sys/rndsource.h>
     71 
     72 #include <dev/cons.h>
     73 
     74 #include <net/if.h>
     75 
     76 #ifdef IPKDB
     77 #include <ipkdb/ipkdb.h>
     78 #endif
     79 
     80 static kmutex_t kprintf_mtx;
     81 static bool kprintf_inited = false;
     82 
     83 #ifdef KGDB
     84 #include <sys/kgdb.h>
     85 #endif
     86 
     87 #ifdef DDB
     88 #include <ddb/ddbvar.h>		/* db_panic */
     89 #include <ddb/db_output.h>	/* db_printf, db_putchar prototypes */
     90 #endif
     91 
     92 
     93 /*
     94  * defines
     95  */
     96 #define KLOG_PRI	0x80000000
     97 
     98 
     99 /*
    100  * local prototypes
    101  */
    102 
    103 static void	 putchar(int, int, struct tty *);
    104 
    105 
    106 /*
    107  * globals
    108  */
    109 
    110 extern	struct tty *constty;	/* pointer to console "window" tty */
    111 extern	int log_open;	/* subr_log: is /dev/klog open? */
    112 extern	krndsource_t	rnd_printf_source;
    113 const	char *panicstr; /* arg to first call to panic (used as a flag
    114 			   to indicate that panic has already been called). */
    115 struct cpu_info *paniccpu;	/* cpu that first paniced */
    116 long	panicstart, panicend;	/* position in the msgbuf of the start and
    117 				   end of the formatted panicstr. */
    118 int	doing_shutdown;	/* set to indicate shutdown in progress */
    119 
    120 #ifdef RND_PRINTF
    121 static bool kprintf_inited_callout = false;
    122 static SHA512_CTX kprnd_sha;
    123 static uint8_t kprnd_accum[SHA512_DIGEST_LENGTH];
    124 static int kprnd_added;
    125 
    126 static struct callout kprnd_callout;
    127 #endif
    128 
    129 #ifndef	DUMP_ON_PANIC
    130 #define	DUMP_ON_PANIC	1
    131 #endif
    132 int	dumponpanic = DUMP_ON_PANIC;
    133 
    134 /*
    135  * v_putc: routine to putc on virtual console
    136  *
    137  * the v_putc pointer can be used to redirect the console cnputc elsewhere
    138  * [e.g. to a "virtual console"].
    139  */
    140 
    141 void (*v_putc)(int) = cnputc;	/* start with cnputc (normal cons) */
    142 void (*v_flush)(void) = cnflush;	/* start with cnflush (normal cons) */
    143 
    144 const char hexdigits[] = "0123456789abcdef";
    145 const char HEXDIGITS[] = "0123456789ABCDEF";
    146 
    147 
    148 /*
    149  * functions
    150  */
    151 
    152 #ifdef RND_PRINTF
    153 static void kprintf_rnd_get(size_t bytes, void *priv)
    154 {
    155 	if (kprnd_added)  {
    156 		KASSERT(kprintf_inited);
    157 		if (mutex_tryenter(&kprintf_mtx)) {
    158 			SHA512_Final(kprnd_accum, &kprnd_sha);
    159 			rnd_add_data(&rnd_printf_source,
    160 				     kprnd_accum, sizeof(kprnd_accum), 0);
    161 			kprnd_added = 0;
    162 			/* This, we must do, since we called _Final. */
    163 			SHA512_Init(&kprnd_sha);
    164 			/* This is optional but seems useful. */
    165 			SHA512_Update(&kprnd_sha, kprnd_accum,
    166 				      sizeof(kprnd_accum));
    167 			mutex_exit(&kprintf_mtx);
    168 		}
    169 	}
    170 }
    171 
    172 static void kprintf_rnd_callout(void *arg)
    173 {
    174 	kprintf_rnd_get(0, NULL);
    175 	callout_schedule(&kprnd_callout, hz);
    176 }
    177 
    178 #endif
    179 
    180 /*
    181  * Locking is inited fairly early in MI bootstrap.  Before that
    182  * prints are done unlocked.  But that doesn't really matter,
    183  * since nothing can preempt us before interrupts are enabled.
    184  */
    185 void
    186 kprintf_init(void)
    187 {
    188 
    189 	KASSERT(!kprintf_inited && cold); /* not foolproof, but ... */
    190 #ifdef RND_PRINTF
    191 	SHA512_Init(&kprnd_sha);
    192 #endif
    193 	mutex_init(&kprintf_mtx, MUTEX_DEFAULT, IPL_HIGH);
    194 	kprintf_inited = true;
    195 }
    196 
    197 #ifdef RND_PRINTF
    198 void
    199 kprintf_init_callout(void)
    200 {
    201 	KASSERT(!kprintf_inited_callout);
    202 	callout_init(&kprnd_callout, CALLOUT_MPSAFE);
    203 	callout_setfunc(&kprnd_callout, kprintf_rnd_callout, NULL);
    204 	callout_schedule(&kprnd_callout, hz);
    205 	kprintf_inited_callout = true;
    206 }
    207 #endif
    208 
    209 void
    210 kprintf_lock(void)
    211 {
    212 
    213 	if (__predict_true(kprintf_inited))
    214 		mutex_enter(&kprintf_mtx);
    215 }
    216 
    217 void
    218 kprintf_unlock(void)
    219 {
    220 
    221 	if (__predict_true(kprintf_inited)) {
    222 		/* assert kprintf wasn't somehow inited while we were in */
    223 		KASSERT(mutex_owned(&kprintf_mtx));
    224 		mutex_exit(&kprintf_mtx);
    225 	}
    226 }
    227 
    228 /*
    229  * twiddle: spin a little propellor on the console.
    230  */
    231 
    232 void
    233 twiddle(void)
    234 {
    235 	static const char twiddle_chars[] = "|/-\\";
    236 	static int pos;
    237 
    238 	kprintf_lock();
    239 
    240 	putchar(twiddle_chars[pos++ & 3], TOCONS, NULL);
    241 	putchar('\b', TOCONS, NULL);
    242 
    243 	kprintf_unlock();
    244 }
    245 
    246 /*
    247  * panic: handle an unresolvable fatal error
    248  *
    249  * prints "panic: <message>" and reboots.   if called twice (i.e. recursive
    250  * call) we avoid trying to dump and just reboot (to avoid recursive panics).
    251  */
    252 
    253 void
    254 panic(const char *fmt, ...)
    255 {
    256 	va_list ap;
    257 
    258 	va_start(ap, fmt);
    259 	vpanic(fmt, ap);
    260 	va_end(ap);
    261 }
    262 
    263 void
    264 vpanic(const char *fmt, va_list ap)
    265 {
    266 	CPU_INFO_ITERATOR cii;
    267 	struct cpu_info *ci, *oci;
    268 	int bootopt;
    269 	static char scratchstr[384]; /* stores panic message */
    270 
    271 	spldebug_stop();
    272 
    273 	if (lwp0.l_cpu && curlwp) {
    274 		/*
    275 		 * Disable preemption.  If already panicing on another CPU, sit
    276 		 * here and spin until the system is rebooted.  Allow the CPU that
    277 		 * first paniced to panic again.
    278 		 */
    279 		kpreempt_disable();
    280 		ci = curcpu();
    281 		oci = atomic_cas_ptr((void *)&paniccpu, NULL, ci);
    282 		if (oci != NULL && oci != ci) {
    283 			/* Give interrupts a chance to try and prevent deadlock. */
    284 			for (;;) {
    285 #ifndef _RUMPKERNEL /* XXXpooka: temporary build fix, see kern/40505 */
    286 				DELAY(10);
    287 #endif /* _RUMPKERNEL */
    288 			}
    289 		}
    290 
    291 		/*
    292 		 * Convert the current thread to a bound thread and prevent all
    293 		 * CPUs from scheduling unbound jobs.  Do so without taking any
    294 		 * locks.
    295 		 */
    296 		curlwp->l_pflag |= LP_BOUND;
    297 		for (CPU_INFO_FOREACH(cii, ci)) {
    298 			ci->ci_schedstate.spc_flags |= SPCF_OFFLINE;
    299 		}
    300 	}
    301 
    302 	bootopt = RB_AUTOBOOT | RB_NOSYNC;
    303 	if (!doing_shutdown) {
    304 		if (dumponpanic)
    305 			bootopt |= RB_DUMP;
    306 	} else
    307 		printf("Skipping crash dump on recursive panic\n");
    308 
    309 	doing_shutdown = 1;
    310 
    311 	if (logenabled(msgbufp))
    312 		panicstart = msgbufp->msg_bufx;
    313 
    314 	printf("panic: ");
    315 	if (panicstr == NULL) {
    316 		/* first time in panic - store fmt first for precaution */
    317 		panicstr = fmt;
    318 
    319 		vsnprintf(scratchstr, sizeof(scratchstr), fmt, ap);
    320 		printf("%s", scratchstr);
    321 		panicstr = scratchstr;
    322 	} else {
    323 		vprintf(fmt, ap);
    324 	}
    325 	printf("\n");
    326 
    327 	if (logenabled(msgbufp))
    328 		panicend = msgbufp->msg_bufx;
    329 
    330 #ifdef IPKDB
    331 	ipkdb_panic();
    332 #endif
    333 #ifdef KGDB
    334 	kgdb_panic();
    335 #endif
    336 #ifdef KADB
    337 	if (boothowto & RB_KDB)
    338 		kdbpanic();
    339 #endif
    340 #ifdef DDB
    341 	db_panic();
    342 #endif
    343 	cpu_reboot(bootopt, NULL);
    344 }
    345 
    346 /*
    347  * kernel logging functions: log, logpri, addlog
    348  */
    349 
    350 /*
    351  * log: write to the log buffer
    352  *
    353  * => will not sleep [so safe to call from interrupt]
    354  * => will log to console if /dev/klog isn't open
    355  */
    356 
    357 void
    358 log(int level, const char *fmt, ...)
    359 {
    360 	va_list ap;
    361 
    362 	kprintf_lock();
    363 
    364 	klogpri(level);		/* log the level first */
    365 	va_start(ap, fmt);
    366 	kprintf(fmt, TOLOG, NULL, NULL, ap);
    367 	va_end(ap);
    368 	if (!log_open) {
    369 		va_start(ap, fmt);
    370 		kprintf(fmt, TOCONS, NULL, NULL, ap);
    371 		va_end(ap);
    372 	}
    373 
    374 	kprintf_unlock();
    375 
    376 	logwakeup();		/* wake up anyone waiting for log msgs */
    377 }
    378 
    379 /*
    380  * vlog: write to the log buffer [already have va_list]
    381  */
    382 
    383 void
    384 vlog(int level, const char *fmt, va_list ap)
    385 {
    386 	va_list cap;
    387 
    388 	va_copy(cap, ap);
    389 	kprintf_lock();
    390 
    391 	klogpri(level);		/* log the level first */
    392 	kprintf(fmt, TOLOG, NULL, NULL, ap);
    393 	if (!log_open)
    394 		kprintf(fmt, TOCONS, NULL, NULL, cap);
    395 
    396 	kprintf_unlock();
    397 	va_end(cap);
    398 
    399 	logwakeup();		/* wake up anyone waiting for log msgs */
    400 }
    401 
    402 /*
    403  * logpri: log the priority level to the klog
    404  */
    405 
    406 void
    407 logpri(int level)
    408 {
    409 
    410 	kprintf_lock();
    411 	klogpri(level);
    412 	kprintf_unlock();
    413 }
    414 
    415 /*
    416  * Note: we must be in the mutex here!
    417  */
    418 void
    419 klogpri(int level)
    420 {
    421 	KASSERT((level & KLOG_PRI) == 0);
    422 
    423 	putchar(level | KLOG_PRI, TOLOG, NULL);
    424 }
    425 
    426 /*
    427  * addlog: add info to previous log message
    428  */
    429 
    430 void
    431 addlog(const char *fmt, ...)
    432 {
    433 	va_list ap;
    434 
    435 	kprintf_lock();
    436 
    437 	va_start(ap, fmt);
    438 	kprintf(fmt, TOLOG, NULL, NULL, ap);
    439 	va_end(ap);
    440 	if (!log_open) {
    441 		va_start(ap, fmt);
    442 		kprintf(fmt, TOCONS, NULL, NULL, ap);
    443 		va_end(ap);
    444 	}
    445 
    446 	kprintf_unlock();
    447 
    448 	logwakeup();
    449 }
    450 
    451 static void
    452 putone(int c, int flags, struct tty *tp)
    453 {
    454 	if (panicstr)
    455 		constty = NULL;
    456 
    457 	if ((flags & TOCONS) && tp == NULL && constty) {
    458 		tp = constty;
    459 		flags |= TOTTY;
    460 	}
    461 	if ((flags & TOTTY) && tp &&
    462 	    tputchar(c, flags, tp) < 0 &&
    463 	    (flags & TOCONS) && tp == constty)
    464 		constty = NULL;
    465 	if ((flags & TOLOG) &&
    466 	    c != '\0' && c != '\r' && c != 0177)
    467 	    	logputchar(c);
    468 	if ((flags & TOCONS) && constty == NULL && c != '\0')
    469 		(*v_putc)(c);
    470 }
    471 
    472 static void
    473 putlogpri(int level)
    474 {
    475 	char *p;
    476 	char snbuf[KPRINTF_BUFSIZE];
    477 
    478 	putone('<', TOLOG, NULL);
    479 	snprintf(snbuf, sizeof(snbuf), "%d", level);
    480 	for (p = snbuf ; *p ; p++)
    481 		putone(*p, TOLOG, NULL);
    482 	putone('>', TOLOG, NULL);
    483 }
    484 
    485 #ifndef KLOG_NOTIMESTAMP
    486 static int needtstamp = 1;
    487 
    488 static void
    489 addtstamp(int flags, struct tty *tp)
    490 {
    491 	char buf[64];
    492 	struct timespec ts;
    493 	int n;
    494 
    495 	getnanouptime(&ts);
    496 	n = snprintf(buf, sizeof(buf), "[% 9jd.%.9ld] ",
    497 	    (intmax_t)ts.tv_sec, ts.tv_nsec);
    498 
    499 	for (int i = 0; i < n; i++)
    500 		putone(buf[i], flags, tp);
    501 }
    502 #endif
    503 
    504 /*
    505  * putchar: print a single character on console or user terminal.
    506  *
    507  * => if console, then the last MSGBUFS chars are saved in msgbuf
    508  *	for inspection later (e.g. dmesg/syslog)
    509  * => we must already be in the mutex!
    510  */
    511 static void
    512 putchar(int c, int flags, struct tty *tp)
    513 {
    514 	if (c & KLOG_PRI) {
    515 		putlogpri(c & ~KLOG_PRI);
    516 		return;
    517 	}
    518 
    519 #ifndef KLOG_NOTIMESTAMP
    520 	if (needtstamp) {
    521 		addtstamp(flags, tp);
    522 		needtstamp = 0;
    523 	}
    524 
    525 	if (c == '\n')
    526 		needtstamp++;
    527 #endif
    528 	putone(c, flags, tp);
    529 
    530 #ifdef DDB
    531 	if (flags & TODDB) {
    532 		db_putchar(c);
    533 		return;
    534 	}
    535 #endif
    536 
    537 #ifdef RND_PRINTF
    538 	if (__predict_true(kprintf_inited)) {
    539 		static uint8_t rbuf[SHA512_BLOCK_LENGTH];
    540 		static int cursor;
    541 
    542 		rbuf[cursor] = c;
    543 		if (cursor == sizeof(rbuf) - 1) {
    544 			SHA512_Update(&kprnd_sha, rbuf, sizeof(rbuf));
    545 			kprnd_added++;
    546 			cursor = 0;
    547 		} else {
    548 			cursor++;
    549 		}
    550 	}
    551 #endif
    552 }
    553 
    554 /*
    555  * tablefull: warn that a system table is full
    556  */
    557 
    558 void
    559 tablefull(const char *tab, const char *hint)
    560 {
    561 	if (hint)
    562 		log(LOG_ERR, "%s: table is full - %s\n", tab, hint);
    563 	else
    564 		log(LOG_ERR, "%s: table is full\n", tab);
    565 }
    566 
    567 
    568 /*
    569  * uprintf: print to the controlling tty of the current process
    570  *
    571  * => we may block if the tty queue is full
    572  * => no message is printed if the queue doesn't clear in a reasonable
    573  *	time
    574  */
    575 
    576 void
    577 uprintf(const char *fmt, ...)
    578 {
    579 	struct proc *p = curproc;
    580 	va_list ap;
    581 
    582 	/* mutex_enter(proc_lock); XXXSMP */
    583 
    584 	if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) {
    585 		/* No mutex needed; going to process TTY. */
    586 		va_start(ap, fmt);
    587 		kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap);
    588 		va_end(ap);
    589 	}
    590 
    591 	/* mutex_exit(proc_lock); XXXSMP */
    592 }
    593 
    594 void
    595 uprintf_locked(const char *fmt, ...)
    596 {
    597 	struct proc *p = curproc;
    598 	va_list ap;
    599 
    600 	if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) {
    601 		/* No mutex needed; going to process TTY. */
    602 		va_start(ap, fmt);
    603 		kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap);
    604 		va_end(ap);
    605 	}
    606 }
    607 
    608 /*
    609  * tprintf functions: used to send messages to a specific process
    610  *
    611  * usage:
    612  *   get a tpr_t handle on a process "p" by using "tprintf_open(p)"
    613  *   use the handle when calling "tprintf"
    614  *   when done, do a "tprintf_close" to drop the handle
    615  */
    616 
    617 /*
    618  * tprintf_open: get a tprintf handle on a process "p"
    619  *
    620  * => returns NULL if process can't be printed to
    621  */
    622 
    623 tpr_t
    624 tprintf_open(struct proc *p)
    625 {
    626 	tpr_t cookie;
    627 
    628 	cookie = NULL;
    629 
    630 	mutex_enter(proc_lock);
    631 	if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) {
    632 		proc_sesshold(p->p_session);
    633 		cookie = (tpr_t)p->p_session;
    634 	}
    635 	mutex_exit(proc_lock);
    636 
    637 	return cookie;
    638 }
    639 
    640 /*
    641  * tprintf_close: dispose of a tprintf handle obtained with tprintf_open
    642  */
    643 
    644 void
    645 tprintf_close(tpr_t sess)
    646 {
    647 
    648 	if (sess) {
    649 		mutex_enter(proc_lock);
    650 		/* Releases proc_lock. */
    651 		proc_sessrele((struct session *)sess);
    652 	}
    653 }
    654 
    655 /*
    656  * tprintf: given tprintf handle to a process [obtained with tprintf_open],
    657  * send a message to the controlling tty for that process.
    658  *
    659  * => also sends message to /dev/klog
    660  */
    661 void
    662 tprintf(tpr_t tpr, const char *fmt, ...)
    663 {
    664 	struct session *sess = (struct session *)tpr;
    665 	struct tty *tp = NULL;
    666 	int flags = TOLOG;
    667 	va_list ap;
    668 
    669 	/* mutex_enter(proc_lock); XXXSMP */
    670 	if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
    671 		flags |= TOTTY;
    672 		tp = sess->s_ttyp;
    673 	}
    674 
    675 	kprintf_lock();
    676 
    677 	klogpri(LOG_INFO);
    678 	va_start(ap, fmt);
    679 	kprintf(fmt, flags, tp, NULL, ap);
    680 	va_end(ap);
    681 
    682 	kprintf_unlock();
    683 	/* mutex_exit(proc_lock);	XXXSMP */
    684 
    685 	logwakeup();
    686 }
    687 
    688 
    689 /*
    690  * ttyprintf: send a message to a specific tty
    691  *
    692  * => should be used only by tty driver or anything that knows the
    693  *    underlying tty will not be revoked(2)'d away.  [otherwise,
    694  *    use tprintf]
    695  */
    696 void
    697 ttyprintf(struct tty *tp, const char *fmt, ...)
    698 {
    699 	va_list ap;
    700 
    701 	/* No mutex needed; going to process TTY. */
    702 	va_start(ap, fmt);
    703 	kprintf(fmt, TOTTY, tp, NULL, ap);
    704 	va_end(ap);
    705 }
    706 
    707 #ifdef DDB
    708 
    709 /*
    710  * db_printf: printf for DDB (via db_putchar)
    711  */
    712 
    713 void
    714 db_printf(const char *fmt, ...)
    715 {
    716 	va_list ap;
    717 
    718 	/* No mutex needed; DDB pauses all processors. */
    719 	va_start(ap, fmt);
    720 	kprintf(fmt, TODDB, NULL, NULL, ap);
    721 	va_end(ap);
    722 
    723 	if (db_tee_msgbuf) {
    724 		va_start(ap, fmt);
    725 		kprintf(fmt, TOLOG, NULL, NULL, ap);
    726 		va_end(ap);
    727 	}
    728 }
    729 
    730 void
    731 db_vprintf(const char *fmt, va_list ap)
    732 {
    733 	va_list cap;
    734 
    735 	va_copy(cap, ap);
    736 	/* No mutex needed; DDB pauses all processors. */
    737 	kprintf(fmt, TODDB, NULL, NULL, ap);
    738 	if (db_tee_msgbuf)
    739 		kprintf(fmt, TOLOG, NULL, NULL, cap);
    740 	va_end(cap);
    741 }
    742 
    743 #endif /* DDB */
    744 
    745 static void
    746 kprintf_internal(const char *fmt, int oflags, void *vp, char *sbuf, ...)
    747 {
    748 	va_list ap;
    749 
    750 	va_start(ap, sbuf);
    751 	(void)kprintf(fmt, oflags, vp, sbuf, ap);
    752 	va_end(ap);
    753 }
    754 
    755 /*
    756  * Device autoconfiguration printf routines.  These change their
    757  * behavior based on the AB_* flags in boothowto.  If AB_SILENT
    758  * is set, messages never go to the console (but they still always
    759  * go to the log).  AB_VERBOSE overrides AB_SILENT.
    760  */
    761 
    762 /*
    763  * aprint_normal: Send to console unless AB_QUIET.  Always goes
    764  * to the log.
    765  */
    766 static void
    767 aprint_normal_internal(const char *prefix, const char *fmt, va_list ap)
    768 {
    769 	int flags = TOLOG;
    770 
    771 	if ((boothowto & (AB_SILENT|AB_QUIET)) == 0 ||
    772 	    (boothowto & AB_VERBOSE) != 0)
    773 		flags |= TOCONS;
    774 
    775 	kprintf_lock();
    776 
    777 	if (prefix)
    778 		kprintf_internal("%s: ", flags, NULL, NULL, prefix);
    779 	kprintf(fmt, flags, NULL, NULL, ap);
    780 
    781 	kprintf_unlock();
    782 
    783 	if (!panicstr)
    784 		logwakeup();
    785 }
    786 
    787 void
    788 aprint_normal(const char *fmt, ...)
    789 {
    790 	va_list ap;
    791 
    792 	va_start(ap, fmt);
    793 	aprint_normal_internal(NULL, fmt, ap);
    794 	va_end(ap);
    795 }
    796 
    797 void
    798 aprint_normal_dev(device_t dv, const char *fmt, ...)
    799 {
    800 	va_list ap;
    801 
    802 	va_start(ap, fmt);
    803 	aprint_normal_internal(device_xname(dv), fmt, ap);
    804 	va_end(ap);
    805 }
    806 
    807 void
    808 aprint_normal_ifnet(struct ifnet *ifp, const char *fmt, ...)
    809 {
    810 	va_list ap;
    811 
    812 	va_start(ap, fmt);
    813 	aprint_normal_internal(ifp->if_xname, fmt, ap);
    814 	va_end(ap);
    815 }
    816 
    817 /*
    818  * aprint_error: Send to console unless AB_QUIET.  Always goes
    819  * to the log.  Also counts the number of times called so other
    820  * parts of the kernel can report the number of errors during a
    821  * given phase of system startup.
    822  */
    823 static int aprint_error_count;
    824 
    825 int
    826 aprint_get_error_count(void)
    827 {
    828 	int count;
    829 
    830 	kprintf_lock();
    831 
    832 	count = aprint_error_count;
    833 	aprint_error_count = 0;
    834 
    835 	kprintf_unlock();
    836 
    837 	return (count);
    838 }
    839 
    840 static void
    841 aprint_error_internal(const char *prefix, const char *fmt, va_list ap)
    842 {
    843 	int flags = TOLOG;
    844 
    845 	if ((boothowto & (AB_SILENT|AB_QUIET)) == 0 ||
    846 	    (boothowto & AB_VERBOSE) != 0)
    847 		flags |= TOCONS;
    848 
    849 	kprintf_lock();
    850 
    851 	aprint_error_count++;
    852 
    853 	if (prefix)
    854 		kprintf_internal("%s: ", flags, NULL, NULL, prefix);
    855 	kprintf(fmt, flags, NULL, NULL, ap);
    856 
    857 	kprintf_unlock();
    858 
    859 	if (!panicstr)
    860 		logwakeup();
    861 }
    862 
    863 void
    864 aprint_error(const char *fmt, ...)
    865 {
    866 	va_list ap;
    867 
    868 	va_start(ap, fmt);
    869 	aprint_error_internal(NULL, fmt, ap);
    870 	va_end(ap);
    871 }
    872 
    873 void
    874 aprint_error_dev(device_t dv, const char *fmt, ...)
    875 {
    876 	va_list ap;
    877 
    878 	va_start(ap, fmt);
    879 	aprint_error_internal(device_xname(dv), fmt, ap);
    880 	va_end(ap);
    881 }
    882 
    883 void
    884 aprint_error_ifnet(struct ifnet *ifp, const char *fmt, ...)
    885 {
    886 	va_list ap;
    887 
    888 	va_start(ap, fmt);
    889 	aprint_error_internal(ifp->if_xname, fmt, ap);
    890 	va_end(ap);
    891 }
    892 
    893 /*
    894  * aprint_naive: Send to console only if AB_QUIET.  Never goes
    895  * to the log.
    896  */
    897 static void
    898 aprint_naive_internal(const char *prefix, const char *fmt, va_list ap)
    899 {
    900 	if ((boothowto & (AB_QUIET|AB_SILENT|AB_VERBOSE)) != AB_QUIET)
    901 		return;
    902 
    903 	kprintf_lock();
    904 
    905 	if (prefix)
    906 		kprintf_internal("%s: ", TOCONS, NULL, NULL, prefix);
    907 	kprintf(fmt, TOCONS, NULL, NULL, ap);
    908 
    909 	kprintf_unlock();
    910 }
    911 
    912 void
    913 aprint_naive(const char *fmt, ...)
    914 {
    915 	va_list ap;
    916 
    917 	va_start(ap, fmt);
    918 	aprint_naive_internal(NULL, fmt, ap);
    919 	va_end(ap);
    920 }
    921 
    922 void
    923 aprint_naive_dev(device_t dv, const char *fmt, ...)
    924 {
    925 	va_list ap;
    926 
    927 	va_start(ap, fmt);
    928 	aprint_naive_internal(device_xname(dv), fmt, ap);
    929 	va_end(ap);
    930 }
    931 
    932 void
    933 aprint_naive_ifnet(struct ifnet *ifp, const char *fmt, ...)
    934 {
    935 	va_list ap;
    936 
    937 	va_start(ap, fmt);
    938 	aprint_naive_internal(ifp->if_xname, fmt, ap);
    939 	va_end(ap);
    940 }
    941 
    942 /*
    943  * aprint_verbose: Send to console only if AB_VERBOSE.  Always
    944  * goes to the log.
    945  */
    946 static void
    947 aprint_verbose_internal(const char *prefix, const char *fmt, va_list ap)
    948 {
    949 	int flags = TOLOG;
    950 
    951 	if (boothowto & AB_VERBOSE)
    952 		flags |= TOCONS;
    953 
    954 	kprintf_lock();
    955 
    956 	if (prefix)
    957 		kprintf_internal("%s: ", flags, NULL, NULL, prefix);
    958 	kprintf(fmt, flags, NULL, NULL, ap);
    959 
    960 	kprintf_unlock();
    961 
    962 	if (!panicstr)
    963 		logwakeup();
    964 }
    965 
    966 void
    967 aprint_verbose(const char *fmt, ...)
    968 {
    969 	va_list ap;
    970 
    971 	va_start(ap, fmt);
    972 	aprint_verbose_internal(NULL, fmt, ap);
    973 	va_end(ap);
    974 }
    975 
    976 void
    977 aprint_verbose_dev(device_t dv, const char *fmt, ...)
    978 {
    979 	va_list ap;
    980 
    981 	va_start(ap, fmt);
    982 	aprint_verbose_internal(device_xname(dv), fmt, ap);
    983 	va_end(ap);
    984 }
    985 
    986 void
    987 aprint_verbose_ifnet(struct ifnet *ifp, const char *fmt, ...)
    988 {
    989 	va_list ap;
    990 
    991 	va_start(ap, fmt);
    992 	aprint_verbose_internal(ifp->if_xname, fmt, ap);
    993 	va_end(ap);
    994 }
    995 
    996 /*
    997  * aprint_debug: Send to console and log only if AB_DEBUG.
    998  */
    999 static void
   1000 aprint_debug_internal(const char *prefix, const char *fmt, va_list ap)
   1001 {
   1002 	if ((boothowto & AB_DEBUG) == 0)
   1003 		return;
   1004 
   1005 	kprintf_lock();
   1006 
   1007 	if (prefix)
   1008 		kprintf_internal("%s: ", TOCONS | TOLOG, NULL, NULL, prefix);
   1009 	kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
   1010 
   1011 	kprintf_unlock();
   1012 }
   1013 
   1014 void
   1015 aprint_debug(const char *fmt, ...)
   1016 {
   1017 	va_list ap;
   1018 
   1019 	va_start(ap, fmt);
   1020 	aprint_debug_internal(NULL, fmt, ap);
   1021 	va_end(ap);
   1022 }
   1023 
   1024 void
   1025 aprint_debug_dev(device_t dv, const char *fmt, ...)
   1026 {
   1027 	va_list ap;
   1028 
   1029 	va_start(ap, fmt);
   1030 	aprint_debug_internal(device_xname(dv), fmt, ap);
   1031 	va_end(ap);
   1032 }
   1033 
   1034 void
   1035 aprint_debug_ifnet(struct ifnet *ifp, const char *fmt, ...)
   1036 {
   1037 	va_list ap;
   1038 
   1039 	va_start(ap, fmt);
   1040 	aprint_debug_internal(ifp->if_xname, fmt, ap);
   1041 	va_end(ap);
   1042 }
   1043 
   1044 void
   1045 printf_tolog(const char *fmt, ...)
   1046 {
   1047 	va_list ap;
   1048 
   1049 	kprintf_lock();
   1050 
   1051 	va_start(ap, fmt);
   1052 	kprintf(fmt, TOLOG, NULL, NULL, ap);
   1053 	va_end(ap);
   1054 
   1055 	kprintf_unlock();
   1056 }
   1057 
   1058 /*
   1059  * printf_nolog: Like printf(), but does not send message to the log.
   1060  */
   1061 
   1062 void
   1063 printf_nolog(const char *fmt, ...)
   1064 {
   1065 	va_list ap;
   1066 
   1067 	kprintf_lock();
   1068 
   1069 	va_start(ap, fmt);
   1070 	kprintf(fmt, TOCONS, NULL, NULL, ap);
   1071 	va_end(ap);
   1072 
   1073 	kprintf_unlock();
   1074 }
   1075 
   1076 /*
   1077  * normal kernel printf functions: printf, vprintf, snprintf, vsnprintf
   1078  */
   1079 
   1080 /*
   1081  * printf: print a message to the console and the log
   1082  */
   1083 void
   1084 printf(const char *fmt, ...)
   1085 {
   1086 	va_list ap;
   1087 
   1088 	kprintf_lock();
   1089 
   1090 	va_start(ap, fmt);
   1091 	kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
   1092 	va_end(ap);
   1093 
   1094 	kprintf_unlock();
   1095 
   1096 	if (!panicstr)
   1097 		logwakeup();
   1098 }
   1099 
   1100 /*
   1101  * vprintf: print a message to the console and the log [already have
   1102  *	va_list]
   1103  */
   1104 
   1105 void
   1106 vprintf(const char *fmt, va_list ap)
   1107 {
   1108 	kprintf_lock();
   1109 
   1110 	kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
   1111 
   1112 	kprintf_unlock();
   1113 
   1114 	if (!panicstr)
   1115 		logwakeup();
   1116 }
   1117 
   1118 /*
   1119  * snprintf: print a message to a buffer
   1120  */
   1121 int
   1122 snprintf(char *bf, size_t size, const char *fmt, ...)
   1123 {
   1124 	int retval;
   1125 	va_list ap;
   1126 
   1127 	va_start(ap, fmt);
   1128 	retval = vsnprintf(bf, size, fmt, ap);
   1129 	va_end(ap);
   1130 
   1131 	return retval;
   1132 }
   1133 
   1134 /*
   1135  * vsnprintf: print a message to a buffer [already have va_list]
   1136  */
   1137 int
   1138 vsnprintf(char *bf, size_t size, const char *fmt, va_list ap)
   1139 {
   1140 	int retval;
   1141 	char *p;
   1142 
   1143 	p = bf + size;
   1144 	retval = kprintf(fmt, TOBUFONLY, &p, bf, ap);
   1145 	if (bf && size > 0) {
   1146 		/* nul terminate */
   1147 		if (size <= (size_t)retval)
   1148 			bf[size - 1] = '\0';
   1149 		else
   1150 			bf[retval] = '\0';
   1151 	}
   1152 	return retval;
   1153 }
   1154 
   1155 /*
   1156  * kprintf: scaled down version of printf(3).
   1157  *
   1158  * this version based on vfprintf() from libc which was derived from
   1159  * software contributed to Berkeley by Chris Torek.
   1160  *
   1161  * NOTE: The kprintf mutex must be held if we're going TOBUF or TOCONS!
   1162  */
   1163 
   1164 /*
   1165  * macros for converting digits to letters and vice versa
   1166  */
   1167 #define	to_digit(c)	((c) - '0')
   1168 #define is_digit(c)	((unsigned)to_digit(c) <= 9)
   1169 #define	to_char(n)	((n) + '0')
   1170 
   1171 /*
   1172  * flags used during conversion.
   1173  */
   1174 #define	ALT		0x001		/* alternate form */
   1175 #define	HEXPREFIX	0x002		/* add 0x or 0X prefix */
   1176 #define	LADJUST		0x004		/* left adjustment */
   1177 #define	LONGDBL		0x008		/* long double; unimplemented */
   1178 #define	LONGINT		0x010		/* long integer */
   1179 #define	QUADINT		0x020		/* quad integer */
   1180 #define	SHORTINT	0x040		/* short integer */
   1181 #define	MAXINT		0x080		/* intmax_t */
   1182 #define	PTRINT		0x100		/* intptr_t */
   1183 #define	SIZEINT		0x200		/* size_t */
   1184 #define	ZEROPAD		0x400		/* zero (as opposed to blank) pad */
   1185 #define FPT		0x800		/* Floating point number */
   1186 
   1187 	/*
   1188 	 * To extend shorts properly, we need both signed and unsigned
   1189 	 * argument extraction methods.
   1190 	 */
   1191 #define	SARG() \
   1192 	(flags&MAXINT ? va_arg(ap, intmax_t) : \
   1193 	    flags&PTRINT ? va_arg(ap, intptr_t) : \
   1194 	    flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
   1195 	    flags&QUADINT ? va_arg(ap, quad_t) : \
   1196 	    flags&LONGINT ? va_arg(ap, long) : \
   1197 	    flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
   1198 	    (long)va_arg(ap, int))
   1199 #define	UARG() \
   1200 	(flags&MAXINT ? va_arg(ap, uintmax_t) : \
   1201 	    flags&PTRINT ? va_arg(ap, uintptr_t) : \
   1202 	    flags&SIZEINT ? va_arg(ap, size_t) : \
   1203 	    flags&QUADINT ? va_arg(ap, u_quad_t) : \
   1204 	    flags&LONGINT ? va_arg(ap, u_long) : \
   1205 	    flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
   1206 	    (u_long)va_arg(ap, u_int))
   1207 
   1208 #define KPRINTF_PUTCHAR(C) {						\
   1209 	if (oflags == TOBUFONLY) {					\
   1210 		if (sbuf && ((vp == NULL) || (sbuf < tailp))) 		\
   1211 			*sbuf++ = (C);					\
   1212 	} else {							\
   1213 		putchar((C), oflags, vp);				\
   1214 	}								\
   1215 }
   1216 
   1217 void
   1218 device_printf(device_t dev, const char *fmt, ...)
   1219 {
   1220 	va_list ap;
   1221 
   1222 	va_start(ap, fmt);
   1223 	printf("%s: ", device_xname(dev));
   1224 	vprintf(fmt, ap);
   1225 	va_end(ap);
   1226 	return;
   1227 }
   1228 
   1229 /*
   1230  * Guts of kernel printf.  Note, we already expect to be in a mutex!
   1231  */
   1232 int
   1233 kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap)
   1234 {
   1235 	const char *fmt;	/* format string */
   1236 	int ch;			/* character from fmt */
   1237 	int n;			/* handy integer (short term usage) */
   1238 	char *cp;		/* handy char pointer (short term usage) */
   1239 	int flags;		/* flags as above */
   1240 	int ret;		/* return value accumulator */
   1241 	int width;		/* width from format (%8d), or 0 */
   1242 	int prec;		/* precision from format (%.3d), or -1 */
   1243 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
   1244 
   1245 	u_quad_t _uquad;	/* integer arguments %[diouxX] */
   1246 	enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
   1247 	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
   1248 	int realsz;		/* field size expanded by dprec */
   1249 	int size;		/* size of converted field or string */
   1250 	const char *xdigs;	/* digits for [xX] conversion */
   1251 	char bf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
   1252 	char *tailp;		/* tail pointer for snprintf */
   1253 
   1254 	if (oflags == TOBUFONLY && (vp != NULL))
   1255 		tailp = *(char **)vp;
   1256 	else
   1257 		tailp = NULL;
   1258 
   1259 	cp = NULL;	/* XXX: shutup gcc */
   1260 	size = 0;	/* XXX: shutup gcc */
   1261 
   1262 	fmt = fmt0;
   1263 	ret = 0;
   1264 
   1265 	xdigs = NULL;		/* XXX: shut up gcc warning */
   1266 
   1267 	/*
   1268 	 * Scan the format for conversions (`%' character).
   1269 	 */
   1270 	for (;;) {
   1271 		for (; *fmt != '%' && *fmt; fmt++) {
   1272 			ret++;
   1273 			KPRINTF_PUTCHAR(*fmt);
   1274 		}
   1275 		if (*fmt == 0)
   1276 			goto done;
   1277 
   1278 		fmt++;		/* skip over '%' */
   1279 
   1280 		flags = 0;
   1281 		dprec = 0;
   1282 		width = 0;
   1283 		prec = -1;
   1284 		sign = '\0';
   1285 
   1286 rflag:		ch = *fmt++;
   1287 reswitch:	switch (ch) {
   1288 		case ' ':
   1289 			/*
   1290 			 * ``If the space and + flags both appear, the space
   1291 			 * flag will be ignored.''
   1292 			 *	-- ANSI X3J11
   1293 			 */
   1294 			if (!sign)
   1295 				sign = ' ';
   1296 			goto rflag;
   1297 		case '#':
   1298 			flags |= ALT;
   1299 			goto rflag;
   1300 		case '*':
   1301 			/*
   1302 			 * ``A negative field width argument is taken as a
   1303 			 * - flag followed by a positive field width.''
   1304 			 *	-- ANSI X3J11
   1305 			 * They don't exclude field widths read from args.
   1306 			 */
   1307 			if ((width = va_arg(ap, int)) >= 0)
   1308 				goto rflag;
   1309 			width = -width;
   1310 			/* FALLTHROUGH */
   1311 		case '-':
   1312 			flags |= LADJUST;
   1313 			goto rflag;
   1314 		case '+':
   1315 			sign = '+';
   1316 			goto rflag;
   1317 		case '.':
   1318 			if ((ch = *fmt++) == '*') {
   1319 				n = va_arg(ap, int);
   1320 				prec = n < 0 ? -1 : n;
   1321 				goto rflag;
   1322 			}
   1323 			n = 0;
   1324 			while (is_digit(ch)) {
   1325 				n = 10 * n + to_digit(ch);
   1326 				ch = *fmt++;
   1327 			}
   1328 			prec = n < 0 ? -1 : n;
   1329 			goto reswitch;
   1330 		case '0':
   1331 			/*
   1332 			 * ``Note that 0 is taken as a flag, not as the
   1333 			 * beginning of a field width.''
   1334 			 *	-- ANSI X3J11
   1335 			 */
   1336 			flags |= ZEROPAD;
   1337 			goto rflag;
   1338 		case '1': case '2': case '3': case '4':
   1339 		case '5': case '6': case '7': case '8': case '9':
   1340 			n = 0;
   1341 			do {
   1342 				n = 10 * n + to_digit(ch);
   1343 				ch = *fmt++;
   1344 			} while (is_digit(ch));
   1345 			width = n;
   1346 			goto reswitch;
   1347 		case 'h':
   1348 			flags |= SHORTINT;
   1349 			goto rflag;
   1350 		case 'j':
   1351 			flags |= MAXINT;
   1352 			goto rflag;
   1353 		case 'l':
   1354 			if (*fmt == 'l') {
   1355 				fmt++;
   1356 				flags |= QUADINT;
   1357 			} else {
   1358 				flags |= LONGINT;
   1359 			}
   1360 			goto rflag;
   1361 		case 'q':
   1362 			flags |= QUADINT;
   1363 			goto rflag;
   1364 		case 't':
   1365 			flags |= PTRINT;
   1366 			goto rflag;
   1367 		case 'z':
   1368 			flags |= SIZEINT;
   1369 			goto rflag;
   1370 		case 'c':
   1371 			*(cp = bf) = va_arg(ap, int);
   1372 			size = 1;
   1373 			sign = '\0';
   1374 			break;
   1375 		case 'D':
   1376 			flags |= LONGINT;
   1377 			/*FALLTHROUGH*/
   1378 		case 'd':
   1379 		case 'i':
   1380 			_uquad = SARG();
   1381 			if ((quad_t)_uquad < 0) {
   1382 				_uquad = -_uquad;
   1383 				sign = '-';
   1384 			}
   1385 			base = DEC;
   1386 			goto number;
   1387 		case 'n':
   1388 			if (flags & MAXINT)
   1389 				*va_arg(ap, intmax_t *) = ret;
   1390 			else if (flags & PTRINT)
   1391 				*va_arg(ap, intptr_t *) = ret;
   1392 			else if (flags & SIZEINT)
   1393 				*va_arg(ap, ssize_t *) = ret;
   1394 			else if (flags & QUADINT)
   1395 				*va_arg(ap, quad_t *) = ret;
   1396 			else if (flags & LONGINT)
   1397 				*va_arg(ap, long *) = ret;
   1398 			else if (flags & SHORTINT)
   1399 				*va_arg(ap, short *) = ret;
   1400 			else
   1401 				*va_arg(ap, int *) = ret;
   1402 			continue;	/* no output */
   1403 		case 'O':
   1404 			flags |= LONGINT;
   1405 			/*FALLTHROUGH*/
   1406 		case 'o':
   1407 			_uquad = UARG();
   1408 			base = OCT;
   1409 			goto nosign;
   1410 		case 'p':
   1411 			/*
   1412 			 * ``The argument shall be a pointer to void.  The
   1413 			 * value of the pointer is converted to a sequence
   1414 			 * of printable characters, in an implementation-
   1415 			 * defined manner.''
   1416 			 *	-- ANSI X3J11
   1417 			 */
   1418 			/* NOSTRICT */
   1419 			_uquad = (u_long)va_arg(ap, void *);
   1420 			base = HEX;
   1421 			xdigs = hexdigits;
   1422 			flags |= HEXPREFIX;
   1423 			ch = 'x';
   1424 			goto nosign;
   1425 		case 's':
   1426 			if ((cp = va_arg(ap, char *)) == NULL)
   1427 				/*XXXUNCONST*/
   1428 				cp = __UNCONST("(null)");
   1429 			if (prec >= 0) {
   1430 				/*
   1431 				 * can't use strlen; can only look for the
   1432 				 * NUL in the first `prec' characters, and
   1433 				 * strlen() will go further.
   1434 				 */
   1435 				char *p = memchr(cp, 0, prec);
   1436 
   1437 				if (p != NULL) {
   1438 					size = p - cp;
   1439 					if (size > prec)
   1440 						size = prec;
   1441 				} else
   1442 					size = prec;
   1443 			} else
   1444 				size = strlen(cp);
   1445 			sign = '\0';
   1446 			break;
   1447 		case 'U':
   1448 			flags |= LONGINT;
   1449 			/*FALLTHROUGH*/
   1450 		case 'u':
   1451 			_uquad = UARG();
   1452 			base = DEC;
   1453 			goto nosign;
   1454 		case 'X':
   1455 			xdigs = HEXDIGITS;
   1456 			goto hex;
   1457 		case 'x':
   1458 			xdigs = hexdigits;
   1459 hex:			_uquad = UARG();
   1460 			base = HEX;
   1461 			/* leading 0x/X only if non-zero */
   1462 			if (flags & ALT && _uquad != 0)
   1463 				flags |= HEXPREFIX;
   1464 
   1465 			/* unsigned conversions */
   1466 nosign:			sign = '\0';
   1467 			/*
   1468 			 * ``... diouXx conversions ... if a precision is
   1469 			 * specified, the 0 flag will be ignored.''
   1470 			 *	-- ANSI X3J11
   1471 			 */
   1472 number:			if ((dprec = prec) >= 0)
   1473 				flags &= ~ZEROPAD;
   1474 
   1475 			/*
   1476 			 * ``The result of converting a zero value with an
   1477 			 * explicit precision of zero is no characters.''
   1478 			 *	-- ANSI X3J11
   1479 			 */
   1480 			cp = bf + KPRINTF_BUFSIZE;
   1481 			if (_uquad != 0 || prec != 0) {
   1482 				/*
   1483 				 * Unsigned mod is hard, and unsigned mod
   1484 				 * by a constant is easier than that by
   1485 				 * a variable; hence this switch.
   1486 				 */
   1487 				switch (base) {
   1488 				case OCT:
   1489 					do {
   1490 						*--cp = to_char(_uquad & 7);
   1491 						_uquad >>= 3;
   1492 					} while (_uquad);
   1493 					/* handle octal leading 0 */
   1494 					if (flags & ALT && *cp != '0')
   1495 						*--cp = '0';
   1496 					break;
   1497 
   1498 				case DEC:
   1499 					/* many numbers are 1 digit */
   1500 					while (_uquad >= 10) {
   1501 						*--cp = to_char(_uquad % 10);
   1502 						_uquad /= 10;
   1503 					}
   1504 					*--cp = to_char(_uquad);
   1505 					break;
   1506 
   1507 				case HEX:
   1508 					do {
   1509 						*--cp = xdigs[_uquad & 15];
   1510 						_uquad >>= 4;
   1511 					} while (_uquad);
   1512 					break;
   1513 
   1514 				default:
   1515 					/*XXXUNCONST*/
   1516 					cp = __UNCONST("bug in kprintf: bad base");
   1517 					size = strlen(cp);
   1518 					goto skipsize;
   1519 				}
   1520 			}
   1521 			size = bf + KPRINTF_BUFSIZE - cp;
   1522 		skipsize:
   1523 			break;
   1524 		default:	/* "%?" prints ?, unless ? is NUL */
   1525 			if (ch == '\0')
   1526 				goto done;
   1527 			/* pretend it was %c with argument ch */
   1528 			cp = bf;
   1529 			*cp = ch;
   1530 			size = 1;
   1531 			sign = '\0';
   1532 			break;
   1533 		}
   1534 
   1535 		/*
   1536 		 * All reasonable formats wind up here.  At this point, `cp'
   1537 		 * points to a string which (if not flags&LADJUST) should be
   1538 		 * padded out to `width' places.  If flags&ZEROPAD, it should
   1539 		 * first be prefixed by any sign or other prefix; otherwise,
   1540 		 * it should be blank padded before the prefix is emitted.
   1541 		 * After any left-hand padding and prefixing, emit zeroes
   1542 		 * required by a decimal [diouxX] precision, then print the
   1543 		 * string proper, then emit zeroes required by any leftover
   1544 		 * floating precision; finally, if LADJUST, pad with blanks.
   1545 		 *
   1546 		 * Compute actual size, so we know how much to pad.
   1547 		 * size excludes decimal prec; realsz includes it.
   1548 		 */
   1549 		realsz = dprec > size ? dprec : size;
   1550 		if (sign)
   1551 			realsz++;
   1552 		else if (flags & HEXPREFIX)
   1553 			realsz+= 2;
   1554 
   1555 		/* adjust ret */
   1556 		ret += width > realsz ? width : realsz;
   1557 
   1558 		/* right-adjusting blank padding */
   1559 		if ((flags & (LADJUST|ZEROPAD)) == 0) {
   1560 			n = width - realsz;
   1561 			while (n-- > 0)
   1562 				KPRINTF_PUTCHAR(' ');
   1563 		}
   1564 
   1565 		/* prefix */
   1566 		if (sign) {
   1567 			KPRINTF_PUTCHAR(sign);
   1568 		} else if (flags & HEXPREFIX) {
   1569 			KPRINTF_PUTCHAR('0');
   1570 			KPRINTF_PUTCHAR(ch);
   1571 		}
   1572 
   1573 		/* right-adjusting zero padding */
   1574 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
   1575 			n = width - realsz;
   1576 			while (n-- > 0)
   1577 				KPRINTF_PUTCHAR('0');
   1578 		}
   1579 
   1580 		/* leading zeroes from decimal precision */
   1581 		n = dprec - size;
   1582 		while (n-- > 0)
   1583 			KPRINTF_PUTCHAR('0');
   1584 
   1585 		/* the string or number proper */
   1586 		for (; size--; cp++)
   1587 			KPRINTF_PUTCHAR(*cp);
   1588 		/* left-adjusting padding (always blank) */
   1589 		if (flags & LADJUST) {
   1590 			n = width - realsz;
   1591 			while (n-- > 0)
   1592 				KPRINTF_PUTCHAR(' ');
   1593 		}
   1594 	}
   1595 
   1596 done:
   1597 	if ((oflags == TOBUFONLY) && (vp != NULL))
   1598 		*(char **)vp = sbuf;
   1599 	(*v_flush)();
   1600 
   1601 #ifdef RND_PRINTF
   1602 	if (!cold) {
   1603 		struct timespec ts;
   1604 		(void)nanotime(&ts);
   1605 		SHA512_Update(&kprnd_sha, (char *)&ts, sizeof(ts));
   1606 	}
   1607 #endif
   1608 	return ret;
   1609 }
   1610