2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1991 The Regents of the University of California.
6 * Copyright (c) 1999 Michael Smith
7 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
11 * This code is derived from software contributed to Berkeley by
12 * the Systems Programming Group of the University of Utah Computer
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/cdefs.h>
42 #include "opt_syscons.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/mutex.h>
50 #include <sys/fcntl.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/msgbuf.h>
56 #include <sys/namei.h>
59 #include <sys/queue.h>
60 #include <sys/reboot.h>
61 #include <sys/sysctl.h>
63 #include <sys/tslog.h>
66 #include <sys/vnode.h>
70 #include <dev/kbd/kbdreg.h>
72 #include <machine/cpu.h>
73 #include <machine/clock.h>
76 * Check for 'options EARLY_PRINTF' that may have been used in old kernel
77 * config files. If you are hitting this error you should update your
78 * config to use 'options EARLY_PRINTF=<device name>', e.g. with the
81 * options EARLY_PRINTF=pl011
83 #if CHECK_EARLY_PRINTF(1)
84 #error Update your config to use 'options EARLY_PRINTF=<device name>'
87 static MALLOC_DEFINE(M_TTYCONS, "tty console", "tty console handling");
90 STAILQ_ENTRY(cn_device) cnd_next;
91 struct consdev *cnd_cn;
94 #define CNDEVPATHMAX 32
95 #define CNDEVTAB_SIZE 4
96 static struct cn_device cn_devtab[CNDEVTAB_SIZE];
97 static STAILQ_HEAD(, cn_device) cn_devlist =
98 STAILQ_HEAD_INITIALIZER(cn_devlist);
100 int cons_avail_mask = 0; /* Bit mask. Each registered low level console
101 * which is currently unavailable for inpit
102 * (i.e., if it is in graphics mode) will have
107 SYSCTL_INT(_kern, OID_AUTO, consmute, CTLFLAG_RW, &cn_mute, 0,
108 "State of the console muting");
110 static char *consbuf; /* buffer used by `consmsgbuf' */
111 static struct callout conscallout; /* callout for outputting to constty */
112 struct msgbuf consmsgbuf; /* message buffer for console tty */
113 static bool console_pausing; /* pause after each line during probe */
114 static const char console_pausestr[] =
115 "<pause; press any key to proceed to next line or '.' to end pause mode>";
116 struct tty *constty; /* pointer to console "window" tty */
117 static struct mtx constty_mtx; /* Mutex for constty assignment. */
118 MTX_SYSINIT(constty_mtx, &constty_mtx, "constty_mtx", MTX_DEF);
119 static struct mtx cnputs_mtx; /* Mutex for cnputs(). */
120 MTX_SYSINIT(cnputs_mtx, &cnputs_mtx, "cnputs_mtx", MTX_SPIN | MTX_NOWITNESS);
122 static void constty_timeout(void *arg);
124 static struct consdev cons_consdev;
125 DATA_SET(cons_set, cons_consdev);
126 SET_DECLARE(cons_set, struct consdev);
129 * Stub for configurations that don't actually have a keyboard driver. Inclusion
130 * of kbd.c is contingent on any number of keyboard/console drivers being
131 * present in the kernel; rather than trying to catch them all, we'll just
132 * maintain this weak kbdinit that will be overridden by the strong version in
133 * kbd.c if it's present.
142 mute_console(void *data __unused)
145 if ((boothowto & (RB_MUTEMSGS | RB_VERBOSE)) == RB_MUTEMSGS) {
146 printf("-- Muting boot messages --\n");
151 SYSINIT(mute_console, SI_SUB_COPYRIGHT, SI_ORDER_ANY, mute_console, NULL);
156 struct consdev *best_cn, *cn, **list;
160 * Check if we should mute the console (for security reasons perhaps)
161 * It can be changes dynamically using sysctl kern.consmute
162 * once we are up and going.
165 cn_mute = ((boothowto & (RB_MUTE
168 |RB_ASKNAME)) == RB_MUTE);
171 * Bring up the kbd layer just in time for cnprobe. Console drivers
172 * have a dependency on kbd being ready, so this fits nicely between the
173 * machdep callers of cninit() and MI probing/initialization of consoles
179 * Find the first console with the highest priority.
182 SET_FOREACH(list, cons_set) {
185 /* Skip cons_consdev. */
186 if (cn->cn_ops == NULL)
188 cn->cn_ops->cn_probe(cn);
189 if (cn->cn_pri == CN_DEAD)
191 if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
193 if (boothowto & RB_MULTIPLE) {
195 * Initialize console, and attach to it.
197 cn->cn_ops->cn_init(cn);
203 if ((boothowto & RB_MULTIPLE) == 0) {
204 best_cn->cn_ops->cn_init(best_cn);
207 if (boothowto & RB_PAUSE)
208 console_pausing = true;
210 * Make the best console the preferred console.
216 * Release early console.
226 console_pausing = false;
229 /* add a new physical console to back the virtual console */
231 cnadd(struct consdev *cn)
233 struct cn_device *cnd;
236 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
237 if (cnd->cnd_cn == cn)
239 for (i = 0; i < CNDEVTAB_SIZE; i++) {
241 if (cnd->cnd_cn == NULL)
244 if (cnd->cnd_cn != NULL)
247 if (cn->cn_name[0] == '\0') {
248 /* XXX: it is unclear if/where this print might output */
249 printf("WARNING: console at %p has no name\n", cn);
251 STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
252 if (STAILQ_FIRST(&cn_devlist) == cnd)
253 ttyconsdev_select(cnd->cnd_cn->cn_name);
255 /* Add device to the active mask. */
256 cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
262 cnremove(struct consdev *cn)
264 struct cn_device *cnd;
267 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
268 if (cnd->cnd_cn != cn)
270 if (STAILQ_FIRST(&cn_devlist) == cnd)
271 ttyconsdev_select(NULL);
272 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
275 /* Remove this device from available mask. */
276 for (i = 0; i < CNDEVTAB_SIZE; i++)
277 if (cnd == &cn_devtab[i]) {
278 cons_avail_mask &= ~(1 << i);
284 * syscons gets really confused if console resources are
285 * freed after the system has initialized.
287 if (cn->cn_term != NULL)
288 cn->cn_ops->cn_term(cn);
295 cnselect(struct consdev *cn)
297 struct cn_device *cnd;
299 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
300 if (cnd->cnd_cn != cn)
302 if (cnd == STAILQ_FIRST(&cn_devlist))
304 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
305 STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
306 ttyconsdev_select(cnd->cnd_cn->cn_name);
312 cnavailable(struct consdev *cn, int available)
316 for (i = 0; i < CNDEVTAB_SIZE; i++) {
317 if (cn_devtab[i].cnd_cn == cn)
321 if (i < CNDEVTAB_SIZE)
322 cons_avail_mask |= (1 << i);
323 cn->cn_flags &= ~CN_FLAG_NOAVAIL;
325 if (i < CNDEVTAB_SIZE)
326 cons_avail_mask &= ~(1 << i);
327 cn->cn_flags |= CN_FLAG_NOAVAIL;
335 return (cons_avail_mask == 0);
339 * sysctl_kern_console() provides output parseable in conscontrol(1).
342 sysctl_kern_console(SYSCTL_HANDLER_ARGS)
344 struct cn_device *cnd;
345 struct consdev *cp, **list;
351 sb = sbuf_new(NULL, NULL, CNDEVPATHMAX * 2, SBUF_AUTOEXTEND |
356 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
357 sbuf_printf(sb, "%s,", cnd->cnd_cn->cn_name);
359 SET_FOREACH(list, cons_set) {
361 if (cp->cn_name[0] != '\0')
362 sbuf_printf(sb, "%s,", cp->cn_name);
365 error = sysctl_handle_string(oidp, sbuf_data(sb), sbuf_len(sb), req);
366 if (error == 0 && req->newptr != NULL) {
374 SET_FOREACH(list, cons_set) {
376 if (strcmp(p, cp->cn_name) != 0)
393 SYSCTL_PROC(_kern, OID_AUTO, console,
394 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0,
395 sysctl_kern_console, "A",
396 "Console device control");
401 struct cn_device *cnd;
404 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
406 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
407 cn->cn_ops->cn_grab(cn);
414 struct cn_device *cnd;
417 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
419 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
420 cn->cn_ops->cn_ungrab(cn);
427 struct cn_device *cnd;
430 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
432 if (cn->cn_ops->cn_resume != NULL)
433 cn->cn_ops->cn_resume(cn);
438 * Low level console routines.
447 while ((c = cncheckc()) == -1)
450 c = '\n'; /* console input is always ICRNL */
457 struct cn_device *cnd;
463 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
465 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
466 c = cn->cn_ops->cn_getc(cn);
475 cngets(char *cp, size_t size, int visible)
524 struct cn_device *cnd;
529 if (early_putc != NULL) {
537 if (cn_mute || c == '\0')
539 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
541 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
543 cn->cn_ops->cn_putc(cn, '\r');
544 cn->cn_ops->cn_putc(cn, c);
547 if (console_pausing && c == '\n' && !kdb_active) {
548 for (cp = console_pausestr; *cp != '\0'; cp++)
552 console_pausing = false;
555 for (cp = console_pausestr; *cp != '\0'; cp++)
562 cnputsn(const char *p, size_t n)
565 bool unlock_reqd = false;
567 if (mtx_initialized(&cnputs_mtx)) {
569 * NOTE: Debug prints and/or witness printouts in
570 * console driver clients can cause the "cnputs_mtx"
571 * mutex to recurse. Simply return if that happens.
573 if (mtx_owned(&cnputs_mtx))
575 mtx_lock_spin(&cnputs_mtx);
579 for (i = 0; i < n; i++)
583 mtx_unlock_spin(&cnputs_mtx);
587 cnputs(const char *p)
589 cnputsn(p, strlen(p));
592 static unsigned int consmsgbuf_size = 65536;
593 SYSCTL_UINT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RWTUN, &consmsgbuf_size,
594 0, "Console tty buffer size");
597 * Redirect console output to a tty.
600 constty_set(struct tty *tp)
602 int size = consmsgbuf_size;
605 tty_assert_locked(tp);
611 if (consbuf == NULL) {
613 buf = malloc(size, M_TTYCONS, M_WAITOK);
616 mtx_lock(&constty_mtx);
617 if (constty != NULL) {
618 mtx_unlock(&constty_mtx);
619 free(buf, M_TTYCONS);
622 if (consbuf == NULL) {
624 msgbuf_init(&consmsgbuf, buf, size);
626 free(buf, M_TTYCONS);
628 mtx_unlock(&constty_mtx);
630 callout_init_mtx(&conscallout, tty_getlock(tp), 0);
636 * Disable console redirection to a tty.
639 constty_clear(struct tty *tp)
643 tty_assert_locked(tp);
646 callout_stop(&conscallout);
647 mtx_lock(&constty_mtx);
649 mtx_unlock(&constty_mtx);
650 while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
652 /* We never free consbuf because it can still be in use. */
656 /* Times per second to check for pending console tty messages. */
657 static int constty_wakeups_per_second = 15;
658 SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
659 &constty_wakeups_per_second, 0,
660 "Times per second to check for pending console tty messages");
663 constty_timeout(void *arg)
665 struct tty *tp = arg;
668 tty_assert_locked(tp);
669 while ((c = msgbuf_getchar(&consmsgbuf)) != -1) {
670 if (tty_putchar(tp, c) < 0) {
675 callout_reset_sbt(&conscallout, SBT_1S / constty_wakeups_per_second,
676 0, constty_timeout, tp, C_PREL(1));
680 * Sysbeep(), if we have hardware for it
683 #ifdef HAS_TIMER_SPKR
686 static struct callout beeping_timer;
689 sysbeepstop(void *chan)
692 timer_spkr_release();
697 sysbeep(int pitch, sbintime_t duration)
700 if (timer_spkr_acquire()) {
702 /* Something else owns it. */
706 timer_spkr_setfreq(pitch);
709 callout_reset_sbt(&beeping_timer, duration, 0, sysbeepstop,
716 sysbeep_init(void *unused)
719 callout_init(&beeping_timer, 1);
721 SYSINIT(sysbeep, SI_SUB_SOFTINTR, SI_ORDER_ANY, sysbeep_init, NULL);
725 * No hardware, no sound
729 sysbeep(int pitch __unused, sbintime_t duration __unused)
738 * Temporary support for sc(4) to vt(4) transition.
740 static unsigned vty_prefer;
741 static char vty_name[16];
742 SYSCTL_STRING(_kern, OID_AUTO, vty, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, vty_name,
743 0, "Console vty driver");
746 vty_enabled(unsigned vty)
748 static unsigned vty_selected = 0;
750 if (vty_selected == 0) {
751 TUNABLE_STR_FETCH("kern.vty", vty_name, sizeof(vty_name));
754 if (strcmp(vty_name, "sc") == 0) {
755 vty_selected = VTY_SC;
760 if (strcmp(vty_name, "vt") == 0) {
761 vty_selected = VTY_VT;
765 if (vty_prefer != 0) {
766 vty_selected = vty_prefer;
770 vty_selected = VTY_VT;
771 #elif defined(DEV_SC)
772 vty_selected = VTY_SC;
776 if (vty_selected == VTY_VT)
777 strcpy(vty_name, "vt");
778 else if (vty_selected == VTY_SC)
779 strcpy(vty_name, "sc");
781 return ((vty_selected & vty) != 0);
785 vty_set_preferred(unsigned vty)
790 vty_prefer &= ~VTY_SC;
793 vty_prefer &= ~VTY_VT;