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