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>
45 #include <sys/mutex.h>
48 #include <sys/fcntl.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/msgbuf.h>
53 #include <sys/namei.h>
56 #include <sys/queue.h>
57 #include <sys/reboot.h>
58 #include <sys/sysctl.h>
62 #include <sys/vnode.h>
66 #include <machine/cpu.h>
68 static d_open_t cnopen;
69 static d_close_t cnclose;
70 static d_read_t cnread;
71 static d_write_t cnwrite;
72 static d_ioctl_t cnioctl;
73 static d_poll_t cnpoll;
74 static d_kqfilter_t cnkqfilter;
76 static struct cdevsw cn_cdevsw = {
77 .d_version = D_VERSION,
85 .d_flags = D_TTY | D_NEEDGIANT,
86 .d_kqfilter = cnkqfilter,
90 STAILQ_ENTRY(cn_device) cnd_next;
92 struct consdev *cnd_cn;
95 #define CNDEVPATHMAX 32
96 #define CNDEVTAB_SIZE 4
97 static struct cn_device cn_devtab[CNDEVTAB_SIZE];
98 static STAILQ_HEAD(, cn_device) cn_devlist =
99 STAILQ_HEAD_INITIALIZER(cn_devlist);
101 #define CND_INVALID(cnd, td) \
102 (cnd == NULL || cnd->cnd_vp == NULL || \
103 (cnd->cnd_vp->v_type == VBAD && !cn_devopen(cnd, td, 1)))
105 static dev_t cn_udev_t;
106 SYSCTL_OPAQUE(_machdep, OID_AUTO, consdev, CTLFLAG_RD,
107 &cn_udev_t, sizeof cn_udev_t, "T,struct cdev *", "");
109 int cons_avail_mask = 0; /* Bit mask. Each registered low level console
110 * which is currently unavailable for inpit
111 * (i.e., if it is in graphics mode) will have
115 static int openflag; /* how /dev/console was opened */
116 static int cn_is_open;
117 static char *consbuf; /* buffer used by `consmsgbuf' */
118 static struct callout conscallout; /* callout for outputting to constty */
119 struct msgbuf consmsgbuf; /* message buffer for console tty */
120 static u_char console_pausing; /* pause after each line during probe */
121 static char *console_pausestr=
122 "<pause; press any key to proceed to next line or '.' to end pause mode>";
123 struct tty *constty; /* pointer to console "window" tty */
124 static struct mtx cnputs_mtx; /* Mutex for cnputs(). */
125 static int use_cnputs_mtx = 0; /* != 0 if cnputs_mtx locking reqd. */
127 static void constty_timeout(void *arg);
129 static struct consdev cons_consdev;
130 DATA_SET(cons_set, cons_consdev);
131 SET_DECLARE(cons_set, struct consdev);
136 struct consdev *best_cn, *cn, **list;
139 * Check if we should mute the console (for security reasons perhaps)
140 * It can be changes dynamically using sysctl kern.consmute
141 * once we are up and going.
144 cn_mute = ((boothowto & (RB_MUTE
147 |RB_ASKNAME)) == RB_MUTE);
150 * Find the first console with the highest priority.
153 SET_FOREACH(list, cons_set) {
156 if (cn->cn_probe == NULL)
159 if (cn->cn_pri == CN_DEAD)
161 if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
163 if (boothowto & RB_MULTIPLE) {
165 * Initialize console, and attach to it.
173 if ((boothowto & RB_MULTIPLE) == 0) {
174 best_cn->cn_init(best_cn);
177 if (boothowto & RB_PAUSE)
180 * Make the best console the preferred console.
191 /* add a new physical console to back the virtual console */
193 cnadd(struct consdev *cn)
195 struct cn_device *cnd;
198 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
199 if (cnd->cnd_cn == cn)
201 for (i = 0; i < CNDEVTAB_SIZE; i++) {
203 if (cnd->cnd_cn == NULL)
206 if (cnd->cnd_cn != NULL)
209 if (cn->cn_name[0] == '\0') {
210 /* XXX: it is unclear if/where this print might output */
211 printf("WARNING: console at %p has no name\n", cn);
213 STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
215 /* Add device to the active mask. */
216 cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
222 cnremove(struct consdev *cn)
224 struct cn_device *cnd;
227 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
228 if (cnd->cnd_cn != cn)
230 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
231 if (cnd->cnd_vp != NULL)
232 vn_close(cnd->cnd_vp, openflag, NOCRED, NULL);
236 /* Remove this device from available mask. */
237 for (i = 0; i < CNDEVTAB_SIZE; i++)
238 if (cnd == &cn_devtab[i]) {
239 cons_avail_mask &= ~(1 << i);
245 * syscons gets really confused if console resources are
246 * freed after the system has initialized.
248 if (cn->cn_term != NULL)
256 cnselect(struct consdev *cn)
258 struct cn_device *cnd;
260 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
261 if (cnd->cnd_cn != cn)
263 if (cnd == STAILQ_FIRST(&cn_devlist))
265 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
266 STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
272 cnavailable(struct consdev *cn, int available)
276 for (i = 0; i < CNDEVTAB_SIZE; i++) {
277 if (cn_devtab[i].cnd_cn == cn)
281 if (i < CNDEVTAB_SIZE)
282 cons_avail_mask |= (1 << i);
283 cn->cn_flags &= ~CN_FLAG_NOAVAIL;
285 if (i < CNDEVTAB_SIZE)
286 cons_avail_mask &= ~(1 << i);
287 cn->cn_flags |= CN_FLAG_NOAVAIL;
295 return (cons_avail_mask == 0);
299 * sysctl_kern_console() provides output parseable in conscontrol(1).
302 sysctl_kern_console(SYSCTL_HANDLER_ARGS)
304 struct cn_device *cnd;
305 struct consdev *cp, **list;
310 sb = sbuf_new(NULL, NULL, CNDEVPATHMAX * 2, SBUF_AUTOEXTEND);
314 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
315 sbuf_printf(sb, "%s,", cnd->cnd_cn->cn_name);
316 sbuf_printf(sb, "/");
317 SET_FOREACH(list, cons_set) {
319 if (cp->cn_name[0] != '\0')
320 sbuf_printf(sb, "%s,", cp->cn_name);
323 error = sysctl_handle_string(oidp, sbuf_data(sb), sbuf_len(sb), req);
324 if (error == 0 && req->newptr != NULL) {
332 SET_FOREACH(list, cons_set) {
334 if (strcmp(p, cp->cn_name) != 0)
351 SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW,
352 0, 0, sysctl_kern_console, "A", "Console device control");
355 * User has changed the state of the console muting.
356 * This may require us to open or close the device in question.
359 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
365 error = sysctl_handle_int(oidp, &cn_mute, 0, req);
366 if (error != 0 || req->newptr == NULL)
368 if (ocn_mute && !cn_mute && cn_is_open)
369 error = cnopen(NULL, openflag, 0, curthread);
370 else if (!ocn_mute && cn_mute && cn_is_open) {
371 error = cnclose(NULL, openflag, 0, curthread);
372 cn_is_open = 1; /* XXX hack */
377 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
378 0, sizeof(cn_mute), sysctl_kern_consmute, "I", "");
381 cn_devopen(struct cn_device *cnd, struct thread *td, int forceopen)
383 char path[CNDEVPATHMAX];
390 if ((vp = cnd->cnd_vp) != NULL) {
391 if (!forceopen && vp->v_type != VBAD) {
393 csw = dev_refthread(dev);
396 error = (*csw->d_open)(dev, openflag, 0, td);
401 vn_close(vp, openflag, td->td_ucred, td);
403 snprintf(path, sizeof(path), "/dev/%s", cnd->cnd_cn->cn_name);
404 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, td);
405 error = vn_open(&nd, &openflag, 0, NULL);
407 NDFREE(&nd, NDF_ONLY_PNBUF);
408 VOP_UNLOCK(nd.ni_vp, 0);
409 if (nd.ni_vp->v_type == VCHR)
410 cnd->cnd_vp = nd.ni_vp;
412 vn_close(nd.ni_vp, openflag, td->td_ucred, td);
414 return (cnd->cnd_vp != NULL);
418 cnopen(struct cdev *dev, int flag, int mode, struct thread *td)
420 struct cn_device *cnd;
422 openflag = flag | FWRITE; /* XXX */
423 cn_is_open = 1; /* console is logically open */
426 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
427 cn_devopen(cnd, td, 0);
432 cnclose(struct cdev *dev, int flag, int mode, struct thread *td)
434 struct cn_device *cnd;
437 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
438 if ((vp = cnd->cnd_vp) == NULL)
441 vn_close(vp, openflag, td->td_ucred, td);
448 cnread(struct cdev *dev, struct uio *uio, int flag)
450 struct cn_device *cnd;
454 cnd = STAILQ_FIRST(&cn_devlist);
455 if (cn_mute || CND_INVALID(cnd, curthread))
457 dev = cnd->cnd_vp->v_rdev;
458 csw = dev_refthread(dev);
461 error = (csw->d_read)(dev, uio, flag);
467 cnwrite(struct cdev *dev, struct uio *uio, int flag)
469 struct cn_device *cnd;
473 cnd = STAILQ_FIRST(&cn_devlist);
474 if (cn_mute || CND_INVALID(cnd, curthread))
477 dev = constty->t_dev;
479 dev = cnd->cnd_vp->v_rdev;
482 csw = dev_refthread(dev);
485 error = (csw->d_write)(dev, uio, flag);
490 uio->uio_resid = 0; /* dump the data */
495 cnioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
497 struct cn_device *cnd;
501 cnd = STAILQ_FIRST(&cn_devlist);
502 if (cn_mute || CND_INVALID(cnd, td))
505 * Superuser can always use this to wrest control of console
506 * output from the "virtual" console.
508 if (cmd == TIOCCONS && constty) {
509 error = priv_check(td, PRIV_TTY_CONSOLE);
515 dev = cnd->cnd_vp->v_rdev;
517 return (0); /* XXX : ENOTTY ? */
518 csw = dev_refthread(dev);
521 error = (csw->d_ioctl)(dev, cmd, data, flag, td);
528 * poll/kqfilter do not appear to be correct
531 cnpoll(struct cdev *dev, int events, struct thread *td)
533 struct cn_device *cnd;
537 cnd = STAILQ_FIRST(&cn_devlist);
538 if (cn_mute || CND_INVALID(cnd, td))
540 dev = cnd->cnd_vp->v_rdev;
543 csw = dev_refthread(dev);
546 error = (csw->d_poll)(dev, events, td);
552 cnkqfilter(struct cdev *dev, struct knote *kn)
554 struct cn_device *cnd;
558 cnd = STAILQ_FIRST(&cn_devlist);
559 if (cn_mute || CND_INVALID(cnd, curthread))
561 dev = cnd->cnd_vp->v_rdev;
564 csw = dev_refthread(dev);
567 error = (csw->d_kqfilter)(dev, kn);
573 * Low level console routines.
582 while ((c = cncheckc()) == -1)
585 c = '\n'; /* console input is always ICRNL */
592 struct cn_device *cnd;
598 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
600 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
601 if (cn->cn_checkc != NULL)
602 c = cn->cn_checkc(cn);
616 struct cn_device *cnd;
620 if (cn_mute || c == '\0')
622 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
624 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
626 cn->cn_putc(cn, '\r');
630 if (console_pausing && c == '\n' && !kdb_active) {
631 for (cp = console_pausestr; *cp != '\0'; cp++)
636 for (cp = console_pausestr; *cp != '\0'; cp++)
648 if (use_cnputs_mtx) {
649 mtx_lock_spin(&cnputs_mtx);
653 while ((c = *p++) != '\0')
657 mtx_unlock_spin(&cnputs_mtx);
660 static int consmsgbuf_size = 8192;
661 SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
665 * Redirect console output to a tty.
668 constty_set(struct tty *tp)
672 KASSERT(tp != NULL, ("constty_set: NULL tp"));
673 if (consbuf == NULL) {
674 size = consmsgbuf_size;
675 consbuf = malloc(size, M_TTYS, M_WAITOK);
676 msgbuf_init(&consmsgbuf, consbuf, size);
677 callout_init(&conscallout, 0);
680 constty_timeout(NULL);
684 * Disable console redirection to a tty.
694 callout_stop(&conscallout);
695 while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
697 free(consbuf, M_TTYS);
701 /* Times per second to check for pending console tty messages. */
702 static int constty_wakeups_per_second = 5;
703 SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
704 &constty_wakeups_per_second, 0, "");
707 constty_timeout(void *arg)
711 while (constty != NULL && (c = msgbuf_getchar(&consmsgbuf)) != -1) {
712 if (tputchar(c, constty) < 0)
715 if (constty != NULL) {
716 callout_reset(&conscallout, hz / constty_wakeups_per_second,
717 constty_timeout, NULL);
719 /* Deallocate the constty buffer memory. */
725 cn_drvinit(void *unused)
728 make_dev(&cn_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "console");
730 mtx_init(&cnputs_mtx, "cnputs_mtx", NULL, MTX_SPIN | MTX_NOWITNESS);
734 SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL);