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