xref: /freebsd/sys/kern/kern_cons.c (revision 3ed7166aca6c1450c2cc7ad58f3244c80dc08994)
1c9dba40cSEd Schouten /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4c9dba40cSEd Schouten  * Copyright (c) 1988 University of Utah.
5c9dba40cSEd Schouten  * Copyright (c) 1991 The Regents of the University of California.
6f389bc95SAndriy Gapon  * Copyright (c) 1999 Michael Smith
7f389bc95SAndriy Gapon  * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
8f389bc95SAndriy Gapon  *
9c9dba40cSEd Schouten  * All rights reserved.
10c9dba40cSEd Schouten  *
11c9dba40cSEd Schouten  * This code is derived from software contributed to Berkeley by
12c9dba40cSEd Schouten  * the Systems Programming Group of the University of Utah Computer
13c9dba40cSEd Schouten  * Science Department.
14c9dba40cSEd Schouten  *
15c9dba40cSEd Schouten  * Redistribution and use in source and binary forms, with or without
16c9dba40cSEd Schouten  * modification, are permitted provided that the following conditions
17c9dba40cSEd Schouten  * are met:
18c9dba40cSEd Schouten  * 1. Redistributions of source code must retain the above copyright
19c9dba40cSEd Schouten  *    notice, this list of conditions and the following disclaimer.
20c9dba40cSEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
21c9dba40cSEd Schouten  *    notice, this list of conditions and the following disclaimer in the
22c9dba40cSEd Schouten  *    documentation and/or other materials provided with the distribution.
2369a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
24c9dba40cSEd Schouten  *    may be used to endorse or promote products derived from this software
25c9dba40cSEd Schouten  *    without specific prior written permission.
26c9dba40cSEd Schouten  *
27c9dba40cSEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28c9dba40cSEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29c9dba40cSEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30c9dba40cSEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31c9dba40cSEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32c9dba40cSEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33c9dba40cSEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34c9dba40cSEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35c9dba40cSEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36c9dba40cSEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37c9dba40cSEd Schouten  * SUCH DAMAGE.
38c9dba40cSEd Schouten  *
39c9dba40cSEd Schouten  *	from: @(#)cons.c	7.2 (Berkeley) 5/9/91
40c9dba40cSEd Schouten  */
41c9dba40cSEd Schouten 
42c9dba40cSEd Schouten #include <sys/cdefs.h>
43c9dba40cSEd Schouten __FBSDID("$FreeBSD$");
44c9dba40cSEd Schouten 
45c9dba40cSEd Schouten #include "opt_ddb.h"
4659644098SEd Maste #include "opt_syscons.h"
47c9dba40cSEd Schouten 
48c9dba40cSEd Schouten #include <sys/param.h>
49c9dba40cSEd Schouten #include <sys/systm.h>
50c9dba40cSEd Schouten #include <sys/lock.h>
51c9dba40cSEd Schouten #include <sys/mutex.h>
52c9dba40cSEd Schouten #include <sys/conf.h>
53c9dba40cSEd Schouten #include <sys/cons.h>
54c9dba40cSEd Schouten #include <sys/fcntl.h>
55*3ed7166aSKyle Evans #include <sys/kbio.h>
56c9dba40cSEd Schouten #include <sys/kdb.h>
57c9dba40cSEd Schouten #include <sys/kernel.h>
58c9dba40cSEd Schouten #include <sys/malloc.h>
59c9dba40cSEd Schouten #include <sys/msgbuf.h>
60c9dba40cSEd Schouten #include <sys/namei.h>
61c9dba40cSEd Schouten #include <sys/priv.h>
62c9dba40cSEd Schouten #include <sys/proc.h>
63c9dba40cSEd Schouten #include <sys/queue.h>
64c9dba40cSEd Schouten #include <sys/reboot.h>
65c9dba40cSEd Schouten #include <sys/sysctl.h>
66c9dba40cSEd Schouten #include <sys/sbuf.h>
67c9dba40cSEd Schouten #include <sys/tty.h>
68c9dba40cSEd Schouten #include <sys/uio.h>
69c9dba40cSEd Schouten #include <sys/vnode.h>
70c9dba40cSEd Schouten 
71c9dba40cSEd Schouten #include <ddb/ddb.h>
72c9dba40cSEd Schouten 
73*3ed7166aSKyle Evans #include <dev/kbd/kbdreg.h>
74*3ed7166aSKyle Evans 
75c9dba40cSEd Schouten #include <machine/cpu.h>
76c9dba40cSEd Schouten #include <machine/clock.h>
77c9dba40cSEd Schouten 
78c9dba40cSEd Schouten static MALLOC_DEFINE(M_TTYCONS, "tty console", "tty console handling");
79c9dba40cSEd Schouten 
80c9dba40cSEd Schouten struct cn_device {
81c9dba40cSEd Schouten 	STAILQ_ENTRY(cn_device) cnd_next;
82c9dba40cSEd Schouten 	struct		consdev *cnd_cn;
83c9dba40cSEd Schouten };
84c9dba40cSEd Schouten 
85c9dba40cSEd Schouten #define CNDEVPATHMAX	32
86c9dba40cSEd Schouten #define CNDEVTAB_SIZE	4
87c9dba40cSEd Schouten static struct cn_device cn_devtab[CNDEVTAB_SIZE];
88c9dba40cSEd Schouten static STAILQ_HEAD(, cn_device) cn_devlist =
89c9dba40cSEd Schouten     STAILQ_HEAD_INITIALIZER(cn_devlist);
90c9dba40cSEd Schouten 
91c9dba40cSEd Schouten int	cons_avail_mask = 0;	/* Bit mask. Each registered low level console
92c9dba40cSEd Schouten 				 * which is currently unavailable for inpit
93c9dba40cSEd Schouten 				 * (i.e., if it is in graphics mode) will have
94c9dba40cSEd Schouten 				 * this bit cleared.
95c9dba40cSEd Schouten 				 */
96c9dba40cSEd Schouten static int cn_mute;
97c9dba40cSEd Schouten static char *consbuf;			/* buffer used by `consmsgbuf' */
98c9dba40cSEd Schouten static struct callout conscallout;	/* callout for outputting to constty */
99c9dba40cSEd Schouten struct msgbuf consmsgbuf;		/* message buffer for console tty */
100c9dba40cSEd Schouten static u_char console_pausing;		/* pause after each line during probe */
101c9dba40cSEd Schouten static char *console_pausestr=
102c9dba40cSEd Schouten "<pause; press any key to proceed to next line or '.' to end pause mode>";
103c9dba40cSEd Schouten struct tty *constty;			/* pointer to console "window" tty */
104c9dba40cSEd Schouten static struct mtx cnputs_mtx;		/* Mutex for cnputs(). */
105c9dba40cSEd Schouten static int use_cnputs_mtx = 0;		/* != 0 if cnputs_mtx locking reqd. */
106c9dba40cSEd Schouten 
107c9dba40cSEd Schouten static void constty_timeout(void *arg);
108c9dba40cSEd Schouten 
109c9dba40cSEd Schouten static struct consdev cons_consdev;
110c9dba40cSEd Schouten DATA_SET(cons_set, cons_consdev);
111c9dba40cSEd Schouten SET_DECLARE(cons_set, struct consdev);
112c9dba40cSEd Schouten 
113c9dba40cSEd Schouten void
114c9dba40cSEd Schouten cninit(void)
115c9dba40cSEd Schouten {
116c9dba40cSEd Schouten 	struct consdev *best_cn, *cn, **list;
117c9dba40cSEd Schouten 
118c9dba40cSEd Schouten 	/*
119c9dba40cSEd Schouten 	 * Check if we should mute the console (for security reasons perhaps)
120c9dba40cSEd Schouten 	 * It can be changes dynamically using sysctl kern.consmute
121c9dba40cSEd Schouten 	 * once we are up and going.
122c9dba40cSEd Schouten 	 *
123c9dba40cSEd Schouten 	 */
124c9dba40cSEd Schouten         cn_mute = ((boothowto & (RB_MUTE
125c9dba40cSEd Schouten 			|RB_SINGLE
126c9dba40cSEd Schouten 			|RB_VERBOSE
127c9dba40cSEd Schouten 			|RB_ASKNAME)) == RB_MUTE);
128c9dba40cSEd Schouten 
129c9dba40cSEd Schouten 	/*
130*3ed7166aSKyle Evans 	 * Bring up the kbd layer just in time for cnprobe.  Console drivers
131*3ed7166aSKyle Evans 	 * have a dependency on kbd being ready, so this fits nicely between the
132*3ed7166aSKyle Evans 	 * machdep callers of cninit() and MI probing/initialization of consoles
133*3ed7166aSKyle Evans 	 * here.
134*3ed7166aSKyle Evans 	 */
135*3ed7166aSKyle Evans 	kbdinit();
136*3ed7166aSKyle Evans 
137*3ed7166aSKyle Evans 	/*
138c9dba40cSEd Schouten 	 * Find the first console with the highest priority.
139c9dba40cSEd Schouten 	 */
140c9dba40cSEd Schouten 	best_cn = NULL;
141c9dba40cSEd Schouten 	SET_FOREACH(list, cons_set) {
142c9dba40cSEd Schouten 		cn = *list;
143c9dba40cSEd Schouten 		cnremove(cn);
1442992abe0SEd Schouten 		/* Skip cons_consdev. */
1452992abe0SEd Schouten 		if (cn->cn_ops == NULL)
146c9dba40cSEd Schouten 			continue;
1472992abe0SEd Schouten 		cn->cn_ops->cn_probe(cn);
148c9dba40cSEd Schouten 		if (cn->cn_pri == CN_DEAD)
149c9dba40cSEd Schouten 			continue;
150c9dba40cSEd Schouten 		if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
151c9dba40cSEd Schouten 			best_cn = cn;
152c9dba40cSEd Schouten 		if (boothowto & RB_MULTIPLE) {
153c9dba40cSEd Schouten 			/*
154c9dba40cSEd Schouten 			 * Initialize console, and attach to it.
155c9dba40cSEd Schouten 			 */
1562992abe0SEd Schouten 			cn->cn_ops->cn_init(cn);
157c9dba40cSEd Schouten 			cnadd(cn);
158c9dba40cSEd Schouten 		}
159c9dba40cSEd Schouten 	}
160c9dba40cSEd Schouten 	if (best_cn == NULL)
161c9dba40cSEd Schouten 		return;
162c9dba40cSEd Schouten 	if ((boothowto & RB_MULTIPLE) == 0) {
1632992abe0SEd Schouten 		best_cn->cn_ops->cn_init(best_cn);
164c9dba40cSEd Schouten 		cnadd(best_cn);
165c9dba40cSEd Schouten 	}
166c9dba40cSEd Schouten 	if (boothowto & RB_PAUSE)
167c9dba40cSEd Schouten 		console_pausing = 1;
168c9dba40cSEd Schouten 	/*
169c9dba40cSEd Schouten 	 * Make the best console the preferred console.
170c9dba40cSEd Schouten 	 */
171c9dba40cSEd Schouten 	cnselect(best_cn);
172dc61566fSZbigniew Bodek 
173dc61566fSZbigniew Bodek #ifdef EARLY_PRINTF
174dc61566fSZbigniew Bodek 	/*
175dc61566fSZbigniew Bodek 	 * Release early console.
176dc61566fSZbigniew Bodek 	 */
177dc61566fSZbigniew Bodek 	early_putc = NULL;
178dc61566fSZbigniew Bodek #endif
179c9dba40cSEd Schouten }
180c9dba40cSEd Schouten 
181c9dba40cSEd Schouten void
182c9dba40cSEd Schouten cninit_finish()
183c9dba40cSEd Schouten {
184c9dba40cSEd Schouten 	console_pausing = 0;
185c9dba40cSEd Schouten }
186c9dba40cSEd Schouten 
187c9dba40cSEd Schouten /* add a new physical console to back the virtual console */
188c9dba40cSEd Schouten int
189c9dba40cSEd Schouten cnadd(struct consdev *cn)
190c9dba40cSEd Schouten {
191c9dba40cSEd Schouten 	struct cn_device *cnd;
192c9dba40cSEd Schouten 	int i;
193c9dba40cSEd Schouten 
194c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
195c9dba40cSEd Schouten 		if (cnd->cnd_cn == cn)
196c9dba40cSEd Schouten 			return (0);
197c9dba40cSEd Schouten 	for (i = 0; i < CNDEVTAB_SIZE; i++) {
198c9dba40cSEd Schouten 		cnd = &cn_devtab[i];
199c9dba40cSEd Schouten 		if (cnd->cnd_cn == NULL)
200c9dba40cSEd Schouten 			break;
201c9dba40cSEd Schouten 	}
202c9dba40cSEd Schouten 	if (cnd->cnd_cn != NULL)
203c9dba40cSEd Schouten 		return (ENOMEM);
204c9dba40cSEd Schouten 	cnd->cnd_cn = cn;
205c9dba40cSEd Schouten 	if (cn->cn_name[0] == '\0') {
206c9dba40cSEd Schouten 		/* XXX: it is unclear if/where this print might output */
207c9dba40cSEd Schouten 		printf("WARNING: console at %p has no name\n", cn);
208c9dba40cSEd Schouten 	}
209c9dba40cSEd Schouten 	STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
210c9dba40cSEd Schouten 	if (STAILQ_FIRST(&cn_devlist) == cnd)
211c9dba40cSEd Schouten 		ttyconsdev_select(cnd->cnd_cn->cn_name);
212c9dba40cSEd Schouten 
213c9dba40cSEd Schouten 	/* Add device to the active mask. */
214c9dba40cSEd Schouten 	cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
215c9dba40cSEd Schouten 
216c9dba40cSEd Schouten 	return (0);
217c9dba40cSEd Schouten }
218c9dba40cSEd Schouten 
219c9dba40cSEd Schouten void
220c9dba40cSEd Schouten cnremove(struct consdev *cn)
221c9dba40cSEd Schouten {
222c9dba40cSEd Schouten 	struct cn_device *cnd;
223c9dba40cSEd Schouten 	int i;
224c9dba40cSEd Schouten 
225c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
226c9dba40cSEd Schouten 		if (cnd->cnd_cn != cn)
227c9dba40cSEd Schouten 			continue;
228c9dba40cSEd Schouten 		if (STAILQ_FIRST(&cn_devlist) == cnd)
229c9dba40cSEd Schouten 			ttyconsdev_select(NULL);
230c9dba40cSEd Schouten 		STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
231c9dba40cSEd Schouten 		cnd->cnd_cn = NULL;
232c9dba40cSEd Schouten 
233c9dba40cSEd Schouten 		/* Remove this device from available mask. */
234c9dba40cSEd Schouten 		for (i = 0; i < CNDEVTAB_SIZE; i++)
235c9dba40cSEd Schouten 			if (cnd == &cn_devtab[i]) {
236c9dba40cSEd Schouten 				cons_avail_mask &= ~(1 << i);
237c9dba40cSEd Schouten 				break;
238c9dba40cSEd Schouten 			}
239c9dba40cSEd Schouten #if 0
240c9dba40cSEd Schouten 		/*
241c9dba40cSEd Schouten 		 * XXX
242c9dba40cSEd Schouten 		 * syscons gets really confused if console resources are
243c9dba40cSEd Schouten 		 * freed after the system has initialized.
244c9dba40cSEd Schouten 		 */
245c9dba40cSEd Schouten 		if (cn->cn_term != NULL)
2462992abe0SEd Schouten 			cn->cn_ops->cn_term(cn);
247c9dba40cSEd Schouten #endif
248c9dba40cSEd Schouten 		return;
249c9dba40cSEd Schouten 	}
250c9dba40cSEd Schouten }
251c9dba40cSEd Schouten 
252c9dba40cSEd Schouten void
253c9dba40cSEd Schouten cnselect(struct consdev *cn)
254c9dba40cSEd Schouten {
255c9dba40cSEd Schouten 	struct cn_device *cnd;
256c9dba40cSEd Schouten 
257c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
258c9dba40cSEd Schouten 		if (cnd->cnd_cn != cn)
259c9dba40cSEd Schouten 			continue;
260c9dba40cSEd Schouten 		if (cnd == STAILQ_FIRST(&cn_devlist))
261c9dba40cSEd Schouten 			return;
262c9dba40cSEd Schouten 		STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
263c9dba40cSEd Schouten 		STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
264c9dba40cSEd Schouten 		ttyconsdev_select(cnd->cnd_cn->cn_name);
265c9dba40cSEd Schouten 		return;
266c9dba40cSEd Schouten 	}
267c9dba40cSEd Schouten }
268c9dba40cSEd Schouten 
269c9dba40cSEd Schouten void
270c9dba40cSEd Schouten cnavailable(struct consdev *cn, int available)
271c9dba40cSEd Schouten {
272c9dba40cSEd Schouten 	int i;
273c9dba40cSEd Schouten 
274c9dba40cSEd Schouten 	for (i = 0; i < CNDEVTAB_SIZE; i++) {
275c9dba40cSEd Schouten 		if (cn_devtab[i].cnd_cn == cn)
276c9dba40cSEd Schouten 			break;
277c9dba40cSEd Schouten 	}
278c9dba40cSEd Schouten 	if (available) {
279c9dba40cSEd Schouten 		if (i < CNDEVTAB_SIZE)
280c9dba40cSEd Schouten 			cons_avail_mask |= (1 << i);
281c9dba40cSEd Schouten 		cn->cn_flags &= ~CN_FLAG_NOAVAIL;
282c9dba40cSEd Schouten 	} else {
283c9dba40cSEd Schouten 		if (i < CNDEVTAB_SIZE)
284c9dba40cSEd Schouten 			cons_avail_mask &= ~(1 << i);
285c9dba40cSEd Schouten 		cn->cn_flags |= CN_FLAG_NOAVAIL;
286c9dba40cSEd Schouten 	}
287c9dba40cSEd Schouten }
288c9dba40cSEd Schouten 
289c9dba40cSEd Schouten int
290c9dba40cSEd Schouten cnunavailable(void)
291c9dba40cSEd Schouten {
292c9dba40cSEd Schouten 
293c9dba40cSEd Schouten 	return (cons_avail_mask == 0);
294c9dba40cSEd Schouten }
295c9dba40cSEd Schouten 
296c9dba40cSEd Schouten /*
297c9dba40cSEd Schouten  * sysctl_kern_console() provides output parseable in conscontrol(1).
298c9dba40cSEd Schouten  */
299c9dba40cSEd Schouten static int
300c9dba40cSEd Schouten sysctl_kern_console(SYSCTL_HANDLER_ARGS)
301c9dba40cSEd Schouten {
302c9dba40cSEd Schouten 	struct cn_device *cnd;
303c9dba40cSEd Schouten 	struct consdev *cp, **list;
304c9dba40cSEd Schouten 	char *p;
305c9dba40cSEd Schouten 	int delete, error;
306c9dba40cSEd Schouten 	struct sbuf *sb;
307c9dba40cSEd Schouten 
308657282e0SIan Lepore 	sb = sbuf_new(NULL, NULL, CNDEVPATHMAX * 2, SBUF_AUTOEXTEND |
309657282e0SIan Lepore 	    SBUF_INCLUDENUL);
310c9dba40cSEd Schouten 	if (sb == NULL)
311c9dba40cSEd Schouten 		return (ENOMEM);
312c9dba40cSEd Schouten 	sbuf_clear(sb);
313c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
314c9dba40cSEd Schouten 		sbuf_printf(sb, "%s,", cnd->cnd_cn->cn_name);
315c9dba40cSEd Schouten 	sbuf_printf(sb, "/");
316c9dba40cSEd Schouten 	SET_FOREACH(list, cons_set) {
317c9dba40cSEd Schouten 		cp = *list;
318c9dba40cSEd Schouten 		if (cp->cn_name[0] != '\0')
319c9dba40cSEd Schouten 			sbuf_printf(sb, "%s,", cp->cn_name);
320c9dba40cSEd Schouten 	}
321c9dba40cSEd Schouten 	sbuf_finish(sb);
322c9dba40cSEd Schouten 	error = sysctl_handle_string(oidp, sbuf_data(sb), sbuf_len(sb), req);
323c9dba40cSEd Schouten 	if (error == 0 && req->newptr != NULL) {
324c9dba40cSEd Schouten 		p = sbuf_data(sb);
325c9dba40cSEd Schouten 		error = ENXIO;
326c9dba40cSEd Schouten 		delete = 0;
327c9dba40cSEd Schouten 		if (*p == '-') {
328c9dba40cSEd Schouten 			delete = 1;
329c9dba40cSEd Schouten 			p++;
330c9dba40cSEd Schouten 		}
331c9dba40cSEd Schouten 		SET_FOREACH(list, cons_set) {
332c9dba40cSEd Schouten 			cp = *list;
333c9dba40cSEd Schouten 			if (strcmp(p, cp->cn_name) != 0)
334c9dba40cSEd Schouten 				continue;
335c9dba40cSEd Schouten 			if (delete) {
336c9dba40cSEd Schouten 				cnremove(cp);
337c9dba40cSEd Schouten 				error = 0;
338c9dba40cSEd Schouten 			} else {
339c9dba40cSEd Schouten 				error = cnadd(cp);
340c9dba40cSEd Schouten 				if (error == 0)
341c9dba40cSEd Schouten 					cnselect(cp);
342c9dba40cSEd Schouten 			}
343c9dba40cSEd Schouten 			break;
344c9dba40cSEd Schouten 		}
345c9dba40cSEd Schouten 	}
346c9dba40cSEd Schouten 	sbuf_delete(sb);
347c9dba40cSEd Schouten 	return (error);
348c9dba40cSEd Schouten }
349c9dba40cSEd Schouten 
350c9dba40cSEd Schouten SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW,
351c9dba40cSEd Schouten 	0, 0, sysctl_kern_console, "A", "Console device control");
352c9dba40cSEd Schouten 
353c9dba40cSEd Schouten /*
354c9dba40cSEd Schouten  * User has changed the state of the console muting.
355c9dba40cSEd Schouten  * This may require us to open or close the device in question.
356c9dba40cSEd Schouten  */
357c9dba40cSEd Schouten static int
358c9dba40cSEd Schouten sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
359c9dba40cSEd Schouten {
360c9dba40cSEd Schouten 	int error;
361c9dba40cSEd Schouten 
362c9dba40cSEd Schouten 	error = sysctl_handle_int(oidp, &cn_mute, 0, req);
363c9dba40cSEd Schouten 	if (error != 0 || req->newptr == NULL)
364c9dba40cSEd Schouten 		return (error);
365c9dba40cSEd Schouten 	return (error);
366c9dba40cSEd Schouten }
367c9dba40cSEd Schouten 
368c9dba40cSEd Schouten SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
369a0c87b74SGavin Atkinson 	0, sizeof(cn_mute), sysctl_kern_consmute, "I",
370a0c87b74SGavin Atkinson 	"State of the console muting");
371c9dba40cSEd Schouten 
3729976156fSAndriy Gapon void
3739976156fSAndriy Gapon cngrab()
3749976156fSAndriy Gapon {
3759976156fSAndriy Gapon 	struct cn_device *cnd;
3769976156fSAndriy Gapon 	struct consdev *cn;
3779976156fSAndriy Gapon 
3789976156fSAndriy Gapon 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
3799976156fSAndriy Gapon 		cn = cnd->cnd_cn;
3809976156fSAndriy Gapon 		if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
3819976156fSAndriy Gapon 			cn->cn_ops->cn_grab(cn);
3829976156fSAndriy Gapon 	}
3839976156fSAndriy Gapon }
3849976156fSAndriy Gapon 
3859976156fSAndriy Gapon void
3869976156fSAndriy Gapon cnungrab()
3879976156fSAndriy Gapon {
3889976156fSAndriy Gapon 	struct cn_device *cnd;
3899976156fSAndriy Gapon 	struct consdev *cn;
3909976156fSAndriy Gapon 
3919976156fSAndriy Gapon 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
3929976156fSAndriy Gapon 		cn = cnd->cnd_cn;
3939976156fSAndriy Gapon 		if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
3949976156fSAndriy Gapon 			cn->cn_ops->cn_ungrab(cn);
3959976156fSAndriy Gapon 	}
3969976156fSAndriy Gapon }
3979976156fSAndriy Gapon 
398ec6faf94SAndriy Gapon void
399ec6faf94SAndriy Gapon cnresume()
400ec6faf94SAndriy Gapon {
401ec6faf94SAndriy Gapon 	struct cn_device *cnd;
402ec6faf94SAndriy Gapon 	struct consdev *cn;
403ec6faf94SAndriy Gapon 
404ec6faf94SAndriy Gapon 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
405ec6faf94SAndriy Gapon 		cn = cnd->cnd_cn;
406ec6faf94SAndriy Gapon 		if (cn->cn_ops->cn_resume != NULL)
407ec6faf94SAndriy Gapon 			cn->cn_ops->cn_resume(cn);
408ec6faf94SAndriy Gapon 	}
409ec6faf94SAndriy Gapon }
410ec6faf94SAndriy Gapon 
411c9dba40cSEd Schouten /*
412c9dba40cSEd Schouten  * Low level console routines.
413c9dba40cSEd Schouten  */
414c9dba40cSEd Schouten int
415c9dba40cSEd Schouten cngetc(void)
416c9dba40cSEd Schouten {
417c9dba40cSEd Schouten 	int c;
418c9dba40cSEd Schouten 
419c9dba40cSEd Schouten 	if (cn_mute)
420c9dba40cSEd Schouten 		return (-1);
421c9dba40cSEd Schouten 	while ((c = cncheckc()) == -1)
422298fbd16SAndriy Gapon 		cpu_spinwait();
423c9dba40cSEd Schouten 	if (c == '\r')
424c9dba40cSEd Schouten 		c = '\n';		/* console input is always ICRNL */
425c9dba40cSEd Schouten 	return (c);
426c9dba40cSEd Schouten }
427c9dba40cSEd Schouten 
428c9dba40cSEd Schouten int
429c9dba40cSEd Schouten cncheckc(void)
430c9dba40cSEd Schouten {
431c9dba40cSEd Schouten 	struct cn_device *cnd;
432c9dba40cSEd Schouten 	struct consdev *cn;
433c9dba40cSEd Schouten 	int c;
434c9dba40cSEd Schouten 
435c9dba40cSEd Schouten 	if (cn_mute)
436c9dba40cSEd Schouten 		return (-1);
437c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
438c9dba40cSEd Schouten 		cn = cnd->cnd_cn;
439c9dba40cSEd Schouten 		if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
4402992abe0SEd Schouten 			c = cn->cn_ops->cn_getc(cn);
4412992abe0SEd Schouten 			if (c != -1)
442c9dba40cSEd Schouten 				return (c);
443c9dba40cSEd Schouten 		}
444c9dba40cSEd Schouten 	}
445c9dba40cSEd Schouten 	return (-1);
446c9dba40cSEd Schouten }
447c9dba40cSEd Schouten 
448c9dba40cSEd Schouten void
4498e628542SAndriy Gapon cngets(char *cp, size_t size, int visible)
4508e628542SAndriy Gapon {
4518e628542SAndriy Gapon 	char *lp, *end;
4528e628542SAndriy Gapon 	int c;
4538e628542SAndriy Gapon 
4548e628542SAndriy Gapon 	cngrab();
4558e628542SAndriy Gapon 
4568e628542SAndriy Gapon 	lp = cp;
4578e628542SAndriy Gapon 	end = cp + size - 1;
4588e628542SAndriy Gapon 	for (;;) {
4598e628542SAndriy Gapon 		c = cngetc() & 0177;
4608e628542SAndriy Gapon 		switch (c) {
4618e628542SAndriy Gapon 		case '\n':
4628e628542SAndriy Gapon 		case '\r':
4638e628542SAndriy Gapon 			cnputc(c);
4648e628542SAndriy Gapon 			*lp = '\0';
4658e628542SAndriy Gapon 			cnungrab();
4668e628542SAndriy Gapon 			return;
4678e628542SAndriy Gapon 		case '\b':
4688e628542SAndriy Gapon 		case '\177':
4698e628542SAndriy Gapon 			if (lp > cp) {
4709520f952SWarner Losh 				if (visible)
4719520f952SWarner Losh 					cnputs("\b \b");
4728e628542SAndriy Gapon 				lp--;
4738e628542SAndriy Gapon 			}
4748e628542SAndriy Gapon 			continue;
4758e628542SAndriy Gapon 		case '\0':
4768e628542SAndriy Gapon 			continue;
4778e628542SAndriy Gapon 		default:
4788e628542SAndriy Gapon 			if (lp < end) {
4798e628542SAndriy Gapon 				switch (visible) {
4808e628542SAndriy Gapon 				case GETS_NOECHO:
4818e628542SAndriy Gapon 					break;
4828e628542SAndriy Gapon 				case GETS_ECHOPASS:
4838e628542SAndriy Gapon 					cnputc('*');
4848e628542SAndriy Gapon 					break;
4858e628542SAndriy Gapon 				default:
4868e628542SAndriy Gapon 					cnputc(c);
4878e628542SAndriy Gapon 					break;
4888e628542SAndriy Gapon 				}
4898e628542SAndriy Gapon 				*lp++ = c;
4908e628542SAndriy Gapon 			}
4918e628542SAndriy Gapon 		}
4928e628542SAndriy Gapon 	}
4938e628542SAndriy Gapon }
4948e628542SAndriy Gapon 
4958e628542SAndriy Gapon void
496c9dba40cSEd Schouten cnputc(int c)
497c9dba40cSEd Schouten {
498c9dba40cSEd Schouten 	struct cn_device *cnd;
499c9dba40cSEd Schouten 	struct consdev *cn;
500c9dba40cSEd Schouten 	char *cp;
501c9dba40cSEd Schouten 
50242c8459bSIan Lepore #ifdef EARLY_PRINTF
50342c8459bSIan Lepore 	if (early_putc != NULL) {
50442c8459bSIan Lepore 		if (c == '\n')
50542c8459bSIan Lepore 			early_putc('\r');
50642c8459bSIan Lepore 		early_putc(c);
50742c8459bSIan Lepore 		return;
50842c8459bSIan Lepore 	}
50942c8459bSIan Lepore #endif
51042c8459bSIan Lepore 
511c9dba40cSEd Schouten 	if (cn_mute || c == '\0')
512c9dba40cSEd Schouten 		return;
513c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
514c9dba40cSEd Schouten 		cn = cnd->cnd_cn;
515c9dba40cSEd Schouten 		if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
516c9dba40cSEd Schouten 			if (c == '\n')
5172992abe0SEd Schouten 				cn->cn_ops->cn_putc(cn, '\r');
5182992abe0SEd Schouten 			cn->cn_ops->cn_putc(cn, c);
519c9dba40cSEd Schouten 		}
520c9dba40cSEd Schouten 	}
521c9dba40cSEd Schouten 	if (console_pausing && c == '\n' && !kdb_active) {
522c9dba40cSEd Schouten 		for (cp = console_pausestr; *cp != '\0'; cp++)
523c9dba40cSEd Schouten 			cnputc(*cp);
524bf8696b4SAndriy Gapon 		cngrab();
525c9dba40cSEd Schouten 		if (cngetc() == '.')
526c9dba40cSEd Schouten 			console_pausing = 0;
527bf8696b4SAndriy Gapon 		cnungrab();
528c9dba40cSEd Schouten 		cnputc('\r');
529c9dba40cSEd Schouten 		for (cp = console_pausestr; *cp != '\0'; cp++)
530c9dba40cSEd Schouten 			cnputc(' ');
531c9dba40cSEd Schouten 		cnputc('\r');
532c9dba40cSEd Schouten 	}
533c9dba40cSEd Schouten }
534c9dba40cSEd Schouten 
535c9dba40cSEd Schouten void
5366858c2ccSConrad Meyer cnputsn(const char *p, size_t n)
537c9dba40cSEd Schouten {
5386858c2ccSConrad Meyer 	size_t i;
539c9dba40cSEd Schouten 	int unlock_reqd = 0;
540c9dba40cSEd Schouten 
541c9dba40cSEd Schouten 	if (use_cnputs_mtx) {
54204a8159dSHans Petter Selasky 	  	/*
54304a8159dSHans Petter Selasky 		 * NOTE: Debug prints and/or witness printouts in
54404a8159dSHans Petter Selasky 		 * console driver clients can cause the "cnputs_mtx"
54504a8159dSHans Petter Selasky 		 * mutex to recurse. Simply return if that happens.
54604a8159dSHans Petter Selasky 		 */
54704a8159dSHans Petter Selasky 		if (mtx_owned(&cnputs_mtx))
54804a8159dSHans Petter Selasky 			return;
549c9dba40cSEd Schouten 		mtx_lock_spin(&cnputs_mtx);
550c9dba40cSEd Schouten 		unlock_reqd = 1;
551c9dba40cSEd Schouten 	}
552c9dba40cSEd Schouten 
5536858c2ccSConrad Meyer 	for (i = 0; i < n; i++)
5546858c2ccSConrad Meyer 		cnputc(p[i]);
555c9dba40cSEd Schouten 
556c9dba40cSEd Schouten 	if (unlock_reqd)
557c9dba40cSEd Schouten 		mtx_unlock_spin(&cnputs_mtx);
558c9dba40cSEd Schouten }
559c9dba40cSEd Schouten 
5606858c2ccSConrad Meyer void
5616858c2ccSConrad Meyer cnputs(char *p)
5626858c2ccSConrad Meyer {
5636858c2ccSConrad Meyer 	cnputsn(p, strlen(p));
5646858c2ccSConrad Meyer }
5656858c2ccSConrad Meyer 
566c9dba40cSEd Schouten static int consmsgbuf_size = 8192;
567c9dba40cSEd Schouten SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
568a0c87b74SGavin Atkinson     "Console tty buffer size");
569c9dba40cSEd Schouten 
570c9dba40cSEd Schouten /*
571c9dba40cSEd Schouten  * Redirect console output to a tty.
572c9dba40cSEd Schouten  */
573c9dba40cSEd Schouten void
574c9dba40cSEd Schouten constty_set(struct tty *tp)
575c9dba40cSEd Schouten {
576c9dba40cSEd Schouten 	int size;
577c9dba40cSEd Schouten 
578c9dba40cSEd Schouten 	KASSERT(tp != NULL, ("constty_set: NULL tp"));
579c9dba40cSEd Schouten 	if (consbuf == NULL) {
580c9dba40cSEd Schouten 		size = consmsgbuf_size;
581c9dba40cSEd Schouten 		consbuf = malloc(size, M_TTYCONS, M_WAITOK);
582c9dba40cSEd Schouten 		msgbuf_init(&consmsgbuf, consbuf, size);
583c9dba40cSEd Schouten 		callout_init(&conscallout, 0);
584c9dba40cSEd Schouten 	}
585c9dba40cSEd Schouten 	constty = tp;
586c9dba40cSEd Schouten 	constty_timeout(NULL);
587c9dba40cSEd Schouten }
588c9dba40cSEd Schouten 
589c9dba40cSEd Schouten /*
590c9dba40cSEd Schouten  * Disable console redirection to a tty.
591c9dba40cSEd Schouten  */
592c9dba40cSEd Schouten void
593c9dba40cSEd Schouten constty_clear(void)
594c9dba40cSEd Schouten {
595c9dba40cSEd Schouten 	int c;
596c9dba40cSEd Schouten 
597c9dba40cSEd Schouten 	constty = NULL;
598c9dba40cSEd Schouten 	if (consbuf == NULL)
599c9dba40cSEd Schouten 		return;
600c9dba40cSEd Schouten 	callout_stop(&conscallout);
601c9dba40cSEd Schouten 	while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
602c9dba40cSEd Schouten 		cnputc(c);
603c9dba40cSEd Schouten 	free(consbuf, M_TTYCONS);
604c9dba40cSEd Schouten 	consbuf = NULL;
605c9dba40cSEd Schouten }
606c9dba40cSEd Schouten 
607c9dba40cSEd Schouten /* Times per second to check for pending console tty messages. */
608c9dba40cSEd Schouten static int constty_wakeups_per_second = 5;
609c9dba40cSEd Schouten SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
610a0c87b74SGavin Atkinson     &constty_wakeups_per_second, 0,
611a0c87b74SGavin Atkinson     "Times per second to check for pending console tty messages");
612c9dba40cSEd Schouten 
613c9dba40cSEd Schouten static void
614c9dba40cSEd Schouten constty_timeout(void *arg)
615c9dba40cSEd Schouten {
616c9dba40cSEd Schouten 	int c;
617c9dba40cSEd Schouten 
618c9dba40cSEd Schouten 	if (constty != NULL) {
619c9dba40cSEd Schouten 		tty_lock(constty);
620c9dba40cSEd Schouten 		while ((c = msgbuf_getchar(&consmsgbuf)) != -1) {
621c9dba40cSEd Schouten 			if (tty_putchar(constty, c) < 0) {
622c9dba40cSEd Schouten 				tty_unlock(constty);
623c9dba40cSEd Schouten 				constty = NULL;
624c9dba40cSEd Schouten 				break;
625c9dba40cSEd Schouten 			}
626c9dba40cSEd Schouten 		}
627c9dba40cSEd Schouten 
628c9dba40cSEd Schouten 		if (constty != NULL)
629c9dba40cSEd Schouten 			tty_unlock(constty);
630c9dba40cSEd Schouten 	}
631c9dba40cSEd Schouten 	if (constty != NULL) {
632c9dba40cSEd Schouten 		callout_reset(&conscallout, hz / constty_wakeups_per_second,
633c9dba40cSEd Schouten 		    constty_timeout, NULL);
634c9dba40cSEd Schouten 	} else {
635c9dba40cSEd Schouten 		/* Deallocate the constty buffer memory. */
636c9dba40cSEd Schouten 		constty_clear();
637c9dba40cSEd Schouten 	}
638c9dba40cSEd Schouten }
639c9dba40cSEd Schouten 
640c9dba40cSEd Schouten static void
641c9dba40cSEd Schouten cn_drvinit(void *unused)
642c9dba40cSEd Schouten {
643c9dba40cSEd Schouten 
64404a8159dSHans Petter Selasky 	mtx_init(&cnputs_mtx, "cnputs_mtx", NULL, MTX_SPIN | MTX_NOWITNESS);
645c9dba40cSEd Schouten 	use_cnputs_mtx = 1;
646c9dba40cSEd Schouten }
647c9dba40cSEd Schouten 
648c9dba40cSEd Schouten SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL);
649c9dba40cSEd Schouten 
650c9dba40cSEd Schouten /*
651c9dba40cSEd Schouten  * Sysbeep(), if we have hardware for it
652c9dba40cSEd Schouten  */
653c9dba40cSEd Schouten 
654c9dba40cSEd Schouten #ifdef HAS_TIMER_SPKR
655c9dba40cSEd Schouten 
656c9dba40cSEd Schouten static int beeping;
657fadf3fb9SJohn Baldwin static struct callout beeping_timer;
658c9dba40cSEd Schouten 
659c9dba40cSEd Schouten static void
660c9dba40cSEd Schouten sysbeepstop(void *chan)
661c9dba40cSEd Schouten {
662c9dba40cSEd Schouten 
663c9dba40cSEd Schouten 	timer_spkr_release();
664c9dba40cSEd Schouten 	beeping = 0;
665c9dba40cSEd Schouten }
666c9dba40cSEd Schouten 
667c9dba40cSEd Schouten int
668c9dba40cSEd Schouten sysbeep(int pitch, int period)
669c9dba40cSEd Schouten {
670c9dba40cSEd Schouten 
671c9dba40cSEd Schouten 	if (timer_spkr_acquire()) {
672c9dba40cSEd Schouten 		if (!beeping) {
673c9dba40cSEd Schouten 			/* Something else owns it. */
674c9dba40cSEd Schouten 			return (EBUSY);
675c9dba40cSEd Schouten 		}
676c9dba40cSEd Schouten 	}
677c9dba40cSEd Schouten 	timer_spkr_setfreq(pitch);
678c9dba40cSEd Schouten 	if (!beeping) {
679c9dba40cSEd Schouten 		beeping = period;
680fadf3fb9SJohn Baldwin 		callout_reset(&beeping_timer, period, sysbeepstop, NULL);
681c9dba40cSEd Schouten 	}
682c9dba40cSEd Schouten 	return (0);
683c9dba40cSEd Schouten }
684c9dba40cSEd Schouten 
685fadf3fb9SJohn Baldwin static void
686fadf3fb9SJohn Baldwin sysbeep_init(void *unused)
687fadf3fb9SJohn Baldwin {
688fadf3fb9SJohn Baldwin 
689fd90e2edSJung-uk Kim 	callout_init(&beeping_timer, 1);
690fadf3fb9SJohn Baldwin }
691fadf3fb9SJohn Baldwin SYSINIT(sysbeep, SI_SUB_SOFTINTR, SI_ORDER_ANY, sysbeep_init, NULL);
692c9dba40cSEd Schouten #else
693c9dba40cSEd Schouten 
694c9dba40cSEd Schouten /*
695c9dba40cSEd Schouten  * No hardware, no sound
696c9dba40cSEd Schouten  */
697c9dba40cSEd Schouten 
698c9dba40cSEd Schouten int
699c9dba40cSEd Schouten sysbeep(int pitch __unused, int period __unused)
700c9dba40cSEd Schouten {
701c9dba40cSEd Schouten 
702c9dba40cSEd Schouten 	return (ENODEV);
703c9dba40cSEd Schouten }
704c9dba40cSEd Schouten 
705c9dba40cSEd Schouten #endif
706c9dba40cSEd Schouten 
70759644098SEd Maste /*
70859644098SEd Maste  * Temporary support for sc(4) to vt(4) transition.
70959644098SEd Maste  */
710018147eeSEd Maste static unsigned vty_prefer;
711af3b2549SHans Petter Selasky static char vty_name[16];
712af3b2549SHans Petter Selasky SYSCTL_STRING(_kern, OID_AUTO, vty, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, vty_name,
713af3b2549SHans Petter Selasky     0, "Console vty driver");
71459644098SEd Maste 
71559644098SEd Maste int
71659644098SEd Maste vty_enabled(unsigned vty)
71759644098SEd Maste {
71859644098SEd Maste 	static unsigned vty_selected = 0;
71959644098SEd Maste 
72059644098SEd Maste 	if (vty_selected == 0) {
72159644098SEd Maste 		TUNABLE_STR_FETCH("kern.vty", vty_name, sizeof(vty_name));
72259644098SEd Maste 		do {
72359644098SEd Maste #if defined(DEV_SC)
72459644098SEd Maste 			if (strcmp(vty_name, "sc") == 0) {
72559644098SEd Maste 				vty_selected = VTY_SC;
72659644098SEd Maste 				break;
72759644098SEd Maste 			}
72859644098SEd Maste #endif
72959644098SEd Maste #if defined(DEV_VT)
73059644098SEd Maste 			if (strcmp(vty_name, "vt") == 0) {
73159644098SEd Maste 				vty_selected = VTY_VT;
73259644098SEd Maste 				break;
73359644098SEd Maste 			}
73459644098SEd Maste #endif
735018147eeSEd Maste 			if (vty_prefer != 0) {
736018147eeSEd Maste 				vty_selected = vty_prefer;
737018147eeSEd Maste 				break;
738018147eeSEd Maste 			}
7392d6f6d63SJean-Sébastien Pédron #if defined(DEV_VT)
74059644098SEd Maste 			vty_selected = VTY_VT;
7412d6f6d63SJean-Sébastien Pédron #elif defined(DEV_SC)
7422d6f6d63SJean-Sébastien Pédron 			vty_selected = VTY_SC;
74359644098SEd Maste #endif
74459644098SEd Maste 		} while (0);
74559644098SEd Maste 
74659644098SEd Maste 		if (vty_selected == VTY_VT)
74759644098SEd Maste 			strcpy(vty_name, "vt");
74859644098SEd Maste 		else if (vty_selected == VTY_SC)
74959644098SEd Maste 			strcpy(vty_name, "sc");
75059644098SEd Maste 	}
75159644098SEd Maste 	return ((vty_selected & vty) != 0);
75259644098SEd Maste }
75359644098SEd Maste 
754018147eeSEd Maste void
755018147eeSEd Maste vty_set_preferred(unsigned vty)
756018147eeSEd Maste {
757018147eeSEd Maste 
758018147eeSEd Maste 	vty_prefer = vty;
759018147eeSEd Maste #if !defined(DEV_SC)
760969d3cc2SEd Maste 	vty_prefer &= ~VTY_SC;
761018147eeSEd Maste #endif
762018147eeSEd Maste #if !defined(DEV_VT)
763969d3cc2SEd Maste 	vty_prefer &= ~VTY_VT;
764018147eeSEd Maste #endif
765018147eeSEd Maste }
766018147eeSEd Maste 
767