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