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>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
58 #include <sys/msgbuf.h>
59 #include <sys/namei.h>
62 #include <sys/queue.h>
63 #include <sys/reboot.h>
64 #include <sys/sysctl.h>
68 #include <sys/vnode.h>
72 #include <machine/cpu.h>
73 #include <machine/clock.h>
75 static MALLOC_DEFINE(M_TTYCONS, "tty console", "tty console handling");
78 STAILQ_ENTRY(cn_device) cnd_next;
79 struct consdev *cnd_cn;
82 #define CNDEVPATHMAX 32
83 #define CNDEVTAB_SIZE 4
84 static struct cn_device cn_devtab[CNDEVTAB_SIZE];
85 static STAILQ_HEAD(, cn_device) cn_devlist =
86 STAILQ_HEAD_INITIALIZER(cn_devlist);
88 int cons_avail_mask = 0; /* Bit mask. Each registered low level console
89 * which is currently unavailable for inpit
90 * (i.e., if it is in graphics mode) will have
94 static char *consbuf; /* buffer used by `consmsgbuf' */
95 static struct callout conscallout; /* callout for outputting to constty */
96 struct msgbuf consmsgbuf; /* message buffer for console tty */
97 static u_char console_pausing; /* pause after each line during probe */
98 static char *console_pausestr=
99 "<pause; press any key to proceed to next line or '.' to end pause mode>";
100 struct tty *constty; /* pointer to console "window" tty */
101 static struct mtx cnputs_mtx; /* Mutex for cnputs(). */
102 static int use_cnputs_mtx = 0; /* != 0 if cnputs_mtx locking reqd. */
104 static void constty_timeout(void *arg);
106 static struct consdev cons_consdev;
107 DATA_SET(cons_set, cons_consdev);
108 SET_DECLARE(cons_set, struct consdev);
113 struct consdev *best_cn, *cn, **list;
116 * Check if we should mute the console (for security reasons perhaps)
117 * It can be changes dynamically using sysctl kern.consmute
118 * once we are up and going.
121 cn_mute = ((boothowto & (RB_MUTE
124 |RB_ASKNAME)) == RB_MUTE);
127 * Find the first console with the highest priority.
130 SET_FOREACH(list, cons_set) {
133 /* Skip cons_consdev. */
134 if (cn->cn_ops == NULL)
136 cn->cn_ops->cn_probe(cn);
137 if (cn->cn_pri == CN_DEAD)
139 if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
141 if (boothowto & RB_MULTIPLE) {
143 * Initialize console, and attach to it.
145 cn->cn_ops->cn_init(cn);
151 if ((boothowto & RB_MULTIPLE) == 0) {
152 best_cn->cn_ops->cn_init(best_cn);
155 if (boothowto & RB_PAUSE)
158 * Make the best console the preferred console.
164 * Release early console.
176 /* add a new physical console to back the virtual console */
178 cnadd(struct consdev *cn)
180 struct cn_device *cnd;
183 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
184 if (cnd->cnd_cn == cn)
186 for (i = 0; i < CNDEVTAB_SIZE; i++) {
188 if (cnd->cnd_cn == NULL)
191 if (cnd->cnd_cn != NULL)
194 if (cn->cn_name[0] == '\0') {
195 /* XXX: it is unclear if/where this print might output */
196 printf("WARNING: console at %p has no name\n", cn);
198 STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
199 if (STAILQ_FIRST(&cn_devlist) == cnd)
200 ttyconsdev_select(cnd->cnd_cn->cn_name);
202 /* Add device to the active mask. */
203 cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
209 cnremove(struct consdev *cn)
211 struct cn_device *cnd;
214 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
215 if (cnd->cnd_cn != cn)
217 if (STAILQ_FIRST(&cn_devlist) == cnd)
218 ttyconsdev_select(NULL);
219 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
222 /* Remove this device from available mask. */
223 for (i = 0; i < CNDEVTAB_SIZE; i++)
224 if (cnd == &cn_devtab[i]) {
225 cons_avail_mask &= ~(1 << i);
231 * syscons gets really confused if console resources are
232 * freed after the system has initialized.
234 if (cn->cn_term != NULL)
235 cn->cn_ops->cn_term(cn);
242 cnselect(struct consdev *cn)
244 struct cn_device *cnd;
246 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
247 if (cnd->cnd_cn != cn)
249 if (cnd == STAILQ_FIRST(&cn_devlist))
251 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
252 STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
253 ttyconsdev_select(cnd->cnd_cn->cn_name);
259 cnavailable(struct consdev *cn, int available)
263 for (i = 0; i < CNDEVTAB_SIZE; i++) {
264 if (cn_devtab[i].cnd_cn == cn)
268 if (i < CNDEVTAB_SIZE)
269 cons_avail_mask |= (1 << i);
270 cn->cn_flags &= ~CN_FLAG_NOAVAIL;
272 if (i < CNDEVTAB_SIZE)
273 cons_avail_mask &= ~(1 << i);
274 cn->cn_flags |= CN_FLAG_NOAVAIL;
282 return (cons_avail_mask == 0);
286 * sysctl_kern_console() provides output parseable in conscontrol(1).
289 sysctl_kern_console(SYSCTL_HANDLER_ARGS)
291 struct cn_device *cnd;
292 struct consdev *cp, **list;
297 sb = sbuf_new(NULL, NULL, CNDEVPATHMAX * 2, SBUF_AUTOEXTEND |
302 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
303 sbuf_printf(sb, "%s,", cnd->cnd_cn->cn_name);
304 sbuf_printf(sb, "/");
305 SET_FOREACH(list, cons_set) {
307 if (cp->cn_name[0] != '\0')
308 sbuf_printf(sb, "%s,", cp->cn_name);
311 error = sysctl_handle_string(oidp, sbuf_data(sb), sbuf_len(sb), req);
312 if (error == 0 && req->newptr != NULL) {
320 SET_FOREACH(list, cons_set) {
322 if (strcmp(p, cp->cn_name) != 0)
339 SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW,
340 0, 0, sysctl_kern_console, "A", "Console device control");
343 * User has changed the state of the console muting.
344 * This may require us to open or close the device in question.
347 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
351 error = sysctl_handle_int(oidp, &cn_mute, 0, req);
352 if (error != 0 || req->newptr == NULL)
357 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
358 0, sizeof(cn_mute), sysctl_kern_consmute, "I",
359 "State of the console muting");
364 struct cn_device *cnd;
367 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
369 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
370 cn->cn_ops->cn_grab(cn);
377 struct cn_device *cnd;
380 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
382 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
383 cn->cn_ops->cn_ungrab(cn);
390 struct cn_device *cnd;
393 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
395 if (cn->cn_ops->cn_resume != NULL)
396 cn->cn_ops->cn_resume(cn);
401 * Low level console routines.
410 while ((c = cncheckc()) == -1)
413 c = '\n'; /* console input is always ICRNL */
420 struct cn_device *cnd;
426 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
428 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
429 c = cn->cn_ops->cn_getc(cn);
438 cngets(char *cp, size_t size, int visible)
487 struct cn_device *cnd;
492 if (early_putc != NULL) {
500 if (cn_mute || c == '\0')
502 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
504 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
506 cn->cn_ops->cn_putc(cn, '\r');
507 cn->cn_ops->cn_putc(cn, c);
510 if (console_pausing && c == '\n' && !kdb_active) {
511 for (cp = console_pausestr; *cp != '\0'; cp++)
518 for (cp = console_pausestr; *cp != '\0'; cp++)
525 cnputsn(const char *p, size_t n)
530 if (use_cnputs_mtx) {
532 * NOTE: Debug prints and/or witness printouts in
533 * console driver clients can cause the "cnputs_mtx"
534 * mutex to recurse. Simply return if that happens.
536 if (mtx_owned(&cnputs_mtx))
538 mtx_lock_spin(&cnputs_mtx);
542 for (i = 0; i < n; i++)
546 mtx_unlock_spin(&cnputs_mtx);
552 cnputsn(p, strlen(p));
555 static int consmsgbuf_size = 8192;
556 SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
557 "Console tty buffer size");
560 * Redirect console output to a tty.
563 constty_set(struct tty *tp)
567 KASSERT(tp != NULL, ("constty_set: NULL tp"));
568 if (consbuf == NULL) {
569 size = consmsgbuf_size;
570 consbuf = malloc(size, M_TTYCONS, M_WAITOK);
571 msgbuf_init(&consmsgbuf, consbuf, size);
572 callout_init(&conscallout, 0);
575 constty_timeout(NULL);
579 * Disable console redirection to a tty.
589 callout_stop(&conscallout);
590 while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
592 free(consbuf, M_TTYCONS);
596 /* Times per second to check for pending console tty messages. */
597 static int constty_wakeups_per_second = 5;
598 SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
599 &constty_wakeups_per_second, 0,
600 "Times per second to check for pending console tty messages");
603 constty_timeout(void *arg)
607 if (constty != NULL) {
609 while ((c = msgbuf_getchar(&consmsgbuf)) != -1) {
610 if (tty_putchar(constty, c) < 0) {
620 if (constty != NULL) {
621 callout_reset(&conscallout, hz / constty_wakeups_per_second,
622 constty_timeout, NULL);
624 /* Deallocate the constty buffer memory. */
630 cn_drvinit(void *unused)
633 mtx_init(&cnputs_mtx, "cnputs_mtx", NULL, MTX_SPIN | MTX_NOWITNESS);
637 SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL);
640 * Sysbeep(), if we have hardware for it
643 #ifdef HAS_TIMER_SPKR
646 static struct callout beeping_timer;
649 sysbeepstop(void *chan)
652 timer_spkr_release();
657 sysbeep(int pitch, int period)
660 if (timer_spkr_acquire()) {
662 /* Something else owns it. */
666 timer_spkr_setfreq(pitch);
669 callout_reset(&beeping_timer, period, sysbeepstop, NULL);
675 sysbeep_init(void *unused)
678 callout_init(&beeping_timer, 1);
680 SYSINIT(sysbeep, SI_SUB_SOFTINTR, SI_ORDER_ANY, sysbeep_init, NULL);
684 * No hardware, no sound
688 sysbeep(int pitch __unused, int period __unused)
697 * Temporary support for sc(4) to vt(4) transition.
699 static unsigned vty_prefer;
700 static char vty_name[16];
701 SYSCTL_STRING(_kern, OID_AUTO, vty, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, vty_name,
702 0, "Console vty driver");
705 vty_enabled(unsigned vty)
707 static unsigned vty_selected = 0;
709 if (vty_selected == 0) {
710 TUNABLE_STR_FETCH("kern.vty", vty_name, sizeof(vty_name));
713 if (strcmp(vty_name, "sc") == 0) {
714 vty_selected = VTY_SC;
719 if (strcmp(vty_name, "vt") == 0) {
720 vty_selected = VTY_VT;
724 if (vty_prefer != 0) {
725 vty_selected = vty_prefer;
729 vty_selected = VTY_VT;
730 #elif defined(DEV_SC)
731 vty_selected = VTY_SC;
735 if (vty_selected == VTY_VT)
736 strcpy(vty_name, "vt");
737 else if (vty_selected == VTY_SC)
738 strcpy(vty_name, "sc");
740 return ((vty_selected & vty) != 0);
744 vty_set_preferred(unsigned vty)
749 vty_prefer &= ~VTY_SC;
752 vty_prefer &= ~VTY_VT;