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