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