Home | History | Annotate | Line # | Download | only in kern
subr_prf.c revision 1.176
      1 /*	$NetBSD: subr_prf.c,v 1.176 2019/01/14 19:21:54 jdolecek 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.176 2019/01/14 19:21:54 jdolecek 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 	KASSERT(dv != NULL);
    812 
    813 	va_start(ap, fmt);
    814 	aprint_normal_internal(device_xname(dv), fmt, ap);
    815 	va_end(ap);
    816 }
    817 
    818 void
    819 aprint_normal_ifnet(struct ifnet *ifp, const char *fmt, ...)
    820 {
    821 	va_list ap;
    822 
    823 	KASSERT(ifp != NULL);
    824 
    825 	va_start(ap, fmt);
    826 	aprint_normal_internal(ifp->if_xname, fmt, ap);
    827 	va_end(ap);
    828 }
    829 
    830 /*
    831  * aprint_error: Send to console unless AB_QUIET.  Always goes
    832  * to the log.  Also counts the number of times called so other
    833  * parts of the kernel can report the number of errors during a
    834  * given phase of system startup.
    835  */
    836 static int aprint_error_count;
    837 
    838 int
    839 aprint_get_error_count(void)
    840 {
    841 	int count;
    842 
    843 	kprintf_lock();
    844 
    845 	count = aprint_error_count;
    846 	aprint_error_count = 0;
    847 
    848 	kprintf_unlock();
    849 
    850 	return (count);
    851 }
    852 
    853 static void
    854 aprint_error_internal(const char *prefix, const char *fmt, va_list ap)
    855 {
    856 	int flags = TOLOG;
    857 
    858 	if ((boothowto & (AB_SILENT|AB_QUIET)) == 0 ||
    859 	    (boothowto & AB_VERBOSE) != 0)
    860 		flags |= TOCONS;
    861 
    862 	kprintf_lock();
    863 
    864 	aprint_error_count++;
    865 
    866 	if (prefix)
    867 		kprintf_internal("%s: ", flags, NULL, NULL, prefix);
    868 	kprintf_internal("autoconfiguration error: ", TOLOG, NULL, NULL);
    869 	kprintf(fmt, flags, NULL, NULL, ap);
    870 
    871 	kprintf_unlock();
    872 
    873 	if (!panicstr)
    874 		logwakeup();
    875 }
    876 
    877 void
    878 aprint_error(const char *fmt, ...)
    879 {
    880 	va_list ap;
    881 
    882 	va_start(ap, fmt);
    883 	aprint_error_internal(NULL, fmt, ap);
    884 	va_end(ap);
    885 }
    886 
    887 void
    888 aprint_error_dev(device_t dv, const char *fmt, ...)
    889 {
    890 	va_list ap;
    891 
    892 	KASSERT(dv != NULL);
    893 
    894 	va_start(ap, fmt);
    895 	aprint_error_internal(device_xname(dv), fmt, ap);
    896 	va_end(ap);
    897 }
    898 
    899 void
    900 aprint_error_ifnet(struct ifnet *ifp, const char *fmt, ...)
    901 {
    902 	va_list ap;
    903 
    904 	KASSERT(ifp != NULL);
    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 	KASSERT(dv != NULL);
    946 
    947 	va_start(ap, fmt);
    948 	aprint_naive_internal(device_xname(dv), fmt, ap);
    949 	va_end(ap);
    950 }
    951 
    952 void
    953 aprint_naive_ifnet(struct ifnet *ifp, const char *fmt, ...)
    954 {
    955 	va_list ap;
    956 
    957 	KASSERT(ifp != NULL);
    958 
    959 	va_start(ap, fmt);
    960 	aprint_naive_internal(ifp->if_xname, fmt, ap);
    961 	va_end(ap);
    962 }
    963 
    964 /*
    965  * aprint_verbose: Send to console only if AB_VERBOSE.  Always
    966  * goes to the log.
    967  */
    968 static void
    969 aprint_verbose_internal(const char *prefix, const char *fmt, va_list ap)
    970 {
    971 	int flags = TOLOG;
    972 
    973 	if (boothowto & AB_VERBOSE)
    974 		flags |= TOCONS;
    975 
    976 	kprintf_lock();
    977 
    978 	if (prefix)
    979 		kprintf_internal("%s: ", flags, NULL, NULL, prefix);
    980 	kprintf(fmt, flags, NULL, NULL, ap);
    981 
    982 	kprintf_unlock();
    983 
    984 	if (!panicstr)
    985 		logwakeup();
    986 }
    987 
    988 void
    989 aprint_verbose(const char *fmt, ...)
    990 {
    991 	va_list ap;
    992 
    993 	va_start(ap, fmt);
    994 	aprint_verbose_internal(NULL, fmt, ap);
    995 	va_end(ap);
    996 }
    997 
    998 void
    999 aprint_verbose_dev(device_t dv, const char *fmt, ...)
   1000 {
   1001 	va_list ap;
   1002 
   1003 	KASSERT(dv != NULL);
   1004 
   1005 	va_start(ap, fmt);
   1006 	aprint_verbose_internal(device_xname(dv), fmt, ap);
   1007 	va_end(ap);
   1008 }
   1009 
   1010 void
   1011 aprint_verbose_ifnet(struct ifnet *ifp, const char *fmt, ...)
   1012 {
   1013 	va_list ap;
   1014 
   1015 	KASSERT(ifp != NULL);
   1016 
   1017 	va_start(ap, fmt);
   1018 	aprint_verbose_internal(ifp->if_xname, fmt, ap);
   1019 	va_end(ap);
   1020 }
   1021 
   1022 /*
   1023  * aprint_debug: Send to console and log only if AB_DEBUG.
   1024  */
   1025 static void
   1026 aprint_debug_internal(const char *prefix, const char *fmt, va_list ap)
   1027 {
   1028 	if ((boothowto & AB_DEBUG) == 0)
   1029 		return;
   1030 
   1031 	kprintf_lock();
   1032 
   1033 	if (prefix)
   1034 		kprintf_internal("%s: ", TOCONS | TOLOG, NULL, NULL, prefix);
   1035 	kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
   1036 
   1037 	kprintf_unlock();
   1038 }
   1039 
   1040 void
   1041 aprint_debug(const char *fmt, ...)
   1042 {
   1043 	va_list ap;
   1044 
   1045 	va_start(ap, fmt);
   1046 	aprint_debug_internal(NULL, fmt, ap);
   1047 	va_end(ap);
   1048 }
   1049 
   1050 void
   1051 aprint_debug_dev(device_t dv, const char *fmt, ...)
   1052 {
   1053 	va_list ap;
   1054 
   1055 	KASSERT(dv != NULL);
   1056 
   1057 	va_start(ap, fmt);
   1058 	aprint_debug_internal(device_xname(dv), fmt, ap);
   1059 	va_end(ap);
   1060 }
   1061 
   1062 void
   1063 aprint_debug_ifnet(struct ifnet *ifp, const char *fmt, ...)
   1064 {
   1065 	va_list ap;
   1066 
   1067 	KASSERT(ifp != NULL);
   1068 
   1069 	va_start(ap, fmt);
   1070 	aprint_debug_internal(ifp->if_xname, fmt, ap);
   1071 	va_end(ap);
   1072 }
   1073 
   1074 void
   1075 vprintf_flags(int flags, const char *fmt, va_list ap)
   1076 {
   1077 	kprintf_lock();
   1078 	kprintf(fmt, flags, NULL, NULL, ap);
   1079 	kprintf_unlock();
   1080 }
   1081 
   1082 void
   1083 printf_flags(int flags, const char *fmt, ...)
   1084 {
   1085 	va_list ap;
   1086 
   1087 	va_start(ap, fmt);
   1088 	vprintf_flags(flags, fmt, ap);
   1089 	va_end(ap);
   1090 }
   1091 
   1092 void
   1093 printf_tolog(const char *fmt, ...)
   1094 {
   1095 	va_list ap;
   1096 
   1097 	va_start(ap, fmt);
   1098 	vprintf_flags(TOLOG, fmt, ap);
   1099 	va_end(ap);
   1100 }
   1101 
   1102 /*
   1103  * printf_nolog: Like printf(), but does not send message to the log.
   1104  */
   1105 
   1106 void
   1107 printf_nolog(const char *fmt, ...)
   1108 {
   1109 	va_list ap;
   1110 
   1111 	va_start(ap, fmt);
   1112 	vprintf_flags(TOCONS, fmt, ap);
   1113 	va_end(ap);
   1114 }
   1115 
   1116 /*
   1117  * normal kernel printf functions: printf, vprintf, snprintf, vsnprintf
   1118  */
   1119 
   1120 /*
   1121  * printf: print a message to the console and the log
   1122  */
   1123 void
   1124 printf(const char *fmt, ...)
   1125 {
   1126 	va_list ap;
   1127 
   1128 	va_start(ap, fmt);
   1129 	vprintf_flags(TOCONS | TOLOG, fmt, ap);
   1130 	va_end(ap);
   1131 }
   1132 
   1133 /*
   1134  * vprintf: print a message to the console and the log [already have
   1135  *	va_list]
   1136  */
   1137 
   1138 void
   1139 vprintf(const char *fmt, va_list ap)
   1140 {
   1141 	vprintf_flags(TOCONS | TOLOG, fmt, ap);
   1142 
   1143 	if (!panicstr)
   1144 		logwakeup();
   1145 }
   1146 
   1147 /*
   1148  * snprintf: print a message to a buffer
   1149  */
   1150 int
   1151 snprintf(char *bf, size_t size, const char *fmt, ...)
   1152 {
   1153 	int retval;
   1154 	va_list ap;
   1155 
   1156 	va_start(ap, fmt);
   1157 	retval = vsnprintf(bf, size, fmt, ap);
   1158 	va_end(ap);
   1159 
   1160 	return retval;
   1161 }
   1162 
   1163 /*
   1164  * vsnprintf: print a message to a buffer [already have va_list]
   1165  */
   1166 int
   1167 vsnprintf(char *bf, size_t size, const char *fmt, va_list ap)
   1168 {
   1169 	int retval;
   1170 	char *p;
   1171 
   1172 	p = bf + size;
   1173 	retval = kprintf(fmt, TOBUFONLY, &p, bf, ap);
   1174 	if (bf && size > 0) {
   1175 		/* nul terminate */
   1176 		if (size <= (size_t)retval)
   1177 			bf[size - 1] = '\0';
   1178 		else
   1179 			bf[retval] = '\0';
   1180 	}
   1181 	return retval;
   1182 }
   1183 
   1184 /*
   1185  * kprintf: scaled down version of printf(3).
   1186  *
   1187  * this version based on vfprintf() from libc which was derived from
   1188  * software contributed to Berkeley by Chris Torek.
   1189  *
   1190  * NOTE: The kprintf mutex must be held if we're going TOBUF or TOCONS!
   1191  */
   1192 
   1193 /*
   1194  * macros for converting digits to letters and vice versa
   1195  */
   1196 #define	to_digit(c)	((c) - '0')
   1197 #define is_digit(c)	((unsigned)to_digit(c) <= 9)
   1198 #define	to_char(n)	((n) + '0')
   1199 
   1200 /*
   1201  * flags used during conversion.
   1202  */
   1203 #define	ALT		0x001		/* alternate form */
   1204 #define	HEXPREFIX	0x002		/* add 0x or 0X prefix */
   1205 #define	LADJUST		0x004		/* left adjustment */
   1206 #define	LONGDBL		0x008		/* long double; unimplemented */
   1207 #define	LONGINT		0x010		/* long integer */
   1208 #define	QUADINT		0x020		/* quad integer */
   1209 #define	SHORTINT	0x040		/* short integer */
   1210 #define	MAXINT		0x080		/* intmax_t */
   1211 #define	PTRINT		0x100		/* intptr_t */
   1212 #define	SIZEINT		0x200		/* size_t */
   1213 #define	ZEROPAD		0x400		/* zero (as opposed to blank) pad */
   1214 #define FPT		0x800		/* Floating point number */
   1215 
   1216 	/*
   1217 	 * To extend shorts properly, we need both signed and unsigned
   1218 	 * argument extraction methods.
   1219 	 */
   1220 #define	SARG() \
   1221 	(flags&MAXINT ? va_arg(ap, intmax_t) : \
   1222 	    flags&PTRINT ? va_arg(ap, intptr_t) : \
   1223 	    flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
   1224 	    flags&QUADINT ? va_arg(ap, quad_t) : \
   1225 	    flags&LONGINT ? va_arg(ap, long) : \
   1226 	    flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
   1227 	    (long)va_arg(ap, int))
   1228 #define	UARG() \
   1229 	(flags&MAXINT ? va_arg(ap, uintmax_t) : \
   1230 	    flags&PTRINT ? va_arg(ap, uintptr_t) : \
   1231 	    flags&SIZEINT ? va_arg(ap, size_t) : \
   1232 	    flags&QUADINT ? va_arg(ap, u_quad_t) : \
   1233 	    flags&LONGINT ? va_arg(ap, u_long) : \
   1234 	    flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
   1235 	    (u_long)va_arg(ap, u_int))
   1236 
   1237 #define KPRINTF_PUTCHAR(C) {						\
   1238 	if (oflags == TOBUFONLY) {					\
   1239 		if (sbuf && ((vp == NULL) || (sbuf < tailp))) 		\
   1240 			*sbuf++ = (C);					\
   1241 	} else {							\
   1242 		putchar((C), oflags, vp);				\
   1243 	}								\
   1244 }
   1245 
   1246 void
   1247 device_printf(device_t dev, const char *fmt, ...)
   1248 {
   1249 	va_list ap;
   1250 
   1251 	va_start(ap, fmt);
   1252 	printf("%s: ", device_xname(dev));
   1253 	vprintf(fmt, ap);
   1254 	va_end(ap);
   1255 	return;
   1256 }
   1257 
   1258 /*
   1259  * Guts of kernel printf.  Note, we already expect to be in a mutex!
   1260  */
   1261 int
   1262 kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap)
   1263 {
   1264 	const char *fmt;	/* format string */
   1265 	int ch;			/* character from fmt */
   1266 	int n;			/* handy integer (short term usage) */
   1267 	char *cp;		/* handy char pointer (short term usage) */
   1268 	int flags;		/* flags as above */
   1269 	int ret;		/* return value accumulator */
   1270 	int width;		/* width from format (%8d), or 0 */
   1271 	int prec;		/* precision from format (%.3d), or -1 */
   1272 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
   1273 
   1274 	u_quad_t _uquad;	/* integer arguments %[diouxX] */
   1275 	enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
   1276 	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
   1277 	int realsz;		/* field size expanded by dprec */
   1278 	int size;		/* size of converted field or string */
   1279 	const char *xdigs;	/* digits for [xX] conversion */
   1280 	char bf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
   1281 	char *tailp;		/* tail pointer for snprintf */
   1282 
   1283 	if (oflags == TOBUFONLY && (vp != NULL))
   1284 		tailp = *(char **)vp;
   1285 	else
   1286 		tailp = NULL;
   1287 
   1288 	cp = NULL;	/* XXX: shutup gcc */
   1289 	size = 0;	/* XXX: shutup gcc */
   1290 
   1291 	fmt = fmt0;
   1292 	ret = 0;
   1293 
   1294 	xdigs = NULL;		/* XXX: shut up gcc warning */
   1295 
   1296 	/*
   1297 	 * Scan the format for conversions (`%' character).
   1298 	 */
   1299 	for (;;) {
   1300 		for (; *fmt != '%' && *fmt; fmt++) {
   1301 			ret++;
   1302 			KPRINTF_PUTCHAR(*fmt);
   1303 		}
   1304 		if (*fmt == 0)
   1305 			goto done;
   1306 
   1307 		fmt++;		/* skip over '%' */
   1308 
   1309 		flags = 0;
   1310 		dprec = 0;
   1311 		width = 0;
   1312 		prec = -1;
   1313 		sign = '\0';
   1314 
   1315 rflag:		ch = *fmt++;
   1316 reswitch:	switch (ch) {
   1317 		case ' ':
   1318 			/*
   1319 			 * ``If the space and + flags both appear, the space
   1320 			 * flag will be ignored.''
   1321 			 *	-- ANSI X3J11
   1322 			 */
   1323 			if (!sign)
   1324 				sign = ' ';
   1325 			goto rflag;
   1326 		case '#':
   1327 			flags |= ALT;
   1328 			goto rflag;
   1329 		case '*':
   1330 			/*
   1331 			 * ``A negative field width argument is taken as a
   1332 			 * - flag followed by a positive field width.''
   1333 			 *	-- ANSI X3J11
   1334 			 * They don't exclude field widths read from args.
   1335 			 */
   1336 			if ((width = va_arg(ap, int)) >= 0)
   1337 				goto rflag;
   1338 			width = -width;
   1339 			/* FALLTHROUGH */
   1340 		case '-':
   1341 			flags |= LADJUST;
   1342 			goto rflag;
   1343 		case '+':
   1344 			sign = '+';
   1345 			goto rflag;
   1346 		case '.':
   1347 			if ((ch = *fmt++) == '*') {
   1348 				n = va_arg(ap, int);
   1349 				prec = n < 0 ? -1 : n;
   1350 				goto rflag;
   1351 			}
   1352 			n = 0;
   1353 			while (is_digit(ch)) {
   1354 				n = 10 * n + to_digit(ch);
   1355 				ch = *fmt++;
   1356 			}
   1357 			prec = n < 0 ? -1 : n;
   1358 			goto reswitch;
   1359 		case '0':
   1360 			/*
   1361 			 * ``Note that 0 is taken as a flag, not as the
   1362 			 * beginning of a field width.''
   1363 			 *	-- ANSI X3J11
   1364 			 */
   1365 			flags |= ZEROPAD;
   1366 			goto rflag;
   1367 		case '1': case '2': case '3': case '4':
   1368 		case '5': case '6': case '7': case '8': case '9':
   1369 			n = 0;
   1370 			do {
   1371 				n = 10 * n + to_digit(ch);
   1372 				ch = *fmt++;
   1373 			} while (is_digit(ch));
   1374 			width = n;
   1375 			goto reswitch;
   1376 		case 'h':
   1377 			flags |= SHORTINT;
   1378 			goto rflag;
   1379 		case 'j':
   1380 			flags |= MAXINT;
   1381 			goto rflag;
   1382 		case 'l':
   1383 			if (*fmt == 'l') {
   1384 				fmt++;
   1385 				flags |= QUADINT;
   1386 			} else {
   1387 				flags |= LONGINT;
   1388 			}
   1389 			goto rflag;
   1390 		case 'q':
   1391 			flags |= QUADINT;
   1392 			goto rflag;
   1393 		case 't':
   1394 			flags |= PTRINT;
   1395 			goto rflag;
   1396 		case 'z':
   1397 			flags |= SIZEINT;
   1398 			goto rflag;
   1399 		case 'c':
   1400 			*(cp = bf) = va_arg(ap, int);
   1401 			size = 1;
   1402 			sign = '\0';
   1403 			break;
   1404 		case 'D':
   1405 			flags |= LONGINT;
   1406 			/*FALLTHROUGH*/
   1407 		case 'd':
   1408 		case 'i':
   1409 			_uquad = SARG();
   1410 			if ((quad_t)_uquad < 0) {
   1411 				_uquad = -_uquad;
   1412 				sign = '-';
   1413 			}
   1414 			base = DEC;
   1415 			goto number;
   1416 		case 'n':
   1417 			if (flags & MAXINT)
   1418 				*va_arg(ap, intmax_t *) = ret;
   1419 			else if (flags & PTRINT)
   1420 				*va_arg(ap, intptr_t *) = ret;
   1421 			else if (flags & SIZEINT)
   1422 				*va_arg(ap, ssize_t *) = ret;
   1423 			else if (flags & QUADINT)
   1424 				*va_arg(ap, quad_t *) = ret;
   1425 			else if (flags & LONGINT)
   1426 				*va_arg(ap, long *) = ret;
   1427 			else if (flags & SHORTINT)
   1428 				*va_arg(ap, short *) = ret;
   1429 			else
   1430 				*va_arg(ap, int *) = ret;
   1431 			continue;	/* no output */
   1432 		case 'O':
   1433 			flags |= LONGINT;
   1434 			/*FALLTHROUGH*/
   1435 		case 'o':
   1436 			_uquad = UARG();
   1437 			base = OCT;
   1438 			goto nosign;
   1439 		case 'p':
   1440 			/*
   1441 			 * ``The argument shall be a pointer to void.  The
   1442 			 * value of the pointer is converted to a sequence
   1443 			 * of printable characters, in an implementation-
   1444 			 * defined manner.''
   1445 			 *	-- ANSI X3J11
   1446 			 */
   1447 			/* NOSTRICT */
   1448 			_uquad = (u_long)va_arg(ap, void *);
   1449 			base = HEX;
   1450 			xdigs = hexdigits;
   1451 			flags |= HEXPREFIX;
   1452 			ch = 'x';
   1453 			goto nosign;
   1454 		case 's':
   1455 			if ((cp = va_arg(ap, char *)) == NULL)
   1456 				/*XXXUNCONST*/
   1457 				cp = __UNCONST("(null)");
   1458 			if (prec >= 0) {
   1459 				/*
   1460 				 * can't use strlen; can only look for the
   1461 				 * NUL in the first `prec' characters, and
   1462 				 * strlen() will go further.
   1463 				 */
   1464 				char *p = memchr(cp, 0, prec);
   1465 
   1466 				if (p != NULL) {
   1467 					size = p - cp;
   1468 					if (size > prec)
   1469 						size = prec;
   1470 				} else
   1471 					size = prec;
   1472 			} else
   1473 				size = strlen(cp);
   1474 			sign = '\0';
   1475 			break;
   1476 		case 'U':
   1477 			flags |= LONGINT;
   1478 			/*FALLTHROUGH*/
   1479 		case 'u':
   1480 			_uquad = UARG();
   1481 			base = DEC;
   1482 			goto nosign;
   1483 		case 'X':
   1484 			xdigs = HEXDIGITS;
   1485 			goto hex;
   1486 		case 'x':
   1487 			xdigs = hexdigits;
   1488 hex:			_uquad = UARG();
   1489 			base = HEX;
   1490 			/* leading 0x/X only if non-zero */
   1491 			if (flags & ALT && _uquad != 0)
   1492 				flags |= HEXPREFIX;
   1493 
   1494 			/* unsigned conversions */
   1495 nosign:			sign = '\0';
   1496 			/*
   1497 			 * ``... diouXx conversions ... if a precision is
   1498 			 * specified, the 0 flag will be ignored.''
   1499 			 *	-- ANSI X3J11
   1500 			 */
   1501 number:			if ((dprec = prec) >= 0)
   1502 				flags &= ~ZEROPAD;
   1503 
   1504 			/*
   1505 			 * ``The result of converting a zero value with an
   1506 			 * explicit precision of zero is no characters.''
   1507 			 *	-- ANSI X3J11
   1508 			 */
   1509 			cp = bf + KPRINTF_BUFSIZE;
   1510 			if (_uquad != 0 || prec != 0) {
   1511 				/*
   1512 				 * Unsigned mod is hard, and unsigned mod
   1513 				 * by a constant is easier than that by
   1514 				 * a variable; hence this switch.
   1515 				 */
   1516 				switch (base) {
   1517 				case OCT:
   1518 					do {
   1519 						*--cp = to_char(_uquad & 7);
   1520 						_uquad >>= 3;
   1521 					} while (_uquad);
   1522 					/* handle octal leading 0 */
   1523 					if (flags & ALT && *cp != '0')
   1524 						*--cp = '0';
   1525 					break;
   1526 
   1527 				case DEC:
   1528 					/* many numbers are 1 digit */
   1529 					while (_uquad >= 10) {
   1530 						*--cp = to_char(_uquad % 10);
   1531 						_uquad /= 10;
   1532 					}
   1533 					*--cp = to_char(_uquad);
   1534 					break;
   1535 
   1536 				case HEX:
   1537 					do {
   1538 						*--cp = xdigs[_uquad & 15];
   1539 						_uquad >>= 4;
   1540 					} while (_uquad);
   1541 					break;
   1542 
   1543 				default:
   1544 					/*XXXUNCONST*/
   1545 					cp = __UNCONST("bug in kprintf: bad base");
   1546 					size = strlen(cp);
   1547 					goto skipsize;
   1548 				}
   1549 			}
   1550 			size = bf + KPRINTF_BUFSIZE - cp;
   1551 		skipsize:
   1552 			break;
   1553 		default:	/* "%?" prints ?, unless ? is NUL */
   1554 			if (ch == '\0')
   1555 				goto done;
   1556 			/* pretend it was %c with argument ch */
   1557 			cp = bf;
   1558 			*cp = ch;
   1559 			size = 1;
   1560 			sign = '\0';
   1561 			break;
   1562 		}
   1563 
   1564 		/*
   1565 		 * All reasonable formats wind up here.  At this point, `cp'
   1566 		 * points to a string which (if not flags&LADJUST) should be
   1567 		 * padded out to `width' places.  If flags&ZEROPAD, it should
   1568 		 * first be prefixed by any sign or other prefix; otherwise,
   1569 		 * it should be blank padded before the prefix is emitted.
   1570 		 * After any left-hand padding and prefixing, emit zeroes
   1571 		 * required by a decimal [diouxX] precision, then print the
   1572 		 * string proper, then emit zeroes required by any leftover
   1573 		 * floating precision; finally, if LADJUST, pad with blanks.
   1574 		 *
   1575 		 * Compute actual size, so we know how much to pad.
   1576 		 * size excludes decimal prec; realsz includes it.
   1577 		 */
   1578 		realsz = dprec > size ? dprec : size;
   1579 		if (sign)
   1580 			realsz++;
   1581 		else if (flags & HEXPREFIX)
   1582 			realsz+= 2;
   1583 
   1584 		/* adjust ret */
   1585 		ret += width > realsz ? width : realsz;
   1586 
   1587 		/* right-adjusting blank padding */
   1588 		if ((flags & (LADJUST|ZEROPAD)) == 0) {
   1589 			n = width - realsz;
   1590 			while (n-- > 0)
   1591 				KPRINTF_PUTCHAR(' ');
   1592 		}
   1593 
   1594 		/* prefix */
   1595 		if (sign) {
   1596 			KPRINTF_PUTCHAR(sign);
   1597 		} else if (flags & HEXPREFIX) {
   1598 			KPRINTF_PUTCHAR('0');
   1599 			KPRINTF_PUTCHAR(ch);
   1600 		}
   1601 
   1602 		/* right-adjusting zero padding */
   1603 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
   1604 			n = width - realsz;
   1605 			while (n-- > 0)
   1606 				KPRINTF_PUTCHAR('0');
   1607 		}
   1608 
   1609 		/* leading zeroes from decimal precision */
   1610 		n = dprec - size;
   1611 		while (n-- > 0)
   1612 			KPRINTF_PUTCHAR('0');
   1613 
   1614 		/* the string or number proper */
   1615 		for (; size--; cp++)
   1616 			KPRINTF_PUTCHAR(*cp);
   1617 		/* left-adjusting padding (always blank) */
   1618 		if (flags & LADJUST) {
   1619 			n = width - realsz;
   1620 			while (n-- > 0)
   1621 				KPRINTF_PUTCHAR(' ');
   1622 		}
   1623 	}
   1624 
   1625 done:
   1626 	if ((oflags == TOBUFONLY) && (vp != NULL))
   1627 		*(char **)vp = sbuf;
   1628 	(*v_flush)();
   1629 
   1630 #ifdef RND_PRINTF
   1631 	if (!cold) {
   1632 		struct timespec ts;
   1633 		(void)nanotime(&ts);
   1634 		SHA512_Update(&kprnd_sha, (char *)&ts, sizeof(ts));
   1635 	}
   1636 #endif
   1637 	return ret;
   1638 }
   1639