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