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