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