xref: /freebsd/sys/kern/kern_cons.c (revision 445ed7b40948c160f2f7d363d2d0ae1ffac4aabd)
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 		mtx_lock_spin(&cnputs_mtx);
516 		unlock_reqd = 1;
517 	}
518 
519 	while ((c = *p++) != '\0')
520 		cnputc(c);
521 
522 	if (unlock_reqd)
523 		mtx_unlock_spin(&cnputs_mtx);
524 }
525 
526 static int consmsgbuf_size = 8192;
527 SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
528     "Console tty buffer size");
529 
530 /*
531  * Redirect console output to a tty.
532  */
533 void
534 constty_set(struct tty *tp)
535 {
536 	int size;
537 
538 	KASSERT(tp != NULL, ("constty_set: NULL tp"));
539 	if (consbuf == NULL) {
540 		size = consmsgbuf_size;
541 		consbuf = malloc(size, M_TTYCONS, M_WAITOK);
542 		msgbuf_init(&consmsgbuf, consbuf, size);
543 		callout_init(&conscallout, 0);
544 	}
545 	constty = tp;
546 	constty_timeout(NULL);
547 }
548 
549 /*
550  * Disable console redirection to a tty.
551  */
552 void
553 constty_clear(void)
554 {
555 	int c;
556 
557 	constty = NULL;
558 	if (consbuf == NULL)
559 		return;
560 	callout_stop(&conscallout);
561 	while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
562 		cnputc(c);
563 	free(consbuf, M_TTYCONS);
564 	consbuf = NULL;
565 }
566 
567 /* Times per second to check for pending console tty messages. */
568 static int constty_wakeups_per_second = 5;
569 SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
570     &constty_wakeups_per_second, 0,
571     "Times per second to check for pending console tty messages");
572 
573 static void
574 constty_timeout(void *arg)
575 {
576 	int c;
577 
578 	if (constty != NULL) {
579 		tty_lock(constty);
580 		while ((c = msgbuf_getchar(&consmsgbuf)) != -1) {
581 			if (tty_putchar(constty, c) < 0) {
582 				tty_unlock(constty);
583 				constty = NULL;
584 				break;
585 			}
586 		}
587 
588 		if (constty != NULL)
589 			tty_unlock(constty);
590 	}
591 	if (constty != NULL) {
592 		callout_reset(&conscallout, hz / constty_wakeups_per_second,
593 		    constty_timeout, NULL);
594 	} else {
595 		/* Deallocate the constty buffer memory. */
596 		constty_clear();
597 	}
598 }
599 
600 static void
601 cn_drvinit(void *unused)
602 {
603 
604 	mtx_init(&cnputs_mtx, "cnputs_mtx", NULL, MTX_SPIN | MTX_NOWITNESS);
605 	use_cnputs_mtx = 1;
606 }
607 
608 SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL);
609 
610 /*
611  * Sysbeep(), if we have hardware for it
612  */
613 
614 #ifdef HAS_TIMER_SPKR
615 
616 static int beeping;
617 static struct callout beeping_timer;
618 
619 static void
620 sysbeepstop(void *chan)
621 {
622 
623 	timer_spkr_release();
624 	beeping = 0;
625 }
626 
627 int
628 sysbeep(int pitch, int period)
629 {
630 
631 	if (timer_spkr_acquire()) {
632 		if (!beeping) {
633 			/* Something else owns it. */
634 			return (EBUSY);
635 		}
636 	}
637 	timer_spkr_setfreq(pitch);
638 	if (!beeping) {
639 		beeping = period;
640 		callout_reset(&beeping_timer, period, sysbeepstop, NULL);
641 	}
642 	return (0);
643 }
644 
645 static void
646 sysbeep_init(void *unused)
647 {
648 
649 	callout_init(&beeping_timer, CALLOUT_MPSAFE);
650 }
651 SYSINIT(sysbeep, SI_SUB_SOFTINTR, SI_ORDER_ANY, sysbeep_init, NULL);
652 #else
653 
654 /*
655  * No hardware, no sound
656  */
657 
658 int
659 sysbeep(int pitch __unused, int period __unused)
660 {
661 
662 	return (ENODEV);
663 }
664 
665 #endif
666 
667 /*
668  * Temporary support for sc(4) to vt(4) transition.
669  */
670 static unsigned vty_prefer;
671 static char vty_name[16];
672 SYSCTL_STRING(_kern, OID_AUTO, vty, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, vty_name,
673     0, "Console vty driver");
674 
675 int
676 vty_enabled(unsigned vty)
677 {
678 	static unsigned vty_selected = 0;
679 
680 	if (vty_selected == 0) {
681 		TUNABLE_STR_FETCH("kern.vty", vty_name, sizeof(vty_name));
682 		do {
683 #if defined(DEV_SC)
684 			if (strcmp(vty_name, "sc") == 0) {
685 				vty_selected = VTY_SC;
686 				break;
687 			}
688 #endif
689 #if defined(DEV_VT)
690 			if (strcmp(vty_name, "vt") == 0) {
691 				vty_selected = VTY_VT;
692 				break;
693 			}
694 #endif
695 			if (vty_prefer != 0) {
696 				vty_selected = vty_prefer;
697 				break;
698 			}
699 #if defined(DEV_VT)
700 			vty_selected = VTY_VT;
701 #elif defined(DEV_SC)
702 			vty_selected = VTY_SC;
703 #endif
704 		} while (0);
705 
706 		if (vty_selected == VTY_VT)
707 			strcpy(vty_name, "vt");
708 		else if (vty_selected == VTY_SC)
709 			strcpy(vty_name, "sc");
710 	}
711 	return ((vty_selected & vty) != 0);
712 }
713 
714 void
715 vty_set_preferred(unsigned vty)
716 {
717 
718 	vty_prefer = vty;
719 #if !defined(DEV_SC)
720 	vty_prefer &= ~VTY_SC;
721 #endif
722 #if !defined(DEV_VT)
723 	vty_prefer &= ~VTY_VT;
724 #endif
725 }
726 
727