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>
67 #include <machine/clock.h>
69 static d_open_t cnopen;
70 static d_close_t cnclose;
71 static d_read_t cnread;
72 static d_write_t cnwrite;
73 static d_ioctl_t cnioctl;
74 static d_poll_t cnpoll;
75 static d_kqfilter_t cnkqfilter;
77 static struct cdevsw cn_cdevsw = {
78 .d_version = D_VERSION,
86 .d_flags = D_TTY | D_NEEDGIANT,
87 .d_kqfilter = cnkqfilter,
91 STAILQ_ENTRY(cn_device) cnd_next;
93 struct consdev *cnd_cn;
96 #define CNDEVPATHMAX 32
97 #define CNDEVTAB_SIZE 4
98 static struct cn_device cn_devtab[CNDEVTAB_SIZE];
99 static STAILQ_HEAD(, cn_device) cn_devlist =
100 STAILQ_HEAD_INITIALIZER(cn_devlist);
102 #define CND_INVALID(cnd, td) \
103 (cnd == NULL || cnd->cnd_vp == NULL || \
104 (cnd->cnd_vp->v_type == VBAD && !cn_devopen(cnd, td, 1)))
106 static dev_t cn_udev_t;
107 SYSCTL_OPAQUE(_machdep, OID_AUTO, consdev, CTLFLAG_RD,
108 &cn_udev_t, sizeof cn_udev_t, "T,struct cdev *", "");
110 int cons_avail_mask = 0; /* Bit mask. Each registered low level console
111 * which is currently unavailable for inpit
112 * (i.e., if it is in graphics mode) will have
116 static int openflag; /* how /dev/console was opened */
117 static int cn_is_open;
118 static char *consbuf; /* buffer used by `consmsgbuf' */
119 static struct callout conscallout; /* callout for outputting to constty */
120 struct msgbuf consmsgbuf; /* message buffer for console tty */
121 static u_char console_pausing; /* pause after each line during probe */
122 static char *console_pausestr=
123 "<pause; press any key to proceed to next line or '.' to end pause mode>";
124 struct tty *constty; /* pointer to console "window" tty */
125 static struct mtx cnputs_mtx; /* Mutex for cnputs(). */
126 static int use_cnputs_mtx = 0; /* != 0 if cnputs_mtx locking reqd. */
128 static void constty_timeout(void *arg);
130 static struct consdev cons_consdev;
131 DATA_SET(cons_set, cons_consdev);
132 SET_DECLARE(cons_set, struct consdev);
137 struct consdev *best_cn, *cn, **list;
140 * Check if we should mute the console (for security reasons perhaps)
141 * It can be changes dynamically using sysctl kern.consmute
142 * once we are up and going.
145 cn_mute = ((boothowto & (RB_MUTE
148 |RB_ASKNAME)) == RB_MUTE);
151 * Find the first console with the highest priority.
154 SET_FOREACH(list, cons_set) {
157 if (cn->cn_probe == NULL)
160 if (cn->cn_pri == CN_DEAD)
162 if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
164 if (boothowto & RB_MULTIPLE) {
166 * Initialize console, and attach to it.
174 if ((boothowto & RB_MULTIPLE) == 0) {
175 best_cn->cn_init(best_cn);
178 if (boothowto & RB_PAUSE)
181 * Make the best console the preferred console.
192 /* add a new physical console to back the virtual console */
194 cnadd(struct consdev *cn)
196 struct cn_device *cnd;
199 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
200 if (cnd->cnd_cn == cn)
202 for (i = 0; i < CNDEVTAB_SIZE; i++) {
204 if (cnd->cnd_cn == NULL)
207 if (cnd->cnd_cn != NULL)
210 if (cn->cn_name[0] == '\0') {
211 /* XXX: it is unclear if/where this print might output */
212 printf("WARNING: console at %p has no name\n", cn);
214 STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
216 /* Add device to the active mask. */
217 cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
223 cnremove(struct consdev *cn)
225 struct cn_device *cnd;
228 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
229 if (cnd->cnd_cn != cn)
231 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
232 if (cnd->cnd_vp != NULL)
233 vn_close(cnd->cnd_vp, openflag, NOCRED, NULL);
237 /* Remove this device from available mask. */
238 for (i = 0; i < CNDEVTAB_SIZE; i++)
239 if (cnd == &cn_devtab[i]) {
240 cons_avail_mask &= ~(1 << i);
246 * syscons gets really confused if console resources are
247 * freed after the system has initialized.
249 if (cn->cn_term != NULL)
257 cnselect(struct consdev *cn)
259 struct cn_device *cnd;
261 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
262 if (cnd->cnd_cn != cn)
264 if (cnd == STAILQ_FIRST(&cn_devlist))
266 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
267 STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
273 cnavailable(struct consdev *cn, int available)
277 for (i = 0; i < CNDEVTAB_SIZE; i++) {
278 if (cn_devtab[i].cnd_cn == cn)
282 if (i < CNDEVTAB_SIZE)
283 cons_avail_mask |= (1 << i);
284 cn->cn_flags &= ~CN_FLAG_NOAVAIL;
286 if (i < CNDEVTAB_SIZE)
287 cons_avail_mask &= ~(1 << i);
288 cn->cn_flags |= CN_FLAG_NOAVAIL;
296 return (cons_avail_mask == 0);
300 * sysctl_kern_console() provides output parseable in conscontrol(1).
303 sysctl_kern_console(SYSCTL_HANDLER_ARGS)
305 struct cn_device *cnd;
306 struct consdev *cp, **list;
311 sb = sbuf_new(NULL, NULL, CNDEVPATHMAX * 2, SBUF_AUTOEXTEND);
315 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
316 sbuf_printf(sb, "%s,", cnd->cnd_cn->cn_name);
317 sbuf_printf(sb, "/");
318 SET_FOREACH(list, cons_set) {
320 if (cp->cn_name[0] != '\0')
321 sbuf_printf(sb, "%s,", cp->cn_name);
324 error = sysctl_handle_string(oidp, sbuf_data(sb), sbuf_len(sb), req);
325 if (error == 0 && req->newptr != NULL) {
333 SET_FOREACH(list, cons_set) {
335 if (strcmp(p, cp->cn_name) != 0)
352 SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW,
353 0, 0, sysctl_kern_console, "A", "Console device control");
356 * User has changed the state of the console muting.
357 * This may require us to open or close the device in question.
360 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
366 error = sysctl_handle_int(oidp, &cn_mute, 0, req);
367 if (error != 0 || req->newptr == NULL)
369 if (ocn_mute && !cn_mute && cn_is_open)
370 error = cnopen(NULL, openflag, 0, curthread);
371 else if (!ocn_mute && cn_mute && cn_is_open) {
372 error = cnclose(NULL, openflag, 0, curthread);
373 cn_is_open = 1; /* XXX hack */
378 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
379 0, sizeof(cn_mute), sysctl_kern_consmute, "I", "");
382 cn_devopen(struct cn_device *cnd, struct thread *td, int forceopen)
384 char path[CNDEVPATHMAX];
391 if ((vp = cnd->cnd_vp) != NULL) {
392 if (!forceopen && vp->v_type != VBAD) {
394 csw = dev_refthread(dev);
397 error = (*csw->d_open)(dev, openflag, 0, td);
402 vn_close(vp, openflag, td->td_ucred, td);
404 snprintf(path, sizeof(path), "/dev/%s", cnd->cnd_cn->cn_name);
405 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, td);
406 error = vn_open(&nd, &openflag, 0, NULL);
408 NDFREE(&nd, NDF_ONLY_PNBUF);
409 VOP_UNLOCK(nd.ni_vp, 0);
410 if (nd.ni_vp->v_type == VCHR)
411 cnd->cnd_vp = nd.ni_vp;
413 vn_close(nd.ni_vp, openflag, td->td_ucred, td);
415 return (cnd->cnd_vp != NULL);
419 cnopen(struct cdev *dev, int flag, int mode, struct thread *td)
421 struct cn_device *cnd;
423 openflag = flag | FWRITE; /* XXX */
424 cn_is_open = 1; /* console is logically open */
427 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
428 cn_devopen(cnd, td, 0);
433 cnclose(struct cdev *dev, int flag, int mode, struct thread *td)
435 struct cn_device *cnd;
438 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
439 if ((vp = cnd->cnd_vp) == NULL)
442 vn_close(vp, openflag, td->td_ucred, td);
449 cnread(struct cdev *dev, struct uio *uio, int flag)
451 struct cn_device *cnd;
455 cnd = STAILQ_FIRST(&cn_devlist);
456 if (cn_mute || CND_INVALID(cnd, curthread))
458 dev = cnd->cnd_vp->v_rdev;
459 csw = dev_refthread(dev);
462 error = (csw->d_read)(dev, uio, flag);
468 cnwrite(struct cdev *dev, struct uio *uio, int flag)
470 struct cn_device *cnd;
474 cnd = STAILQ_FIRST(&cn_devlist);
475 if (cn_mute || CND_INVALID(cnd, curthread))
478 dev = constty->t_dev;
480 dev = cnd->cnd_vp->v_rdev;
483 csw = dev_refthread(dev);
486 error = (csw->d_write)(dev, uio, flag);
491 uio->uio_resid = 0; /* dump the data */
496 cnioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
498 struct cn_device *cnd;
502 cnd = STAILQ_FIRST(&cn_devlist);
503 if (cn_mute || CND_INVALID(cnd, td))
506 * Superuser can always use this to wrest control of console
507 * output from the "virtual" console.
509 if (cmd == TIOCCONS && constty) {
510 error = priv_check(td, PRIV_TTY_CONSOLE);
516 dev = cnd->cnd_vp->v_rdev;
518 return (0); /* XXX : ENOTTY ? */
519 csw = dev_refthread(dev);
522 error = (csw->d_ioctl)(dev, cmd, data, flag, td);
529 * poll/kqfilter do not appear to be correct
532 cnpoll(struct cdev *dev, int events, struct thread *td)
534 struct cn_device *cnd;
538 cnd = STAILQ_FIRST(&cn_devlist);
539 if (cn_mute || CND_INVALID(cnd, td))
541 dev = cnd->cnd_vp->v_rdev;
544 csw = dev_refthread(dev);
547 error = (csw->d_poll)(dev, events, td);
553 cnkqfilter(struct cdev *dev, struct knote *kn)
555 struct cn_device *cnd;
559 cnd = STAILQ_FIRST(&cn_devlist);
560 if (cn_mute || CND_INVALID(cnd, curthread))
562 dev = cnd->cnd_vp->v_rdev;
565 csw = dev_refthread(dev);
568 error = (csw->d_kqfilter)(dev, kn);
574 * Low level console routines.
583 while ((c = cncheckc()) == -1)
586 c = '\n'; /* console input is always ICRNL */
593 struct cn_device *cnd;
599 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
601 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
602 if (cn->cn_checkc != NULL)
603 c = cn->cn_checkc(cn);
617 struct cn_device *cnd;
621 if (cn_mute || c == '\0')
623 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
625 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
627 cn->cn_putc(cn, '\r');
631 if (console_pausing && c == '\n' && !kdb_active) {
632 for (cp = console_pausestr; *cp != '\0'; cp++)
637 for (cp = console_pausestr; *cp != '\0'; cp++)
649 if (use_cnputs_mtx) {
650 mtx_lock_spin(&cnputs_mtx);
654 while ((c = *p++) != '\0')
658 mtx_unlock_spin(&cnputs_mtx);
661 static int consmsgbuf_size = 8192;
662 SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
666 * Redirect console output to a tty.
669 constty_set(struct tty *tp)
673 KASSERT(tp != NULL, ("constty_set: NULL tp"));
674 if (consbuf == NULL) {
675 size = consmsgbuf_size;
676 consbuf = malloc(size, M_TTYS, M_WAITOK);
677 msgbuf_init(&consmsgbuf, consbuf, size);
678 callout_init(&conscallout, 0);
681 constty_timeout(NULL);
685 * Disable console redirection to a tty.
695 callout_stop(&conscallout);
696 while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
698 free(consbuf, M_TTYS);
702 /* Times per second to check for pending console tty messages. */
703 static int constty_wakeups_per_second = 5;
704 SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
705 &constty_wakeups_per_second, 0, "");
708 constty_timeout(void *arg)
712 while (constty != NULL && (c = msgbuf_getchar(&consmsgbuf)) != -1) {
713 if (tputchar(c, constty) < 0)
716 if (constty != NULL) {
717 callout_reset(&conscallout, hz / constty_wakeups_per_second,
718 constty_timeout, NULL);
720 /* Deallocate the constty buffer memory. */
726 cn_drvinit(void *unused)
729 make_dev(&cn_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "console");
731 mtx_init(&cnputs_mtx, "cnputs_mtx", NULL, MTX_SPIN | MTX_NOWITNESS);
735 SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL);
738 * Sysbeep(), if we have hardware for it
741 #ifdef HAS_TIMER_SPKR
746 sysbeepstop(void *chan)
749 timer_spkr_release();
754 sysbeep(int pitch, int period)
757 if (timer_spkr_acquire()) {
759 /* Something else owns it. */
763 timer_spkr_setfreq(pitch);
766 timeout(sysbeepstop, (void *)NULL, period);
774 * No hardware, no sound
778 sysbeep(int pitch __unused, int period __unused)