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