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