kern_cons.c revision 125467
1/*
2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
8 * Science Department.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	from: @(#)cons.c	7.2 (Berkeley) 5/9/91
39 */
40
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: head/sys/kern/tty_cons.c 125467 2004-02-05 01:56:43Z kan $");
43
44#include "opt_ddb.h"
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/conf.h>
49#include <sys/cons.h>
50#include <sys/fcntl.h>
51#include <sys/kernel.h>
52#include <sys/malloc.h>
53#include <sys/msgbuf.h>
54#include <sys/namei.h>
55#include <sys/proc.h>
56#include <sys/queue.h>
57#include <sys/reboot.h>
58#include <sys/sysctl.h>
59#include <sys/tty.h>
60#include <sys/uio.h>
61#include <sys/vnode.h>
62
63#include <ddb/ddb.h>
64
65#include <machine/cpu.h>
66
67static	d_open_t	cnopen;
68static	d_close_t	cnclose;
69static	d_read_t	cnread;
70static	d_write_t	cnwrite;
71static	d_ioctl_t	cnioctl;
72static	d_poll_t	cnpoll;
73static	d_kqfilter_t	cnkqfilter;
74
75static struct cdevsw cn_cdevsw = {
76	.d_open =	cnopen,
77	.d_close =	cnclose,
78	.d_read =	cnread,
79	.d_write =	cnwrite,
80	.d_ioctl =	cnioctl,
81	.d_poll =	cnpoll,
82	.d_name =	"console",
83	.d_maj =	256,
84			/*
85			 * XXX: We really want major #0, but zero here means
86			 * XXX: allocate a major number automatically.
87			 * XXX: kern_conf.c knows what to do when it sees 256.
88			 */
89	.d_flags =	D_TTY,
90	.d_kqfilter =	cnkqfilter,
91};
92
93struct cn_device {
94	STAILQ_ENTRY(cn_device) cnd_next;
95	struct		vnode *cnd_vp;
96	struct		consdev *cnd_cn;
97};
98
99#define CNDEVPATHMAX	32
100#define CNDEVTAB_SIZE	4
101static struct cn_device cn_devtab[CNDEVTAB_SIZE];
102static STAILQ_HEAD(, cn_device) cn_devlist =
103    STAILQ_HEAD_INITIALIZER(cn_devlist);
104
105#define CND_INVALID(cnd, td) 						\
106	(cnd == NULL || cnd->cnd_vp == NULL ||				\
107	    (cnd->cnd_vp->v_type == VBAD && !cn_devopen(cnd, td, 1)))
108
109static udev_t	cn_udev_t;
110SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD,
111	&cn_udev_t, sizeof cn_udev_t, "T,dev_t", "");
112
113int	cons_avail_mask = 0;	/* Bit mask. Each registered low level console
114				 * which is currently unavailable for inpit
115				 * (i.e., if it is in graphics mode) will have
116				 * this bit cleared.
117				 */
118static int cn_mute;
119static int openflag;			/* how /dev/console was opened */
120static int cn_is_open;
121static char *consbuf;			/* buffer used by `consmsgbuf' */
122static struct callout conscallout;	/* callout for outputting to constty */
123struct msgbuf consmsgbuf;		/* message buffer for console tty */
124static u_char console_pausing;		/* pause after each line during probe */
125static char *console_pausestr=
126"<pause; press any key to proceed to next line or '.' to end pause mode>";
127struct tty *constty;			/* pointer to console "window" tty */
128
129void	cndebug(char *);
130static void constty_timeout(void *arg);
131
132CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
133SET_DECLARE(cons_set, struct consdev);
134
135void
136cninit(void)
137{
138	struct consdev *best_cn, *cn, **list;
139
140	/*
141	 * Check if we should mute the console (for security reasons perhaps)
142	 * It can be changes dynamically using sysctl kern.consmute
143	 * once we are up and going.
144	 *
145	 */
146        cn_mute = ((boothowto & (RB_MUTE
147			|RB_SINGLE
148			|RB_VERBOSE
149			|RB_ASKNAME
150			|RB_CONFIG)) == RB_MUTE);
151
152	/*
153	 * Find the first console with the highest priority.
154	 */
155	best_cn = NULL;
156	SET_FOREACH(list, cons_set) {
157		cn = *list;
158		cnremove(cn);
159		if (cn->cn_probe == NULL)
160			continue;
161		cn->cn_probe(cn);
162		if (cn->cn_pri == CN_DEAD)
163			continue;
164		if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
165			best_cn = cn;
166		if (boothowto & RB_MULTIPLE) {
167			/*
168			 * Initialize console, and attach to it.
169			 */
170			cnadd(cn);
171			cn->cn_init(cn);
172		}
173	}
174	if (best_cn == NULL)
175		return;
176	if ((boothowto & RB_MULTIPLE) == 0) {
177		cnadd(best_cn);
178		best_cn->cn_init(best_cn);
179	}
180	if (boothowto & RB_PAUSE)
181		console_pausing = 1;
182	/*
183	 * Make the best console the preferred console.
184	 */
185	cnselect(best_cn);
186}
187
188void
189cninit_finish()
190{
191	console_pausing = 0;
192}
193
194/* add a new physical console to back the virtual console */
195int
196cnadd(struct consdev *cn)
197{
198	struct cn_device *cnd;
199	int i;
200
201	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
202		if (cnd->cnd_cn == cn)
203			return (0);
204	for (i = 0; i < CNDEVTAB_SIZE; i++) {
205		cnd = &cn_devtab[i];
206		if (cnd->cnd_cn == NULL)
207			break;
208	}
209	if (cnd->cnd_cn != NULL)
210		return (ENOMEM);
211	cnd->cnd_cn = cn;
212	if (cn->cn_name[0] == '\0') {
213		/* XXX: it is unclear if/where this print might output */
214		printf("WARNING: console at %p has no name\n", cn);
215	}
216	STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
217
218	/* Add device to the active mask. */
219	cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
220
221	return (0);
222}
223
224void
225cnremove(struct consdev *cn)
226{
227	struct cn_device *cnd;
228	int i;
229
230	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
231		if (cnd->cnd_cn != cn)
232			continue;
233		STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
234		if (cnd->cnd_vp != NULL)
235			vn_close(cnd->cnd_vp, openflag, NOCRED, NULL);
236		cnd->cnd_vp = NULL;
237		cnd->cnd_cn = NULL;
238
239		/* Remove this device from available mask. */
240		for (i = 0; i < CNDEVTAB_SIZE; i++)
241			if (cnd == &cn_devtab[i]) {
242				cons_avail_mask &= ~(1 << i);
243				break;
244			}
245#if 0
246		/*
247		 * XXX
248		 * syscons gets really confused if console resources are
249		 * freed after the system has initialized.
250		 */
251		if (cn->cn_term != NULL)
252			cn->cn_term(cn);
253#endif
254		return;
255	}
256}
257
258void
259cnselect(struct consdev *cn)
260{
261	struct cn_device *cnd;
262
263	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
264		if (cnd->cnd_cn != cn)
265			continue;
266		if (cnd == STAILQ_FIRST(&cn_devlist))
267			return;
268		STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
269		STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
270		return;
271	}
272}
273
274void
275cnavailable(struct consdev *cn, int available)
276{
277	int i;
278
279	for (i = 0; i < CNDEVTAB_SIZE; i++) {
280		if (cn_devtab[i].cnd_cn == cn)
281			break;
282	}
283	if (available) {
284		if (i < CNDEVTAB_SIZE)
285			cons_avail_mask |= (1 << i);
286		cn->cn_flags &= ~CN_FLAG_NOAVAIL;
287	} else {
288		if (i < CNDEVTAB_SIZE)
289			cons_avail_mask &= ~(1 << i);
290		cn->cn_flags |= CN_FLAG_NOAVAIL;
291	}
292}
293
294int
295cn_unavailable(void)
296{
297	return (cons_avail_mask == 0);
298}
299
300void
301cndebug(char *str)
302{
303	int i, len;
304
305	len = strlen(str);
306	cnputc('>'); cnputc('>'); cnputc('>'); cnputc(' ');
307	for (i = 0; i < len; i++)
308		cnputc(str[i]);
309	cnputc('\n');
310}
311
312/*
313 * XXX: rewrite to use sbufs instead
314 */
315
316static int
317sysctl_kern_console(SYSCTL_HANDLER_ARGS)
318{
319	struct cn_device *cnd;
320	struct consdev *cp, **list;
321	char *name, *p;
322	int delete, len, error;
323
324	len = 2;
325	SET_FOREACH(list, cons_set) {
326		cp = *list;
327		if (cp->cn_name[0] != '\0')
328			len += strlen(cp->cn_name) + 1;
329	}
330	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
331		len += strlen(cnd->cnd_cn->cn_name) + 1;
332	len = len > CNDEVPATHMAX ? len : CNDEVPATHMAX;
333	MALLOC(name, char *, len, M_TEMP, M_WAITOK | M_ZERO);
334	p = name;
335	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
336		p += sprintf(p, "%s,", cnd->cnd_cn->cn_name);
337	*p++ = '/';
338	SET_FOREACH(list, cons_set) {
339		cp = *list;
340		if (cp->cn_name[0] != '\0')
341			p += sprintf(p, "%s,", cp->cn_name);
342	}
343	error = sysctl_handle_string(oidp, name, len, req);
344	if (error == 0 && req->newptr != NULL) {
345		p = name;
346		error = ENXIO;
347		delete = 0;
348		if (*p == '-') {
349			delete = 1;
350			p++;
351		}
352		SET_FOREACH(list, cons_set) {
353			cp = *list;
354			if (strcmp(p, cp->cn_name) != 0)
355				continue;
356			if (delete) {
357				cnremove(cp);
358				error = 0;
359			} else {
360				error = cnadd(cp);
361				if (error == 0)
362					cnselect(cp);
363			}
364			break;
365		}
366	}
367	FREE(name, M_TEMP);
368	return (error);
369}
370
371SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW,
372	0, 0, sysctl_kern_console, "A", "Console device control");
373
374/*
375 * User has changed the state of the console muting.
376 * This may require us to open or close the device in question.
377 */
378static int
379sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
380{
381	int error;
382	int ocn_mute;
383
384	ocn_mute = cn_mute;
385	error = sysctl_handle_int(oidp, &cn_mute, 0, req);
386	if (error != 0 || req->newptr == NULL)
387		return (error);
388	if (ocn_mute && !cn_mute && cn_is_open)
389		error = cnopen(NODEV, openflag, 0, curthread);
390	else if (!ocn_mute && cn_mute && cn_is_open) {
391		error = cnclose(NODEV, openflag, 0, curthread);
392		cn_is_open = 1;		/* XXX hack */
393	}
394	return (error);
395}
396
397SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
398	0, sizeof(cn_mute), sysctl_kern_consmute, "I", "");
399
400static int
401cn_devopen(struct cn_device *cnd, struct thread *td, int forceopen)
402{
403	char path[CNDEVPATHMAX];
404	struct nameidata nd;
405	struct vnode *vp;
406	dev_t dev;
407	int error;
408
409	if ((vp = cnd->cnd_vp) != NULL) {
410		if (!forceopen && vp->v_type != VBAD) {
411			dev = vp->v_rdev;
412			return ((*devsw(dev)->d_open)(dev, openflag, 0, td));
413		}
414		cnd->cnd_vp = NULL;
415		vn_close(vp, openflag, td->td_ucred, td);
416	}
417	snprintf(path, sizeof(path), "/dev/%s", cnd->cnd_cn->cn_name);
418	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, td);
419	error = vn_open(&nd, &openflag, 0, -1);
420	if (error == 0) {
421		NDFREE(&nd, NDF_ONLY_PNBUF);
422		VOP_UNLOCK(nd.ni_vp, 0, td);
423		if (nd.ni_vp->v_type == VCHR)
424			cnd->cnd_vp = nd.ni_vp;
425		else
426			vn_close(nd.ni_vp, openflag, td->td_ucred, td);
427	}
428	return (cnd->cnd_vp != NULL);
429}
430
431static int
432cnopen(dev_t dev, int flag, int mode, struct thread *td)
433{
434	struct cn_device *cnd;
435
436	openflag = flag | FWRITE;	/* XXX */
437	cn_is_open = 1;			/* console is logically open */
438	if (cn_mute)
439		return (0);
440	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
441		cn_devopen(cnd, td, 0);
442	return (0);
443}
444
445static int
446cnclose(dev_t dev, int flag, int mode, struct thread *td)
447{
448	struct cn_device *cnd;
449	struct vnode *vp;
450
451	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
452		if ((vp = cnd->cnd_vp) == NULL)
453			continue;
454		cnd->cnd_vp = NULL;
455		vn_close(vp, openflag, td->td_ucred, td);
456	}
457	cn_is_open = 0;
458	return (0);
459}
460
461static int
462cnread(dev_t dev, struct uio *uio, int flag)
463{
464	struct cn_device *cnd;
465
466	cnd = STAILQ_FIRST(&cn_devlist);
467	if (cn_mute || CND_INVALID(cnd, curthread))
468		return (0);
469	dev = cnd->cnd_vp->v_rdev;
470	return ((*devsw(dev)->d_read)(dev, uio, flag));
471}
472
473static int
474cnwrite(dev_t dev, struct uio *uio, int flag)
475{
476	struct cn_device *cnd;
477
478	cnd = STAILQ_FIRST(&cn_devlist);
479	if (cn_mute || CND_INVALID(cnd, curthread))
480		goto done;
481	if (constty)
482		dev = constty->t_dev;
483	else
484		dev = cnd->cnd_vp->v_rdev;
485	if (dev != NULL) {
486		log_console(uio);
487		return ((*devsw(dev)->d_write)(dev, uio, flag));
488	}
489done:
490	uio->uio_resid = 0; /* dump the data */
491	return (0);
492}
493
494static int
495cnioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
496{
497	struct cn_device *cnd;
498	int error;
499
500	cnd = STAILQ_FIRST(&cn_devlist);
501	if (cn_mute || CND_INVALID(cnd, td))
502		return (0);
503	/*
504	 * Superuser can always use this to wrest control of console
505	 * output from the "virtual" console.
506	 */
507	if (cmd == TIOCCONS && constty) {
508		error = suser(td);
509		if (error)
510			return (error);
511		constty = NULL;
512		return (0);
513	}
514	dev = cnd->cnd_vp->v_rdev;
515	if (dev != NULL)
516		return ((*devsw(dev)->d_ioctl)(dev, cmd, data, flag, td));
517	return (0);
518}
519
520/*
521 * XXX
522 * poll/kqfilter do not appear to be correct
523 */
524static int
525cnpoll(dev_t dev, int events, struct thread *td)
526{
527	struct cn_device *cnd;
528
529	cnd = STAILQ_FIRST(&cn_devlist);
530	if (cn_mute || CND_INVALID(cnd, td))
531		return (0);
532	dev = cnd->cnd_vp->v_rdev;
533	if (dev != NULL)
534		return ((*devsw(dev)->d_poll)(dev, events, td));
535	return (0);
536}
537
538static int
539cnkqfilter(dev_t dev, struct knote *kn)
540{
541	struct cn_device *cnd;
542
543	cnd = STAILQ_FIRST(&cn_devlist);
544	if (cn_mute || CND_INVALID(cnd, curthread))
545		return (1);
546	dev = cnd->cnd_vp->v_rdev;
547	if (dev != NULL)
548		return ((*devsw(dev)->d_kqfilter)(dev, kn));
549	return (1);
550}
551
552/*
553 * Low level console routines.
554 */
555int
556cngetc(void)
557{
558	int c;
559
560	if (cn_mute)
561		return (-1);
562	while ((c = cncheckc()) == -1)
563		;
564	if (c == '\r')
565		c = '\n';		/* console input is always ICRNL */
566	return (c);
567}
568
569int
570cncheckc(void)
571{
572	struct cn_device *cnd;
573	struct consdev *cn;
574	int c;
575
576	if (cn_mute)
577		return (-1);
578	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
579		cn = cnd->cnd_cn;
580#ifdef DDB
581		if (!db_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
582#endif
583			c = cn->cn_checkc(cn);
584			if (c != -1) {
585				return (c);
586			}
587#ifdef DDB
588		}
589#endif
590	}
591	return (-1);
592}
593
594void
595cnputc(int c)
596{
597	struct cn_device *cnd;
598	struct consdev *cn;
599	char *cp;
600
601	if (cn_mute || c == '\0')
602		return;
603	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
604		cn = cnd->cnd_cn;
605#ifdef DDB
606		if (!db_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
607#endif
608			if (c == '\n')
609				cn->cn_putc(cn, '\r');
610			cn->cn_putc(cn, c);
611#ifdef DDB
612		}
613#endif
614	}
615#ifdef DDB
616	if (console_pausing && !db_active && (c == '\n')) {
617#else
618	if (console_pausing && (c == '\n')) {
619#endif
620		for (cp = console_pausestr; *cp != '\0'; cp++)
621			cnputc(*cp);
622		if (cngetc() == '.')
623			console_pausing = 0;
624		cnputc('\r');
625		for (cp = console_pausestr; *cp != '\0'; cp++)
626			cnputc(' ');
627		cnputc('\r');
628	}
629}
630
631void
632cndbctl(int on)
633{
634	struct cn_device *cnd;
635	struct consdev *cn;
636	static int refcount;
637
638	if (!on)
639		refcount--;
640	if (refcount == 0)
641		STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
642			cn = cnd->cnd_cn;
643			if (cn->cn_dbctl != NULL)
644				cn->cn_dbctl(cn, on);
645		}
646	if (on)
647		refcount++;
648}
649
650static int consmsgbuf_size = 8192;
651SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
652    "");
653
654/*
655 * Redirect console output to a tty.
656 */
657void
658constty_set(struct tty *tp)
659{
660	int size;
661
662	KASSERT(tp != NULL, ("constty_set: NULL tp"));
663	if (consbuf == NULL) {
664		size = consmsgbuf_size;
665		consbuf = malloc(size, M_TTYS, M_WAITOK);
666		msgbuf_init(&consmsgbuf, consbuf, size);
667		callout_init(&conscallout, 0);
668	}
669	constty = tp;
670	constty_timeout(NULL);
671}
672
673/*
674 * Disable console redirection to a tty.
675 */
676void
677constty_clear(void)
678{
679	int c;
680
681	constty = NULL;
682	if (consbuf == NULL)
683		return;
684	callout_stop(&conscallout);
685	while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
686		cnputc(c);
687	free(consbuf, M_TTYS);
688	consbuf = NULL;
689}
690
691/* Times per second to check for pending console tty messages. */
692static int constty_wakeups_per_second = 5;
693SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
694    &constty_wakeups_per_second, 0, "");
695
696static void
697constty_timeout(void *arg)
698{
699	int c;
700
701	while (constty != NULL && (c = msgbuf_getchar(&consmsgbuf)) != -1) {
702		if (tputchar(c, constty) < 0)
703			constty = NULL;
704	}
705	if (constty != NULL) {
706		callout_reset(&conscallout, hz / constty_wakeups_per_second,
707		    constty_timeout, NULL);
708	} else {
709		/* Deallocate the constty buffer memory. */
710		constty_clear();
711	}
712}
713
714static void
715cn_drvinit(void *unused)
716{
717
718	make_dev(&cn_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "console");
719}
720
721SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL)
722