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>
61 #include <sys/vnode.h>
65 #include <machine/cpu.h>
67 static d_open_t cnopen;
68 static d_close_t cnclose;
69 static d_read_t cnread;
70 static d_write_t cnwrite;
71 static d_ioctl_t cnioctl;
72 static d_poll_t cnpoll;
73 static d_kqfilter_t cnkqfilter;
75 static struct cdevsw cn_cdevsw = {
76 .d_version = D_VERSION,
84 .d_flags = D_TTY | D_NEEDGIANT,
85 .d_kqfilter = cnkqfilter,
89 STAILQ_ENTRY(cn_device) cnd_next;
91 struct consdev *cnd_cn;
94 #define CNDEVPATHMAX 32
95 #define CNDEVTAB_SIZE 4
96 static struct cn_device cn_devtab[CNDEVTAB_SIZE];
97 static STAILQ_HEAD(, cn_device) cn_devlist =
98 STAILQ_HEAD_INITIALIZER(cn_devlist);
100 #define CND_INVALID(cnd, td) \
101 (cnd == NULL || cnd->cnd_vp == NULL || \
102 (cnd->cnd_vp->v_type == VBAD && !cn_devopen(cnd, td, 1)))
104 static dev_t cn_udev_t;
105 SYSCTL_OPAQUE(_machdep, OID_AUTO, consdev, CTLFLAG_RD,
106 &cn_udev_t, sizeof cn_udev_t, "T,struct cdev *", "");
108 int cons_avail_mask = 0; /* Bit mask. Each registered low level console
109 * which is currently unavailable for inpit
110 * (i.e., if it is in graphics mode) will have
114 static int openflag; /* how /dev/console was opened */
115 static int cn_is_open;
116 static char *consbuf; /* buffer used by `consmsgbuf' */
117 static struct callout conscallout; /* callout for outputting to constty */
118 struct msgbuf consmsgbuf; /* message buffer for console tty */
119 static u_char console_pausing; /* pause after each line during probe */
120 static char *console_pausestr=
121 "<pause; press any key to proceed to next line or '.' to end pause mode>";
122 struct tty *constty; /* pointer to console "window" tty */
123 static struct mtx cnputs_mtx; /* Mutex for cnputs(). */
124 static int use_cnputs_mtx = 0; /* != 0 if cnputs_mtx locking reqd. */
126 static void constty_timeout(void *arg);
128 static struct consdev cons_consdev;
129 DATA_SET(cons_set, cons_consdev);
130 SET_DECLARE(cons_set, struct consdev);
135 struct consdev *best_cn, *cn, **list;
138 * Check if we should mute the console (for security reasons perhaps)
139 * It can be changes dynamically using sysctl kern.consmute
140 * once we are up and going.
143 cn_mute = ((boothowto & (RB_MUTE
146 |RB_ASKNAME)) == RB_MUTE);
149 * Find the first console with the highest priority.
152 SET_FOREACH(list, cons_set) {
155 if (cn->cn_probe == NULL)
158 if (cn->cn_pri == CN_DEAD)
160 if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
162 if (boothowto & RB_MULTIPLE) {
164 * Initialize console, and attach to it.
172 if ((boothowto & RB_MULTIPLE) == 0) {
173 best_cn->cn_init(best_cn);
176 if (boothowto & RB_PAUSE)
179 * Make the best console the preferred console.
190 /* add a new physical console to back the virtual console */
192 cnadd(struct consdev *cn)
194 struct cn_device *cnd;
197 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
198 if (cnd->cnd_cn == cn)
200 for (i = 0; i < CNDEVTAB_SIZE; i++) {
202 if (cnd->cnd_cn == NULL)
205 if (cnd->cnd_cn != NULL)
208 if (cn->cn_name[0] == '\0') {
209 /* XXX: it is unclear if/where this print might output */
210 printf("WARNING: console at %p has no name\n", cn);
212 STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
214 /* Add device to the active mask. */
215 cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
221 cnremove(struct consdev *cn)
223 struct cn_device *cnd;
226 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
227 if (cnd->cnd_cn != cn)
229 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
230 if (cnd->cnd_vp != NULL)
231 vn_close(cnd->cnd_vp, openflag, NOCRED, NULL);
235 /* Remove this device from available mask. */
236 for (i = 0; i < CNDEVTAB_SIZE; i++)
237 if (cnd == &cn_devtab[i]) {
238 cons_avail_mask &= ~(1 << i);
244 * syscons gets really confused if console resources are
245 * freed after the system has initialized.
247 if (cn->cn_term != NULL)
255 cnselect(struct consdev *cn)
257 struct cn_device *cnd;
259 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
260 if (cnd->cnd_cn != cn)
262 if (cnd == STAILQ_FIRST(&cn_devlist))
264 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
265 STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
271 cnavailable(struct consdev *cn, int available)
275 for (i = 0; i < CNDEVTAB_SIZE; i++) {
276 if (cn_devtab[i].cnd_cn == cn)
280 if (i < CNDEVTAB_SIZE)
281 cons_avail_mask |= (1 << i);
282 cn->cn_flags &= ~CN_FLAG_NOAVAIL;
284 if (i < CNDEVTAB_SIZE)
285 cons_avail_mask &= ~(1 << i);
286 cn->cn_flags |= CN_FLAG_NOAVAIL;
294 return (cons_avail_mask == 0);
298 * XXX: rewrite to use sbufs instead
302 sysctl_kern_console(SYSCTL_HANDLER_ARGS)
304 struct cn_device *cnd;
305 struct consdev *cp, **list;
307 int delete, len, error;
310 SET_FOREACH(list, cons_set) {
312 if (cp->cn_name[0] != '\0')
313 len += strlen(cp->cn_name) + 1;
315 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
316 len += strlen(cnd->cnd_cn->cn_name) + 1;
317 len = len > CNDEVPATHMAX ? len : CNDEVPATHMAX;
318 MALLOC(name, char *, len, M_TEMP, M_WAITOK | M_ZERO);
320 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
321 p += sprintf(p, "%s,", cnd->cnd_cn->cn_name);
323 SET_FOREACH(list, cons_set) {
325 if (cp->cn_name[0] != '\0')
326 p += sprintf(p, "%s,", cp->cn_name);
328 error = sysctl_handle_string(oidp, name, len, req);
329 if (error == 0 && req->newptr != NULL) {
337 SET_FOREACH(list, cons_set) {
339 if (strcmp(p, cp->cn_name) != 0)
356 SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW,
357 0, 0, sysctl_kern_console, "A", "Console device control");
360 * User has changed the state of the console muting.
361 * This may require us to open or close the device in question.
364 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
370 error = sysctl_handle_int(oidp, &cn_mute, 0, req);
371 if (error != 0 || req->newptr == NULL)
373 if (ocn_mute && !cn_mute && cn_is_open)
374 error = cnopen(NULL, openflag, 0, curthread);
375 else if (!ocn_mute && cn_mute && cn_is_open) {
376 error = cnclose(NULL, openflag, 0, curthread);
377 cn_is_open = 1; /* XXX hack */
382 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
383 0, sizeof(cn_mute), sysctl_kern_consmute, "I", "");
386 cn_devopen(struct cn_device *cnd, struct thread *td, int forceopen)
388 char path[CNDEVPATHMAX];
395 if ((vp = cnd->cnd_vp) != NULL) {
396 if (!forceopen && vp->v_type != VBAD) {
398 csw = dev_refthread(dev);
401 error = (*csw->d_open)(dev, openflag, 0, td);
406 vn_close(vp, openflag, td->td_ucred, td);
408 snprintf(path, sizeof(path), "/dev/%s", cnd->cnd_cn->cn_name);
409 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, td);
410 error = vn_open(&nd, &openflag, 0, -1);
412 NDFREE(&nd, NDF_ONLY_PNBUF);
413 VOP_UNLOCK(nd.ni_vp, 0, td);
414 if (nd.ni_vp->v_type == VCHR)
415 cnd->cnd_vp = nd.ni_vp;
417 vn_close(nd.ni_vp, openflag, td->td_ucred, td);
419 return (cnd->cnd_vp != NULL);
423 cnopen(struct cdev *dev, int flag, int mode, struct thread *td)
425 struct cn_device *cnd;
427 openflag = flag | FWRITE; /* XXX */
428 cn_is_open = 1; /* console is logically open */
431 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
432 cn_devopen(cnd, td, 0);
437 cnclose(struct cdev *dev, int flag, int mode, struct thread *td)
439 struct cn_device *cnd;
442 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
443 if ((vp = cnd->cnd_vp) == NULL)
446 vn_close(vp, openflag, td->td_ucred, td);
453 cnread(struct cdev *dev, struct uio *uio, int flag)
455 struct cn_device *cnd;
459 cnd = STAILQ_FIRST(&cn_devlist);
460 if (cn_mute || CND_INVALID(cnd, curthread))
462 dev = cnd->cnd_vp->v_rdev;
463 csw = dev_refthread(dev);
466 error = (csw->d_read)(dev, uio, flag);
472 cnwrite(struct cdev *dev, struct uio *uio, int flag)
474 struct cn_device *cnd;
478 cnd = STAILQ_FIRST(&cn_devlist);
479 if (cn_mute || CND_INVALID(cnd, curthread))
482 dev = constty->t_dev;
484 dev = cnd->cnd_vp->v_rdev;
487 csw = dev_refthread(dev);
490 error = (csw->d_write)(dev, uio, flag);
495 uio->uio_resid = 0; /* dump the data */
500 cnioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
502 struct cn_device *cnd;
506 cnd = STAILQ_FIRST(&cn_devlist);
507 if (cn_mute || CND_INVALID(cnd, td))
510 * Superuser can always use this to wrest control of console
511 * output from the "virtual" console.
513 if (cmd == TIOCCONS && constty) {
514 error = priv_check(td, PRIV_TTY_CONSOLE);
520 dev = cnd->cnd_vp->v_rdev;
522 return (0); /* XXX : ENOTTY ? */
523 csw = dev_refthread(dev);
526 error = (csw->d_ioctl)(dev, cmd, data, flag, td);
533 * poll/kqfilter do not appear to be correct
536 cnpoll(struct cdev *dev, int events, struct thread *td)
538 struct cn_device *cnd;
542 cnd = STAILQ_FIRST(&cn_devlist);
543 if (cn_mute || CND_INVALID(cnd, td))
545 dev = cnd->cnd_vp->v_rdev;
548 csw = dev_refthread(dev);
551 error = (csw->d_poll)(dev, events, td);
557 cnkqfilter(struct cdev *dev, struct knote *kn)
559 struct cn_device *cnd;
563 cnd = STAILQ_FIRST(&cn_devlist);
564 if (cn_mute || CND_INVALID(cnd, curthread))
566 dev = cnd->cnd_vp->v_rdev;
569 csw = dev_refthread(dev);
572 error = (csw->d_kqfilter)(dev, kn);
578 * Low level console routines.
587 while ((c = cncheckc()) == -1)
590 c = '\n'; /* console input is always ICRNL */
597 struct cn_device *cnd;
603 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
605 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
606 if (cn->cn_checkc != NULL)
607 c = cn->cn_checkc(cn);
621 struct cn_device *cnd;
625 if (cn_mute || c == '\0')
627 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
629 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
631 cn->cn_putc(cn, '\r');
635 if (console_pausing && c == '\n' && !kdb_active) {
636 for (cp = console_pausestr; *cp != '\0'; cp++)
641 for (cp = console_pausestr; *cp != '\0'; cp++)
653 if (use_cnputs_mtx) {
654 mtx_lock_spin(&cnputs_mtx);
658 while ((c = *p++) != '\0')
662 mtx_unlock_spin(&cnputs_mtx);
665 static int consmsgbuf_size = 8192;
666 SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
670 * Redirect console output to a tty.
673 constty_set(struct tty *tp)
677 KASSERT(tp != NULL, ("constty_set: NULL tp"));
678 if (consbuf == NULL) {
679 size = consmsgbuf_size;
680 consbuf = malloc(size, M_TTYS, M_WAITOK);
681 msgbuf_init(&consmsgbuf, consbuf, size);
682 callout_init(&conscallout, 0);
685 constty_timeout(NULL);
689 * Disable console redirection to a tty.
699 callout_stop(&conscallout);
700 while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
702 free(consbuf, M_TTYS);
706 /* Times per second to check for pending console tty messages. */
707 static int constty_wakeups_per_second = 5;
708 SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
709 &constty_wakeups_per_second, 0, "");
712 constty_timeout(void *arg)
716 while (constty != NULL && (c = msgbuf_getchar(&consmsgbuf)) != -1) {
717 if (tputchar(c, constty) < 0)
720 if (constty != NULL) {
721 callout_reset(&conscallout, hz / constty_wakeups_per_second,
722 constty_timeout, NULL);
724 /* Deallocate the constty buffer memory. */
730 cn_drvinit(void *unused)
733 make_dev(&cn_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "console");
735 mtx_init(&cnputs_mtx, "cnputs_mtx", NULL, MTX_SPIN | MTX_NOWITNESS);
739 SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL)