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