2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991 The Regents of the University of California.
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: @(#)cons.c 7.2 (Berkeley) 5/9/91
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
46 #include <sys/fcntl.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/msgbuf.h>
51 #include <sys/namei.h>
53 #include <sys/queue.h>
54 #include <sys/reboot.h>
55 #include <sys/sysctl.h>
58 #include <sys/vnode.h>
62 #include <machine/cpu.h>
64 static d_open_t cnopen;
65 static d_close_t cnclose;
66 static d_read_t cnread;
67 static d_write_t cnwrite;
68 static d_ioctl_t cnioctl;
69 static d_poll_t cnpoll;
70 static d_kqfilter_t cnkqfilter;
72 static struct cdevsw cn_cdevsw = {
73 .d_version = D_VERSION,
81 .d_flags = D_TTY | D_NEEDGIANT,
82 .d_kqfilter = cnkqfilter,
86 STAILQ_ENTRY(cn_device) cnd_next;
88 struct consdev *cnd_cn;
91 #define CNDEVPATHMAX 32
92 #define CNDEVTAB_SIZE 4
93 static struct cn_device cn_devtab[CNDEVTAB_SIZE];
94 static STAILQ_HEAD(, cn_device) cn_devlist =
95 STAILQ_HEAD_INITIALIZER(cn_devlist);
97 #define CND_INVALID(cnd, td) \
98 (cnd == NULL || cnd->cnd_vp == NULL || \
99 (cnd->cnd_vp->v_type == VBAD && !cn_devopen(cnd, td, 1)))
101 static dev_t cn_udev_t;
102 SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD,
103 &cn_udev_t, sizeof cn_udev_t, "T,struct cdev *", "");
105 int cons_avail_mask = 0; /* Bit mask. Each registered low level console
106 * which is currently unavailable for inpit
107 * (i.e., if it is in graphics mode) will have
111 static int openflag; /* how /dev/console was opened */
112 static int cn_is_open;
113 static char *consbuf; /* buffer used by `consmsgbuf' */
114 static struct callout conscallout; /* callout for outputting to constty */
115 struct msgbuf consmsgbuf; /* message buffer for console tty */
116 static u_char console_pausing; /* pause after each line during probe */
117 static char *console_pausestr=
118 "<pause; press any key to proceed to next line or '.' to end pause mode>";
119 struct tty *constty; /* pointer to console "window" tty */
121 static void constty_timeout(void *arg);
123 CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
124 SET_DECLARE(cons_set, struct consdev);
129 struct consdev *best_cn, *cn, **list;
132 * Check if we should mute the console (for security reasons perhaps)
133 * It can be changes dynamically using sysctl kern.consmute
134 * once we are up and going.
137 cn_mute = ((boothowto & (RB_MUTE
140 |RB_ASKNAME)) == RB_MUTE);
143 * Find the first console with the highest priority.
146 SET_FOREACH(list, cons_set) {
149 if (cn->cn_probe == NULL)
152 if (cn->cn_pri == CN_DEAD)
154 if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
156 if (boothowto & RB_MULTIPLE) {
158 * Initialize console, and attach to it.
166 if ((boothowto & RB_MULTIPLE) == 0) {
168 best_cn->cn_init(best_cn);
170 if (boothowto & RB_PAUSE)
173 * Make the best console the preferred console.
184 /* add a new physical console to back the virtual console */
186 cnadd(struct consdev *cn)
188 struct cn_device *cnd;
191 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
192 if (cnd->cnd_cn == cn)
194 for (i = 0; i < CNDEVTAB_SIZE; i++) {
196 if (cnd->cnd_cn == NULL)
199 if (cnd->cnd_cn != NULL)
202 if (cn->cn_name[0] == '\0') {
203 /* XXX: it is unclear if/where this print might output */
204 printf("WARNING: console at %p has no name\n", cn);
206 STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
208 /* Add device to the active mask. */
209 cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
215 cnremove(struct consdev *cn)
217 struct cn_device *cnd;
220 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
221 if (cnd->cnd_cn != cn)
223 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
224 if (cnd->cnd_vp != NULL)
225 vn_close(cnd->cnd_vp, openflag, NOCRED, NULL);
229 /* Remove this device from available mask. */
230 for (i = 0; i < CNDEVTAB_SIZE; i++)
231 if (cnd == &cn_devtab[i]) {
232 cons_avail_mask &= ~(1 << i);
238 * syscons gets really confused if console resources are
239 * freed after the system has initialized.
241 if (cn->cn_term != NULL)
249 cnselect(struct consdev *cn)
251 struct cn_device *cnd;
253 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
254 if (cnd->cnd_cn != cn)
256 if (cnd == STAILQ_FIRST(&cn_devlist))
258 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
259 STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
265 cnavailable(struct consdev *cn, int available)
269 for (i = 0; i < CNDEVTAB_SIZE; i++) {
270 if (cn_devtab[i].cnd_cn == cn)
274 if (i < CNDEVTAB_SIZE)
275 cons_avail_mask |= (1 << i);
276 cn->cn_flags &= ~CN_FLAG_NOAVAIL;
278 if (i < CNDEVTAB_SIZE)
279 cons_avail_mask &= ~(1 << i);
280 cn->cn_flags |= CN_FLAG_NOAVAIL;
288 return (cons_avail_mask == 0);
292 * XXX: rewrite to use sbufs instead
296 sysctl_kern_console(SYSCTL_HANDLER_ARGS)
298 struct cn_device *cnd;
299 struct consdev *cp, **list;
301 int delete, len, error;
304 SET_FOREACH(list, cons_set) {
306 if (cp->cn_name[0] != '\0')
307 len += strlen(cp->cn_name) + 1;
309 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
310 len += strlen(cnd->cnd_cn->cn_name) + 1;
311 len = len > CNDEVPATHMAX ? len : CNDEVPATHMAX;
312 MALLOC(name, char *, len, M_TEMP, M_WAITOK | M_ZERO);
314 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
315 p += sprintf(p, "%s,", cnd->cnd_cn->cn_name);
317 SET_FOREACH(list, cons_set) {
319 if (cp->cn_name[0] != '\0')
320 p += sprintf(p, "%s,", cp->cn_name);
322 error = sysctl_handle_string(oidp, name, len, req);
323 if (error == 0 && req->newptr != NULL) {
331 SET_FOREACH(list, cons_set) {
333 if (strcmp(p, cp->cn_name) != 0)
350 SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW,
351 0, 0, sysctl_kern_console, "A", "Console device control");
354 * User has changed the state of the console muting.
355 * This may require us to open or close the device in question.
358 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
364 error = sysctl_handle_int(oidp, &cn_mute, 0, req);
365 if (error != 0 || req->newptr == NULL)
367 if (ocn_mute && !cn_mute && cn_is_open)
368 error = cnopen(NULL, openflag, 0, curthread);
369 else if (!ocn_mute && cn_mute && cn_is_open) {
370 error = cnclose(NULL, openflag, 0, curthread);
371 cn_is_open = 1; /* XXX hack */
376 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
377 0, sizeof(cn_mute), sysctl_kern_consmute, "I", "");
380 cn_devopen(struct cn_device *cnd, struct thread *td, int forceopen)
382 char path[CNDEVPATHMAX];
389 if ((vp = cnd->cnd_vp) != NULL) {
390 if (!forceopen && vp->v_type != VBAD) {
392 csw = dev_refthread(dev);
395 error = (*csw->d_open)(dev, openflag, 0, td);
400 vn_close(vp, openflag, td->td_ucred, td);
402 snprintf(path, sizeof(path), "/dev/%s", cnd->cnd_cn->cn_name);
403 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, td);
404 error = vn_open(&nd, &openflag, 0, -1);
406 NDFREE(&nd, NDF_ONLY_PNBUF);
407 VOP_UNLOCK(nd.ni_vp, 0, td);
408 if (nd.ni_vp->v_type == VCHR)
409 cnd->cnd_vp = nd.ni_vp;
411 vn_close(nd.ni_vp, openflag, td->td_ucred, td);
413 return (cnd->cnd_vp != NULL);
417 cnopen(struct cdev *dev, int flag, int mode, struct thread *td)
419 struct cn_device *cnd;
421 openflag = flag | FWRITE; /* XXX */
422 cn_is_open = 1; /* console is logically open */
425 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
426 cn_devopen(cnd, td, 0);
431 cnclose(struct cdev *dev, int flag, int mode, struct thread *td)
433 struct cn_device *cnd;
436 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
437 if ((vp = cnd->cnd_vp) == NULL)
440 vn_close(vp, openflag, td->td_ucred, td);
447 cnread(struct cdev *dev, struct uio *uio, int flag)
449 struct cn_device *cnd;
453 cnd = STAILQ_FIRST(&cn_devlist);
454 if (cn_mute || CND_INVALID(cnd, curthread))
456 dev = cnd->cnd_vp->v_rdev;
457 csw = dev_refthread(dev);
460 error = (csw->d_read)(dev, uio, flag);
466 cnwrite(struct cdev *dev, struct uio *uio, int flag)
468 struct cn_device *cnd;
472 cnd = STAILQ_FIRST(&cn_devlist);
473 if (cn_mute || CND_INVALID(cnd, curthread))
476 dev = constty->t_dev;
478 dev = cnd->cnd_vp->v_rdev;
481 csw = dev_refthread(dev);
484 error = (csw->d_write)(dev, uio, flag);
489 uio->uio_resid = 0; /* dump the data */
494 cnioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
496 struct cn_device *cnd;
500 cnd = STAILQ_FIRST(&cn_devlist);
501 if (cn_mute || CND_INVALID(cnd, td))
504 * Superuser can always use this to wrest control of console
505 * output from the "virtual" console.
507 if (cmd == TIOCCONS && constty) {
514 dev = cnd->cnd_vp->v_rdev;
516 return (0); /* XXX : ENOTTY ? */
517 csw = dev_refthread(dev);
520 error = (csw->d_ioctl)(dev, cmd, data, flag, td);
527 * poll/kqfilter do not appear to be correct
530 cnpoll(struct cdev *dev, int events, struct thread *td)
532 struct cn_device *cnd;
536 cnd = STAILQ_FIRST(&cn_devlist);
537 if (cn_mute || CND_INVALID(cnd, td))
539 dev = cnd->cnd_vp->v_rdev;
542 csw = dev_refthread(dev);
545 error = (csw->d_poll)(dev, events, td);
551 cnkqfilter(struct cdev *dev, struct knote *kn)
553 struct cn_device *cnd;
557 cnd = STAILQ_FIRST(&cn_devlist);
558 if (cn_mute || CND_INVALID(cnd, curthread))
560 dev = cnd->cnd_vp->v_rdev;
563 csw = dev_refthread(dev);
566 error = (csw->d_kqfilter)(dev, kn);
572 * Low level console routines.
581 while ((c = cncheckc()) == -1)
584 c = '\n'; /* console input is always ICRNL */
591 struct cn_device *cnd;
597 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
599 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
600 c = cn->cn_checkc(cn);
612 struct cn_device *cnd;
616 if (cn_mute || c == '\0')
618 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
620 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
622 cn->cn_putc(cn, '\r');
626 if (console_pausing && c == '\n' && !kdb_active) {
627 for (cp = console_pausestr; *cp != '\0'; cp++)
632 for (cp = console_pausestr; *cp != '\0'; cp++)
641 struct cn_device *cnd;
648 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
650 if (cn->cn_dbctl != NULL)
651 cn->cn_dbctl(cn, on);
657 static int consmsgbuf_size = 8192;
658 SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
662 * Redirect console output to a tty.
665 constty_set(struct tty *tp)
669 KASSERT(tp != NULL, ("constty_set: NULL tp"));
670 if (consbuf == NULL) {
671 size = consmsgbuf_size;
672 consbuf = malloc(size, M_TTYS, M_WAITOK);
673 msgbuf_init(&consmsgbuf, consbuf, size);
674 callout_init(&conscallout, 0);
677 constty_timeout(NULL);
681 * Disable console redirection to a tty.
691 callout_stop(&conscallout);
692 while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
694 free(consbuf, M_TTYS);
698 /* Times per second to check for pending console tty messages. */
699 static int constty_wakeups_per_second = 5;
700 SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
701 &constty_wakeups_per_second, 0, "");
704 constty_timeout(void *arg)
708 while (constty != NULL && (c = msgbuf_getchar(&consmsgbuf)) != -1) {
709 if (tputchar(c, constty) < 0)
712 if (constty != NULL) {
713 callout_reset(&conscallout, hz / constty_wakeups_per_second,
714 constty_timeout, NULL);
716 /* Deallocate the constty buffer memory. */
722 cn_drvinit(void *unused)
725 make_dev(&cn_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "console");
728 SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL)