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>
69 #include <sys/vnode.h>
73 #include <dev/kbd/kbdreg.h>
75 #include <machine/cpu.h>
76 #include <machine/clock.h>
78 static MALLOC_DEFINE(M_TTYCONS, "tty console", "tty console handling");
81 STAILQ_ENTRY(cn_device) cnd_next;
82 struct consdev *cnd_cn;
85 #define CNDEVPATHMAX 32
86 #define CNDEVTAB_SIZE 4
87 static struct cn_device cn_devtab[CNDEVTAB_SIZE];
88 static STAILQ_HEAD(, cn_device) cn_devlist =
89 STAILQ_HEAD_INITIALIZER(cn_devlist);
91 int cons_avail_mask = 0; /* Bit mask. Each registered low level console
92 * which is currently unavailable for inpit
93 * (i.e., if it is in graphics mode) will have
98 SYSCTL_INT(_kern, OID_AUTO, consmute, CTLFLAG_RW, &cn_mute, 0,
99 "State of the console muting");
101 static char *consbuf; /* buffer used by `consmsgbuf' */
102 static struct callout conscallout; /* callout for outputting to constty */
103 struct msgbuf consmsgbuf; /* message buffer for console tty */
104 static u_char console_pausing; /* pause after each line during probe */
105 static const char console_pausestr[] =
106 "<pause; press any key to proceed to next line or '.' to end pause mode>";
107 struct tty *constty; /* pointer to console "window" tty */
108 static struct mtx cnputs_mtx; /* Mutex for cnputs(). */
109 static int use_cnputs_mtx = 0; /* != 0 if cnputs_mtx locking reqd. */
111 static void constty_timeout(void *arg);
113 static struct consdev cons_consdev;
114 DATA_SET(cons_set, cons_consdev);
115 SET_DECLARE(cons_set, struct consdev);
118 * Stub for configurations that don't actually have a keyboard driver. Inclusion
119 * of kbd.c is contingent on any number of keyboard/console drivers being
120 * present in the kernel; rather than trying to catch them all, we'll just
121 * maintain this weak kbdinit that will be overridden by the strong version in
122 * kbd.c if it's present.
133 struct consdev *best_cn, *cn, **list;
136 * Check if we should mute the console (for security reasons perhaps)
137 * It can be changes dynamically using sysctl kern.consmute
138 * once we are up and going.
141 cn_mute = ((boothowto & (RB_MUTE
144 |RB_ASKNAME)) == RB_MUTE);
147 * Bring up the kbd layer just in time for cnprobe. Console drivers
148 * have a dependency on kbd being ready, so this fits nicely between the
149 * machdep callers of cninit() and MI probing/initialization of consoles
155 * Find the first console with the highest priority.
158 SET_FOREACH(list, cons_set) {
161 /* Skip cons_consdev. */
162 if (cn->cn_ops == NULL)
164 cn->cn_ops->cn_probe(cn);
165 if (cn->cn_pri == CN_DEAD)
167 if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
169 if (boothowto & RB_MULTIPLE) {
171 * Initialize console, and attach to it.
173 cn->cn_ops->cn_init(cn);
179 if ((boothowto & RB_MULTIPLE) == 0) {
180 best_cn->cn_ops->cn_init(best_cn);
183 if (boothowto & RB_PAUSE)
186 * Make the best console the preferred console.
192 * Release early console.
204 /* add a new physical console to back the virtual console */
206 cnadd(struct consdev *cn)
208 struct cn_device *cnd;
211 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
212 if (cnd->cnd_cn == cn)
214 for (i = 0; i < CNDEVTAB_SIZE; i++) {
216 if (cnd->cnd_cn == NULL)
219 if (cnd->cnd_cn != NULL)
222 if (cn->cn_name[0] == '\0') {
223 /* XXX: it is unclear if/where this print might output */
224 printf("WARNING: console at %p has no name\n", cn);
226 STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
227 if (STAILQ_FIRST(&cn_devlist) == cnd)
228 ttyconsdev_select(cnd->cnd_cn->cn_name);
230 /* Add device to the active mask. */
231 cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
237 cnremove(struct consdev *cn)
239 struct cn_device *cnd;
242 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
243 if (cnd->cnd_cn != cn)
245 if (STAILQ_FIRST(&cn_devlist) == cnd)
246 ttyconsdev_select(NULL);
247 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
250 /* Remove this device from available mask. */
251 for (i = 0; i < CNDEVTAB_SIZE; i++)
252 if (cnd == &cn_devtab[i]) {
253 cons_avail_mask &= ~(1 << i);
259 * syscons gets really confused if console resources are
260 * freed after the system has initialized.
262 if (cn->cn_term != NULL)
263 cn->cn_ops->cn_term(cn);
270 cnselect(struct consdev *cn)
272 struct cn_device *cnd;
274 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
275 if (cnd->cnd_cn != cn)
277 if (cnd == STAILQ_FIRST(&cn_devlist))
279 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
280 STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
281 ttyconsdev_select(cnd->cnd_cn->cn_name);
287 cnavailable(struct consdev *cn, int available)
291 for (i = 0; i < CNDEVTAB_SIZE; i++) {
292 if (cn_devtab[i].cnd_cn == cn)
296 if (i < CNDEVTAB_SIZE)
297 cons_avail_mask |= (1 << i);
298 cn->cn_flags &= ~CN_FLAG_NOAVAIL;
300 if (i < CNDEVTAB_SIZE)
301 cons_avail_mask &= ~(1 << i);
302 cn->cn_flags |= CN_FLAG_NOAVAIL;
310 return (cons_avail_mask == 0);
314 * sysctl_kern_console() provides output parseable in conscontrol(1).
317 sysctl_kern_console(SYSCTL_HANDLER_ARGS)
319 struct cn_device *cnd;
320 struct consdev *cp, **list;
325 sb = sbuf_new(NULL, NULL, CNDEVPATHMAX * 2, SBUF_AUTOEXTEND |
330 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
331 sbuf_printf(sb, "%s,", cnd->cnd_cn->cn_name);
332 sbuf_printf(sb, "/");
333 SET_FOREACH(list, cons_set) {
335 if (cp->cn_name[0] != '\0')
336 sbuf_printf(sb, "%s,", cp->cn_name);
339 error = sysctl_handle_string(oidp, sbuf_data(sb), sbuf_len(sb), req);
340 if (error == 0 && req->newptr != NULL) {
348 SET_FOREACH(list, cons_set) {
350 if (strcmp(p, cp->cn_name) != 0)
367 SYSCTL_PROC(_kern, OID_AUTO, console,
368 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0,
369 sysctl_kern_console, "A",
370 "Console device control");
375 struct cn_device *cnd;
378 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
380 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
381 cn->cn_ops->cn_grab(cn);
388 struct cn_device *cnd;
391 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
393 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
394 cn->cn_ops->cn_ungrab(cn);
401 struct cn_device *cnd;
404 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
406 if (cn->cn_ops->cn_resume != NULL)
407 cn->cn_ops->cn_resume(cn);
412 * Low level console routines.
421 while ((c = cncheckc()) == -1)
424 c = '\n'; /* console input is always ICRNL */
431 struct cn_device *cnd;
437 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
439 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
440 c = cn->cn_ops->cn_getc(cn);
449 cngets(char *cp, size_t size, int visible)
498 struct cn_device *cnd;
503 if (early_putc != NULL) {
511 if (cn_mute || c == '\0')
513 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
515 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
517 cn->cn_ops->cn_putc(cn, '\r');
518 cn->cn_ops->cn_putc(cn, c);
521 if (console_pausing && c == '\n' && !kdb_active) {
522 for (cp = console_pausestr; *cp != '\0'; cp++)
529 for (cp = console_pausestr; *cp != '\0'; cp++)
536 cnputsn(const char *p, size_t n)
541 if (use_cnputs_mtx) {
543 * NOTE: Debug prints and/or witness printouts in
544 * console driver clients can cause the "cnputs_mtx"
545 * mutex to recurse. Simply return if that happens.
547 if (mtx_owned(&cnputs_mtx))
549 mtx_lock_spin(&cnputs_mtx);
553 for (i = 0; i < n; i++)
557 mtx_unlock_spin(&cnputs_mtx);
561 cnputs(const char *p)
563 cnputsn(p, strlen(p));
566 static int consmsgbuf_size = 8192;
567 SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
568 "Console tty buffer size");
571 * Redirect console output to a tty.
574 constty_set(struct tty *tp)
578 KASSERT(tp != NULL, ("constty_set: NULL tp"));
579 if (consbuf == NULL) {
580 size = consmsgbuf_size;
581 consbuf = malloc(size, M_TTYCONS, M_WAITOK);
582 msgbuf_init(&consmsgbuf, consbuf, size);
583 callout_init(&conscallout, 0);
586 constty_timeout(NULL);
590 * Disable console redirection to a tty.
600 callout_stop(&conscallout);
601 while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
603 free(consbuf, M_TTYCONS);
607 /* Times per second to check for pending console tty messages. */
608 static int constty_wakeups_per_second = 5;
609 SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
610 &constty_wakeups_per_second, 0,
611 "Times per second to check for pending console tty messages");
614 constty_timeout(void *arg)
618 if (constty != NULL) {
620 while ((c = msgbuf_getchar(&consmsgbuf)) != -1) {
621 if (tty_putchar(constty, c) < 0) {
631 if (constty != NULL) {
632 callout_reset(&conscallout, hz / constty_wakeups_per_second,
633 constty_timeout, NULL);
635 /* Deallocate the constty buffer memory. */
641 cn_drvinit(void *unused)
644 mtx_init(&cnputs_mtx, "cnputs_mtx", NULL, MTX_SPIN | MTX_NOWITNESS);
648 SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL);
651 * Sysbeep(), if we have hardware for it
654 #ifdef HAS_TIMER_SPKR
657 static struct callout beeping_timer;
660 sysbeepstop(void *chan)
663 timer_spkr_release();
668 sysbeep(int pitch, int period)
671 if (timer_spkr_acquire()) {
673 /* Something else owns it. */
677 timer_spkr_setfreq(pitch);
680 callout_reset(&beeping_timer, period, sysbeepstop, NULL);
686 sysbeep_init(void *unused)
689 callout_init(&beeping_timer, 1);
691 SYSINIT(sysbeep, SI_SUB_SOFTINTR, SI_ORDER_ANY, sysbeep_init, NULL);
695 * No hardware, no sound
699 sysbeep(int pitch __unused, int period __unused)
708 * Temporary support for sc(4) to vt(4) transition.
710 static unsigned vty_prefer;
711 static char vty_name[16];
712 SYSCTL_STRING(_kern, OID_AUTO, vty, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, vty_name,
713 0, "Console vty driver");
716 vty_enabled(unsigned vty)
718 static unsigned vty_selected = 0;
720 if (vty_selected == 0) {
721 TUNABLE_STR_FETCH("kern.vty", vty_name, sizeof(vty_name));
724 if (strcmp(vty_name, "sc") == 0) {
725 vty_selected = VTY_SC;
730 if (strcmp(vty_name, "vt") == 0) {
731 vty_selected = VTY_VT;
735 if (vty_prefer != 0) {
736 vty_selected = vty_prefer;
740 vty_selected = VTY_VT;
741 #elif defined(DEV_SC)
742 vty_selected = VTY_SC;
746 if (vty_selected == VTY_VT)
747 strcpy(vty_name, "vt");
748 else if (vty_selected == VTY_SC)
749 strcpy(vty_name, "sc");
751 return ((vty_selected & vty) != 0);
755 vty_set_preferred(unsigned vty)
760 vty_prefer &= ~VTY_SC;
763 vty_prefer &= ~VTY_VT;