xref: /freebsd/sys/kern/kern_cons.c (revision db3cb3640f547c063293e9fdc4db69e9dc120951)
1 /*-
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1991 The Regents of the University of California.
4  * Copyright (c) 1999 Michael Smith
5  * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
6  *
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * the Systems Programming Group of the University of Utah Computer
11  * Science Department.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	from: @(#)cons.c	7.2 (Berkeley) 5/9/91
38  */
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include "opt_ddb.h"
44 #include "opt_syscons.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/conf.h>
51 #include <sys/cons.h>
52 #include <sys/fcntl.h>
53 #include <sys/kdb.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/msgbuf.h>
57 #include <sys/namei.h>
58 #include <sys/priv.h>
59 #include <sys/proc.h>
60 #include <sys/queue.h>
61 #include <sys/reboot.h>
62 #include <sys/sysctl.h>
63 #include <sys/sbuf.h>
64 #include <sys/tty.h>
65 #include <sys/uio.h>
66 #include <sys/vnode.h>
67 
68 #include <ddb/ddb.h>
69 
70 #include <machine/cpu.h>
71 #include <machine/clock.h>
72 
73 static MALLOC_DEFINE(M_TTYCONS, "tty console", "tty console handling");
74 
75 struct cn_device {
76 	STAILQ_ENTRY(cn_device) cnd_next;
77 	struct		consdev *cnd_cn;
78 };
79 
80 #define CNDEVPATHMAX	32
81 #define CNDEVTAB_SIZE	4
82 static struct cn_device cn_devtab[CNDEVTAB_SIZE];
83 static STAILQ_HEAD(, cn_device) cn_devlist =
84     STAILQ_HEAD_INITIALIZER(cn_devlist);
85 
86 int	cons_avail_mask = 0;	/* Bit mask. Each registered low level console
87 				 * which is currently unavailable for inpit
88 				 * (i.e., if it is in graphics mode) will have
89 				 * this bit cleared.
90 				 */
91 static int cn_mute;
92 static char *consbuf;			/* buffer used by `consmsgbuf' */
93 static struct callout conscallout;	/* callout for outputting to constty */
94 struct msgbuf consmsgbuf;		/* message buffer for console tty */
95 static u_char console_pausing;		/* pause after each line during probe */
96 static char *console_pausestr=
97 "<pause; press any key to proceed to next line or '.' to end pause mode>";
98 struct tty *constty;			/* pointer to console "window" tty */
99 static struct mtx cnputs_mtx;		/* Mutex for cnputs(). */
100 static int use_cnputs_mtx = 0;		/* != 0 if cnputs_mtx locking reqd. */
101 
102 static void constty_timeout(void *arg);
103 
104 static struct consdev cons_consdev;
105 DATA_SET(cons_set, cons_consdev);
106 SET_DECLARE(cons_set, struct consdev);
107 
108 void
109 cninit(void)
110 {
111 	struct consdev *best_cn, *cn, **list;
112 
113 	/*
114 	 * Check if we should mute the console (for security reasons perhaps)
115 	 * It can be changes dynamically using sysctl kern.consmute
116 	 * once we are up and going.
117 	 *
118 	 */
119         cn_mute = ((boothowto & (RB_MUTE
120 			|RB_SINGLE
121 			|RB_VERBOSE
122 			|RB_ASKNAME)) == RB_MUTE);
123 
124 	/*
125 	 * Find the first console with the highest priority.
126 	 */
127 	best_cn = NULL;
128 	SET_FOREACH(list, cons_set) {
129 		cn = *list;
130 		cnremove(cn);
131 		/* Skip cons_consdev. */
132 		if (cn->cn_ops == NULL)
133 			continue;
134 		cn->cn_ops->cn_probe(cn);
135 		if (cn->cn_pri == CN_DEAD)
136 			continue;
137 		if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
138 			best_cn = cn;
139 		if (boothowto & RB_MULTIPLE) {
140 			/*
141 			 * Initialize console, and attach to it.
142 			 */
143 			cn->cn_ops->cn_init(cn);
144 			cnadd(cn);
145 		}
146 	}
147 	if (best_cn == NULL)
148 		return;
149 	if ((boothowto & RB_MULTIPLE) == 0) {
150 		best_cn->cn_ops->cn_init(best_cn);
151 		cnadd(best_cn);
152 	}
153 	if (boothowto & RB_PAUSE)
154 		console_pausing = 1;
155 	/*
156 	 * Make the best console the preferred console.
157 	 */
158 	cnselect(best_cn);
159 
160 #ifdef EARLY_PRINTF
161 	/*
162 	 * Release early console.
163 	 */
164 	early_putc = NULL;
165 #endif
166 }
167 
168 void
169 cninit_finish()
170 {
171 	console_pausing = 0;
172 }
173 
174 /* add a new physical console to back the virtual console */
175 int
176 cnadd(struct consdev *cn)
177 {
178 	struct cn_device *cnd;
179 	int i;
180 
181 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
182 		if (cnd->cnd_cn == cn)
183 			return (0);
184 	for (i = 0; i < CNDEVTAB_SIZE; i++) {
185 		cnd = &cn_devtab[i];
186 		if (cnd->cnd_cn == NULL)
187 			break;
188 	}
189 	if (cnd->cnd_cn != NULL)
190 		return (ENOMEM);
191 	cnd->cnd_cn = cn;
192 	if (cn->cn_name[0] == '\0') {
193 		/* XXX: it is unclear if/where this print might output */
194 		printf("WARNING: console at %p has no name\n", cn);
195 	}
196 	STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
197 	if (STAILQ_FIRST(&cn_devlist) == cnd)
198 		ttyconsdev_select(cnd->cnd_cn->cn_name);
199 
200 	/* Add device to the active mask. */
201 	cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
202 
203 	return (0);
204 }
205 
206 void
207 cnremove(struct consdev *cn)
208 {
209 	struct cn_device *cnd;
210 	int i;
211 
212 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
213 		if (cnd->cnd_cn != cn)
214 			continue;
215 		if (STAILQ_FIRST(&cn_devlist) == cnd)
216 			ttyconsdev_select(NULL);
217 		STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
218 		cnd->cnd_cn = NULL;
219 
220 		/* Remove this device from available mask. */
221 		for (i = 0; i < CNDEVTAB_SIZE; i++)
222 			if (cnd == &cn_devtab[i]) {
223 				cons_avail_mask &= ~(1 << i);
224 				break;
225 			}
226 #if 0
227 		/*
228 		 * XXX
229 		 * syscons gets really confused if console resources are
230 		 * freed after the system has initialized.
231 		 */
232 		if (cn->cn_term != NULL)
233 			cn->cn_ops->cn_term(cn);
234 #endif
235 		return;
236 	}
237 }
238 
239 void
240 cnselect(struct consdev *cn)
241 {
242 	struct cn_device *cnd;
243 
244 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
245 		if (cnd->cnd_cn != cn)
246 			continue;
247 		if (cnd == STAILQ_FIRST(&cn_devlist))
248 			return;
249 		STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
250 		STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
251 		ttyconsdev_select(cnd->cnd_cn->cn_name);
252 		return;
253 	}
254 }
255 
256 void
257 cnavailable(struct consdev *cn, int available)
258 {
259 	int i;
260 
261 	for (i = 0; i < CNDEVTAB_SIZE; i++) {
262 		if (cn_devtab[i].cnd_cn == cn)
263 			break;
264 	}
265 	if (available) {
266 		if (i < CNDEVTAB_SIZE)
267 			cons_avail_mask |= (1 << i);
268 		cn->cn_flags &= ~CN_FLAG_NOAVAIL;
269 	} else {
270 		if (i < CNDEVTAB_SIZE)
271 			cons_avail_mask &= ~(1 << i);
272 		cn->cn_flags |= CN_FLAG_NOAVAIL;
273 	}
274 }
275 
276 int
277 cnunavailable(void)
278 {
279 
280 	return (cons_avail_mask == 0);
281 }
282 
283 /*
284  * sysctl_kern_console() provides output parseable in conscontrol(1).
285  */
286 static int
287 sysctl_kern_console(SYSCTL_HANDLER_ARGS)
288 {
289 	struct cn_device *cnd;
290 	struct consdev *cp, **list;
291 	char *p;
292 	int delete, error;
293 	struct sbuf *sb;
294 
295 	sb = sbuf_new(NULL, NULL, CNDEVPATHMAX * 2, SBUF_AUTOEXTEND);
296 	if (sb == NULL)
297 		return (ENOMEM);
298 	sbuf_clear(sb);
299 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
300 		sbuf_printf(sb, "%s,", cnd->cnd_cn->cn_name);
301 	sbuf_printf(sb, "/");
302 	SET_FOREACH(list, cons_set) {
303 		cp = *list;
304 		if (cp->cn_name[0] != '\0')
305 			sbuf_printf(sb, "%s,", cp->cn_name);
306 	}
307 	sbuf_finish(sb);
308 	error = sysctl_handle_string(oidp, sbuf_data(sb), sbuf_len(sb), req);
309 	if (error == 0 && req->newptr != NULL) {
310 		p = sbuf_data(sb);
311 		error = ENXIO;
312 		delete = 0;
313 		if (*p == '-') {
314 			delete = 1;
315 			p++;
316 		}
317 		SET_FOREACH(list, cons_set) {
318 			cp = *list;
319 			if (strcmp(p, cp->cn_name) != 0)
320 				continue;
321 			if (delete) {
322 				cnremove(cp);
323 				error = 0;
324 			} else {
325 				error = cnadd(cp);
326 				if (error == 0)
327 					cnselect(cp);
328 			}
329 			break;
330 		}
331 	}
332 	sbuf_delete(sb);
333 	return (error);
334 }
335 
336 SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW,
337 	0, 0, sysctl_kern_console, "A", "Console device control");
338 
339 /*
340  * User has changed the state of the console muting.
341  * This may require us to open or close the device in question.
342  */
343 static int
344 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
345 {
346 	int error;
347 
348 	error = sysctl_handle_int(oidp, &cn_mute, 0, req);
349 	if (error != 0 || req->newptr == NULL)
350 		return (error);
351 	return (error);
352 }
353 
354 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
355 	0, sizeof(cn_mute), sysctl_kern_consmute, "I",
356 	"State of the console muting");
357 
358 void
359 cngrab()
360 {
361 	struct cn_device *cnd;
362 	struct consdev *cn;
363 
364 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
365 		cn = cnd->cnd_cn;
366 		if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
367 			cn->cn_ops->cn_grab(cn);
368 	}
369 }
370 
371 void
372 cnungrab()
373 {
374 	struct cn_device *cnd;
375 	struct consdev *cn;
376 
377 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
378 		cn = cnd->cnd_cn;
379 		if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
380 			cn->cn_ops->cn_ungrab(cn);
381 	}
382 }
383 
384 /*
385  * Low level console routines.
386  */
387 int
388 cngetc(void)
389 {
390 	int c;
391 
392 	if (cn_mute)
393 		return (-1);
394 	while ((c = cncheckc()) == -1)
395 		cpu_spinwait();
396 	if (c == '\r')
397 		c = '\n';		/* console input is always ICRNL */
398 	return (c);
399 }
400 
401 int
402 cncheckc(void)
403 {
404 	struct cn_device *cnd;
405 	struct consdev *cn;
406 	int c;
407 
408 	if (cn_mute)
409 		return (-1);
410 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
411 		cn = cnd->cnd_cn;
412 		if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
413 			c = cn->cn_ops->cn_getc(cn);
414 			if (c != -1)
415 				return (c);
416 		}
417 	}
418 	return (-1);
419 }
420 
421 void
422 cngets(char *cp, size_t size, int visible)
423 {
424 	char *lp, *end;
425 	int c;
426 
427 	cngrab();
428 
429 	lp = cp;
430 	end = cp + size - 1;
431 	for (;;) {
432 		c = cngetc() & 0177;
433 		switch (c) {
434 		case '\n':
435 		case '\r':
436 			cnputc(c);
437 			*lp = '\0';
438 			cnungrab();
439 			return;
440 		case '\b':
441 		case '\177':
442 			if (lp > cp) {
443 				if (visible)
444 					cnputs("\b \b");
445 				lp--;
446 			}
447 			continue;
448 		case '\0':
449 			continue;
450 		default:
451 			if (lp < end) {
452 				switch (visible) {
453 				case GETS_NOECHO:
454 					break;
455 				case GETS_ECHOPASS:
456 					cnputc('*');
457 					break;
458 				default:
459 					cnputc(c);
460 					break;
461 				}
462 				*lp++ = c;
463 			}
464 		}
465 	}
466 }
467 
468 void
469 cnputc(int c)
470 {
471 	struct cn_device *cnd;
472 	struct consdev *cn;
473 	char *cp;
474 
475 #ifdef EARLY_PRINTF
476 	if (early_putc != NULL) {
477 		if (c == '\n')
478 			early_putc('\r');
479 		early_putc(c);
480 		return;
481 	}
482 #endif
483 
484 	if (cn_mute || c == '\0')
485 		return;
486 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
487 		cn = cnd->cnd_cn;
488 		if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
489 			if (c == '\n')
490 				cn->cn_ops->cn_putc(cn, '\r');
491 			cn->cn_ops->cn_putc(cn, c);
492 		}
493 	}
494 	if (console_pausing && c == '\n' && !kdb_active) {
495 		for (cp = console_pausestr; *cp != '\0'; cp++)
496 			cnputc(*cp);
497 		cngrab();
498 		if (cngetc() == '.')
499 			console_pausing = 0;
500 		cnungrab();
501 		cnputc('\r');
502 		for (cp = console_pausestr; *cp != '\0'; cp++)
503 			cnputc(' ');
504 		cnputc('\r');
505 	}
506 }
507 
508 void
509 cnputs(char *p)
510 {
511 	int c;
512 	int unlock_reqd = 0;
513 
514 	if (use_cnputs_mtx) {
515 	  	/*
516 		 * NOTE: Debug prints and/or witness printouts in
517 		 * console driver clients can cause the "cnputs_mtx"
518 		 * mutex to recurse. Simply return if that happens.
519 		 */
520 		if (mtx_owned(&cnputs_mtx))
521 			return;
522 		mtx_lock_spin(&cnputs_mtx);
523 		unlock_reqd = 1;
524 	}
525 
526 	while ((c = *p++) != '\0')
527 		cnputc(c);
528 
529 	if (unlock_reqd)
530 		mtx_unlock_spin(&cnputs_mtx);
531 }
532 
533 static int consmsgbuf_size = 8192;
534 SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
535     "Console tty buffer size");
536 
537 /*
538  * Redirect console output to a tty.
539  */
540 void
541 constty_set(struct tty *tp)
542 {
543 	int size;
544 
545 	KASSERT(tp != NULL, ("constty_set: NULL tp"));
546 	if (consbuf == NULL) {
547 		size = consmsgbuf_size;
548 		consbuf = malloc(size, M_TTYCONS, M_WAITOK);
549 		msgbuf_init(&consmsgbuf, consbuf, size);
550 		callout_init(&conscallout, 0);
551 	}
552 	constty = tp;
553 	constty_timeout(NULL);
554 }
555 
556 /*
557  * Disable console redirection to a tty.
558  */
559 void
560 constty_clear(void)
561 {
562 	int c;
563 
564 	constty = NULL;
565 	if (consbuf == NULL)
566 		return;
567 	callout_stop(&conscallout);
568 	while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
569 		cnputc(c);
570 	free(consbuf, M_TTYCONS);
571 	consbuf = NULL;
572 }
573 
574 /* Times per second to check for pending console tty messages. */
575 static int constty_wakeups_per_second = 5;
576 SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
577     &constty_wakeups_per_second, 0,
578     "Times per second to check for pending console tty messages");
579 
580 static void
581 constty_timeout(void *arg)
582 {
583 	int c;
584 
585 	if (constty != NULL) {
586 		tty_lock(constty);
587 		while ((c = msgbuf_getchar(&consmsgbuf)) != -1) {
588 			if (tty_putchar(constty, c) < 0) {
589 				tty_unlock(constty);
590 				constty = NULL;
591 				break;
592 			}
593 		}
594 
595 		if (constty != NULL)
596 			tty_unlock(constty);
597 	}
598 	if (constty != NULL) {
599 		callout_reset(&conscallout, hz / constty_wakeups_per_second,
600 		    constty_timeout, NULL);
601 	} else {
602 		/* Deallocate the constty buffer memory. */
603 		constty_clear();
604 	}
605 }
606 
607 static void
608 cn_drvinit(void *unused)
609 {
610 
611 	mtx_init(&cnputs_mtx, "cnputs_mtx", NULL, MTX_SPIN | MTX_NOWITNESS);
612 	use_cnputs_mtx = 1;
613 }
614 
615 SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL);
616 
617 /*
618  * Sysbeep(), if we have hardware for it
619  */
620 
621 #ifdef HAS_TIMER_SPKR
622 
623 static int beeping;
624 static struct callout beeping_timer;
625 
626 static void
627 sysbeepstop(void *chan)
628 {
629 
630 	timer_spkr_release();
631 	beeping = 0;
632 }
633 
634 int
635 sysbeep(int pitch, int period)
636 {
637 
638 	if (timer_spkr_acquire()) {
639 		if (!beeping) {
640 			/* Something else owns it. */
641 			return (EBUSY);
642 		}
643 	}
644 	timer_spkr_setfreq(pitch);
645 	if (!beeping) {
646 		beeping = period;
647 		callout_reset(&beeping_timer, period, sysbeepstop, NULL);
648 	}
649 	return (0);
650 }
651 
652 static void
653 sysbeep_init(void *unused)
654 {
655 
656 	callout_init(&beeping_timer, CALLOUT_MPSAFE);
657 }
658 SYSINIT(sysbeep, SI_SUB_SOFTINTR, SI_ORDER_ANY, sysbeep_init, NULL);
659 #else
660 
661 /*
662  * No hardware, no sound
663  */
664 
665 int
666 sysbeep(int pitch __unused, int period __unused)
667 {
668 
669 	return (ENODEV);
670 }
671 
672 #endif
673 
674 /*
675  * Temporary support for sc(4) to vt(4) transition.
676  */
677 static unsigned vty_prefer;
678 static char vty_name[16];
679 SYSCTL_STRING(_kern, OID_AUTO, vty, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, vty_name,
680     0, "Console vty driver");
681 
682 int
683 vty_enabled(unsigned vty)
684 {
685 	static unsigned vty_selected = 0;
686 
687 	if (vty_selected == 0) {
688 		TUNABLE_STR_FETCH("kern.vty", vty_name, sizeof(vty_name));
689 		do {
690 #if defined(DEV_SC)
691 			if (strcmp(vty_name, "sc") == 0) {
692 				vty_selected = VTY_SC;
693 				break;
694 			}
695 #endif
696 #if defined(DEV_VT)
697 			if (strcmp(vty_name, "vt") == 0) {
698 				vty_selected = VTY_VT;
699 				break;
700 			}
701 #endif
702 			if (vty_prefer != 0) {
703 				vty_selected = vty_prefer;
704 				break;
705 			}
706 #if defined(DEV_VT)
707 			vty_selected = VTY_VT;
708 #elif defined(DEV_SC)
709 			vty_selected = VTY_SC;
710 #endif
711 		} while (0);
712 
713 		if (vty_selected == VTY_VT)
714 			strcpy(vty_name, "vt");
715 		else if (vty_selected == VTY_SC)
716 			strcpy(vty_name, "sc");
717 	}
718 	return ((vty_selected & vty) != 0);
719 }
720 
721 void
722 vty_set_preferred(unsigned vty)
723 {
724 
725 	vty_prefer = vty;
726 #if !defined(DEV_SC)
727 	vty_prefer &= ~VTY_SC;
728 #endif
729 #if !defined(DEV_VT)
730 	vty_prefer &= ~VTY_VT;
731 #endif
732 }
733 
734