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
39 * from: @(#)cons.c 7.2 (Berkeley) 5/9/91
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 #include "opt_syscons.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
51 #include <sys/mutex.h>
54 #include <sys/fcntl.h>
57 #include <sys/kernel.h>
58 #include <sys/malloc.h>
59 #include <sys/msgbuf.h>
60 #include <sys/namei.h>
63 #include <sys/queue.h>
64 #include <sys/reboot.h>
65 #include <sys/sysctl.h>
67 #include <sys/tslog.h>
70 #include <sys/vnode.h>
74 #include <dev/kbd/kbdreg.h>
76 #include <machine/cpu.h>
77 #include <machine/clock.h>
79 static MALLOC_DEFINE(M_TTYCONS, "tty console", "tty console handling");
82 STAILQ_ENTRY(cn_device) cnd_next;
83 struct consdev *cnd_cn;
86 #define CNDEVPATHMAX 32
87 #define CNDEVTAB_SIZE 4
88 static struct cn_device cn_devtab[CNDEVTAB_SIZE];
89 static STAILQ_HEAD(, cn_device) cn_devlist =
90 STAILQ_HEAD_INITIALIZER(cn_devlist);
92 int cons_avail_mask = 0; /* Bit mask. Each registered low level console
93 * which is currently unavailable for inpit
94 * (i.e., if it is in graphics mode) will have
99 SYSCTL_INT(_kern, OID_AUTO, consmute, CTLFLAG_RW, &cn_mute, 0,
100 "State of the console muting");
102 static char *consbuf; /* buffer used by `consmsgbuf' */
103 static struct callout conscallout; /* callout for outputting to constty */
104 struct msgbuf consmsgbuf; /* message buffer for console tty */
105 static bool console_pausing; /* pause after each line during probe */
106 static const char console_pausestr[] =
107 "<pause; press any key to proceed to next line or '.' to end pause mode>";
108 struct tty *constty; /* pointer to console "window" tty */
109 static struct mtx constty_mtx; /* Mutex for constty assignment. */
110 MTX_SYSINIT(constty_mtx, &constty_mtx, "constty_mtx", MTX_DEF);
111 static struct mtx cnputs_mtx; /* Mutex for cnputs(). */
112 MTX_SYSINIT(cnputs_mtx, &cnputs_mtx, "cnputs_mtx", MTX_SPIN | MTX_NOWITNESS);
114 static void constty_timeout(void *arg);
116 static struct consdev cons_consdev;
117 DATA_SET(cons_set, cons_consdev);
118 SET_DECLARE(cons_set, struct consdev);
121 * Stub for configurations that don't actually have a keyboard driver. Inclusion
122 * of kbd.c is contingent on any number of keyboard/console drivers being
123 * present in the kernel; rather than trying to catch them all, we'll just
124 * maintain this weak kbdinit that will be overridden by the strong version in
125 * kbd.c if it's present.
136 struct consdev *best_cn, *cn, **list;
140 * Check if we should mute the console (for security reasons perhaps)
141 * It can be changes dynamically using sysctl kern.consmute
142 * once we are up and going.
145 cn_mute = ((boothowto & (RB_MUTE
148 |RB_ASKNAME)) == RB_MUTE);
151 * Bring up the kbd layer just in time for cnprobe. Console drivers
152 * have a dependency on kbd being ready, so this fits nicely between the
153 * machdep callers of cninit() and MI probing/initialization of consoles
159 * Find the first console with the highest priority.
162 SET_FOREACH(list, cons_set) {
165 /* Skip cons_consdev. */
166 if (cn->cn_ops == NULL)
168 cn->cn_ops->cn_probe(cn);
169 if (cn->cn_pri == CN_DEAD)
171 if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
173 if (boothowto & RB_MULTIPLE) {
175 * Initialize console, and attach to it.
177 cn->cn_ops->cn_init(cn);
183 if ((boothowto & RB_MULTIPLE) == 0) {
184 best_cn->cn_ops->cn_init(best_cn);
187 if (boothowto & RB_PAUSE)
188 console_pausing = true;
190 * Make the best console the preferred console.
196 * Release early console.
206 console_pausing = false;
209 /* add a new physical console to back the virtual console */
211 cnadd(struct consdev *cn)
213 struct cn_device *cnd;
216 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
217 if (cnd->cnd_cn == cn)
219 for (i = 0; i < CNDEVTAB_SIZE; i++) {
221 if (cnd->cnd_cn == NULL)
224 if (cnd->cnd_cn != NULL)
227 if (cn->cn_name[0] == '\0') {
228 /* XXX: it is unclear if/where this print might output */
229 printf("WARNING: console at %p has no name\n", cn);
231 STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
232 if (STAILQ_FIRST(&cn_devlist) == cnd)
233 ttyconsdev_select(cnd->cnd_cn->cn_name);
235 /* Add device to the active mask. */
236 cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
242 cnremove(struct consdev *cn)
244 struct cn_device *cnd;
247 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
248 if (cnd->cnd_cn != cn)
250 if (STAILQ_FIRST(&cn_devlist) == cnd)
251 ttyconsdev_select(NULL);
252 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
255 /* Remove this device from available mask. */
256 for (i = 0; i < CNDEVTAB_SIZE; i++)
257 if (cnd == &cn_devtab[i]) {
258 cons_avail_mask &= ~(1 << i);
264 * syscons gets really confused if console resources are
265 * freed after the system has initialized.
267 if (cn->cn_term != NULL)
268 cn->cn_ops->cn_term(cn);
275 cnselect(struct consdev *cn)
277 struct cn_device *cnd;
279 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
280 if (cnd->cnd_cn != cn)
282 if (cnd == STAILQ_FIRST(&cn_devlist))
284 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
285 STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
286 ttyconsdev_select(cnd->cnd_cn->cn_name);
292 cnavailable(struct consdev *cn, int available)
296 for (i = 0; i < CNDEVTAB_SIZE; i++) {
297 if (cn_devtab[i].cnd_cn == cn)
301 if (i < CNDEVTAB_SIZE)
302 cons_avail_mask |= (1 << i);
303 cn->cn_flags &= ~CN_FLAG_NOAVAIL;
305 if (i < CNDEVTAB_SIZE)
306 cons_avail_mask &= ~(1 << i);
307 cn->cn_flags |= CN_FLAG_NOAVAIL;
315 return (cons_avail_mask == 0);
319 * sysctl_kern_console() provides output parseable in conscontrol(1).
322 sysctl_kern_console(SYSCTL_HANDLER_ARGS)
324 struct cn_device *cnd;
325 struct consdev *cp, **list;
331 sb = sbuf_new(NULL, NULL, CNDEVPATHMAX * 2, SBUF_AUTOEXTEND |
336 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
337 sbuf_printf(sb, "%s,", cnd->cnd_cn->cn_name);
338 sbuf_printf(sb, "/");
339 SET_FOREACH(list, cons_set) {
341 if (cp->cn_name[0] != '\0')
342 sbuf_printf(sb, "%s,", cp->cn_name);
345 error = sysctl_handle_string(oidp, sbuf_data(sb), sbuf_len(sb), req);
346 if (error == 0 && req->newptr != NULL) {
354 SET_FOREACH(list, cons_set) {
356 if (strcmp(p, cp->cn_name) != 0)
373 SYSCTL_PROC(_kern, OID_AUTO, console,
374 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0,
375 sysctl_kern_console, "A",
376 "Console device control");
381 struct cn_device *cnd;
384 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
386 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
387 cn->cn_ops->cn_grab(cn);
394 struct cn_device *cnd;
397 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
399 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
400 cn->cn_ops->cn_ungrab(cn);
407 struct cn_device *cnd;
410 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
412 if (cn->cn_ops->cn_resume != NULL)
413 cn->cn_ops->cn_resume(cn);
418 * Low level console routines.
427 while ((c = cncheckc()) == -1)
430 c = '\n'; /* console input is always ICRNL */
437 struct cn_device *cnd;
443 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
445 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
446 c = cn->cn_ops->cn_getc(cn);
455 cngets(char *cp, size_t size, int visible)
504 struct cn_device *cnd;
509 if (early_putc != NULL) {
517 if (cn_mute || c == '\0')
519 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
521 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
523 cn->cn_ops->cn_putc(cn, '\r');
524 cn->cn_ops->cn_putc(cn, c);
527 if (console_pausing && c == '\n' && !kdb_active) {
528 for (cp = console_pausestr; *cp != '\0'; cp++)
532 console_pausing = false;
535 for (cp = console_pausestr; *cp != '\0'; cp++)
542 cnputsn(const char *p, size_t n)
545 bool unlock_reqd = false;
547 if (mtx_initialized(&cnputs_mtx)) {
549 * NOTE: Debug prints and/or witness printouts in
550 * console driver clients can cause the "cnputs_mtx"
551 * mutex to recurse. Simply return if that happens.
553 if (mtx_owned(&cnputs_mtx))
555 mtx_lock_spin(&cnputs_mtx);
559 for (i = 0; i < n; i++)
563 mtx_unlock_spin(&cnputs_mtx);
567 cnputs(const char *p)
569 cnputsn(p, strlen(p));
572 static unsigned int consmsgbuf_size = 65536;
573 SYSCTL_UINT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RWTUN, &consmsgbuf_size,
574 0, "Console tty buffer size");
577 * Redirect console output to a tty.
580 constty_set(struct tty *tp)
582 int size = consmsgbuf_size;
585 tty_assert_locked(tp);
591 if (consbuf == NULL) {
593 buf = malloc(size, M_TTYCONS, M_WAITOK);
596 mtx_lock(&constty_mtx);
597 if (constty != NULL) {
598 mtx_unlock(&constty_mtx);
599 free(buf, M_TTYCONS);
602 if (consbuf == NULL) {
604 msgbuf_init(&consmsgbuf, buf, size);
606 free(buf, M_TTYCONS);
608 mtx_unlock(&constty_mtx);
610 callout_init_mtx(&conscallout, tty_getlock(tp), 0);
616 * Disable console redirection to a tty.
619 constty_clear(struct tty *tp)
623 tty_assert_locked(tp);
626 callout_stop(&conscallout);
627 mtx_lock(&constty_mtx);
629 mtx_unlock(&constty_mtx);
630 while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
632 /* We never free consbuf because it can still be in use. */
636 /* Times per second to check for pending console tty messages. */
637 static int constty_wakeups_per_second = 15;
638 SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
639 &constty_wakeups_per_second, 0,
640 "Times per second to check for pending console tty messages");
643 constty_timeout(void *arg)
645 struct tty *tp = arg;
648 tty_assert_locked(tp);
649 while ((c = msgbuf_getchar(&consmsgbuf)) != -1) {
650 if (tty_putchar(tp, c) < 0) {
655 callout_reset_sbt(&conscallout, SBT_1S / constty_wakeups_per_second,
656 0, constty_timeout, tp, C_PREL(1));
660 * Sysbeep(), if we have hardware for it
663 #ifdef HAS_TIMER_SPKR
666 static struct callout beeping_timer;
669 sysbeepstop(void *chan)
672 timer_spkr_release();
677 sysbeep(int pitch, sbintime_t duration)
680 if (timer_spkr_acquire()) {
682 /* Something else owns it. */
686 timer_spkr_setfreq(pitch);
689 callout_reset_sbt(&beeping_timer, duration, 0, sysbeepstop,
696 sysbeep_init(void *unused)
699 callout_init(&beeping_timer, 1);
701 SYSINIT(sysbeep, SI_SUB_SOFTINTR, SI_ORDER_ANY, sysbeep_init, NULL);
705 * No hardware, no sound
709 sysbeep(int pitch __unused, sbintime_t duration __unused)
718 * Temporary support for sc(4) to vt(4) transition.
720 static unsigned vty_prefer;
721 static char vty_name[16];
722 SYSCTL_STRING(_kern, OID_AUTO, vty, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, vty_name,
723 0, "Console vty driver");
726 vty_enabled(unsigned vty)
728 static unsigned vty_selected = 0;
730 if (vty_selected == 0) {
731 TUNABLE_STR_FETCH("kern.vty", vty_name, sizeof(vty_name));
734 if (strcmp(vty_name, "sc") == 0) {
735 vty_selected = VTY_SC;
740 if (strcmp(vty_name, "vt") == 0) {
741 vty_selected = VTY_VT;
745 if (vty_prefer != 0) {
746 vty_selected = vty_prefer;
750 vty_selected = VTY_VT;
751 #elif defined(DEV_SC)
752 vty_selected = VTY_SC;
756 if (vty_selected == VTY_VT)
757 strcpy(vty_name, "vt");
758 else if (vty_selected == VTY_SC)
759 strcpy(vty_name, "sc");
761 return ((vty_selected & vty) != 0);
765 vty_set_preferred(unsigned vty)
770 vty_prefer &= ~VTY_SC;
773 vty_prefer &= ~VTY_VT;