xref: /freebsd/sys/kern/kern_cons.c (revision a68d5a66258e953ef6ccdbdd82e89572a3cc04f9)
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 
40c9dba40cSEd Schouten #include <sys/cdefs.h>
41c9dba40cSEd Schouten #include "opt_ddb.h"
4259644098SEd Maste #include "opt_syscons.h"
43c9dba40cSEd Schouten 
44c9dba40cSEd Schouten #include <sys/param.h>
45c9dba40cSEd Schouten #include <sys/systm.h>
46c9dba40cSEd Schouten #include <sys/lock.h>
47c9dba40cSEd Schouten #include <sys/mutex.h>
48c9dba40cSEd Schouten #include <sys/conf.h>
49c9dba40cSEd Schouten #include <sys/cons.h>
50c9dba40cSEd Schouten #include <sys/fcntl.h>
513ed7166aSKyle Evans #include <sys/kbio.h>
52c9dba40cSEd Schouten #include <sys/kdb.h>
53c9dba40cSEd Schouten #include <sys/kernel.h>
54c9dba40cSEd Schouten #include <sys/malloc.h>
55c9dba40cSEd Schouten #include <sys/msgbuf.h>
56c9dba40cSEd Schouten #include <sys/namei.h>
57c9dba40cSEd Schouten #include <sys/priv.h>
58c9dba40cSEd Schouten #include <sys/proc.h>
59c9dba40cSEd Schouten #include <sys/queue.h>
60c9dba40cSEd Schouten #include <sys/reboot.h>
61c9dba40cSEd Schouten #include <sys/sysctl.h>
62c9dba40cSEd Schouten #include <sys/sbuf.h>
639d6ae1e3SColin Percival #include <sys/tslog.h>
64c9dba40cSEd Schouten #include <sys/tty.h>
65c9dba40cSEd Schouten #include <sys/uio.h>
66c9dba40cSEd Schouten #include <sys/vnode.h>
67c9dba40cSEd Schouten 
68c9dba40cSEd Schouten #include <ddb/ddb.h>
69c9dba40cSEd Schouten 
703ed7166aSKyle Evans #include <dev/kbd/kbdreg.h>
713ed7166aSKyle Evans 
72c9dba40cSEd Schouten #include <machine/cpu.h>
73c9dba40cSEd Schouten #include <machine/clock.h>
74c9dba40cSEd Schouten 
75c9dba40cSEd Schouten static MALLOC_DEFINE(M_TTYCONS, "tty console", "tty console handling");
76c9dba40cSEd Schouten 
77c9dba40cSEd Schouten struct cn_device {
78c9dba40cSEd Schouten 	STAILQ_ENTRY(cn_device) cnd_next;
79c9dba40cSEd Schouten 	struct		consdev *cnd_cn;
80c9dba40cSEd Schouten };
81c9dba40cSEd Schouten 
82c9dba40cSEd Schouten #define CNDEVPATHMAX	32
83c9dba40cSEd Schouten #define CNDEVTAB_SIZE	4
84c9dba40cSEd Schouten static struct cn_device cn_devtab[CNDEVTAB_SIZE];
85c9dba40cSEd Schouten static STAILQ_HEAD(, cn_device) cn_devlist =
86c9dba40cSEd Schouten     STAILQ_HEAD_INITIALIZER(cn_devlist);
87c9dba40cSEd Schouten 
88c9dba40cSEd Schouten int	cons_avail_mask = 0;	/* Bit mask. Each registered low level console
89c9dba40cSEd Schouten 				 * which is currently unavailable for inpit
90c9dba40cSEd Schouten 				 * (i.e., if it is in graphics mode) will have
91c9dba40cSEd Schouten 				 * this bit cleared.
92c9dba40cSEd Schouten 				 */
93780766ebSMark Johnston 
94c9dba40cSEd Schouten static int cn_mute;
95780766ebSMark Johnston SYSCTL_INT(_kern, OID_AUTO, consmute, CTLFLAG_RW, &cn_mute, 0,
96780766ebSMark Johnston     "State of the console muting");
97780766ebSMark Johnston 
98c9dba40cSEd Schouten static char *consbuf;			/* buffer used by `consmsgbuf' */
99c9dba40cSEd Schouten static struct callout conscallout;	/* callout for outputting to constty */
100c9dba40cSEd Schouten struct msgbuf consmsgbuf;		/* message buffer for console tty */
1012bfdc1eeSWarner Losh static bool console_pausing;		/* pause after each line during probe */
102fe20aaecSRyan Libby static const char console_pausestr[] =
103c9dba40cSEd Schouten "<pause; press any key to proceed to next line or '.' to end pause mode>";
104c9dba40cSEd Schouten struct tty *constty;			/* pointer to console "window" tty */
105bd6085c6SAlexander Motin static struct mtx constty_mtx;		/* Mutex for constty assignment. */
106bd6085c6SAlexander Motin MTX_SYSINIT(constty_mtx, &constty_mtx, "constty_mtx", MTX_DEF);
107c9dba40cSEd Schouten static struct mtx cnputs_mtx;		/* Mutex for cnputs(). */
108bd6085c6SAlexander Motin MTX_SYSINIT(cnputs_mtx, &cnputs_mtx, "cnputs_mtx", MTX_SPIN | MTX_NOWITNESS);
109c9dba40cSEd Schouten 
110c9dba40cSEd Schouten static void constty_timeout(void *arg);
111c9dba40cSEd Schouten 
112c9dba40cSEd Schouten static struct consdev cons_consdev;
113c9dba40cSEd Schouten DATA_SET(cons_set, cons_consdev);
114c9dba40cSEd Schouten SET_DECLARE(cons_set, struct consdev);
115c9dba40cSEd Schouten 
116f46412c0SKyle Evans /*
117f46412c0SKyle Evans  * Stub for configurations that don't actually have a keyboard driver. Inclusion
118f46412c0SKyle Evans  * of kbd.c is contingent on any number of keyboard/console drivers being
119f46412c0SKyle Evans  * present in the kernel; rather than trying to catch them all, we'll just
120f46412c0SKyle Evans  * maintain this weak kbdinit that will be overridden by the strong version in
121f46412c0SKyle Evans  * kbd.c if it's present.
122f46412c0SKyle Evans  */
123f46412c0SKyle Evans __weak_symbol void
124f46412c0SKyle Evans kbdinit(void)
125f46412c0SKyle Evans {
126f46412c0SKyle Evans 
127f46412c0SKyle Evans }
128f46412c0SKyle Evans 
129c9dba40cSEd Schouten void
130c9dba40cSEd Schouten cninit(void)
131c9dba40cSEd Schouten {
132c9dba40cSEd Schouten 	struct consdev *best_cn, *cn, **list;
133c9dba40cSEd Schouten 
1349d6ae1e3SColin Percival 	TSENTER();
135c9dba40cSEd Schouten 	/*
136c9dba40cSEd Schouten 	 * Check if we should mute the console (for security reasons perhaps)
137c9dba40cSEd Schouten 	 * It can be changes dynamically using sysctl kern.consmute
138c9dba40cSEd Schouten 	 * once we are up and going.
139c9dba40cSEd Schouten 	 *
140c9dba40cSEd Schouten 	 */
141c9dba40cSEd Schouten         cn_mute = ((boothowto & (RB_MUTE
142c9dba40cSEd Schouten 			|RB_SINGLE
143c9dba40cSEd Schouten 			|RB_VERBOSE
144c9dba40cSEd Schouten 			|RB_ASKNAME)) == RB_MUTE);
145c9dba40cSEd Schouten 
146c9dba40cSEd Schouten 	/*
1473ed7166aSKyle Evans 	 * Bring up the kbd layer just in time for cnprobe.  Console drivers
1483ed7166aSKyle Evans 	 * have a dependency on kbd being ready, so this fits nicely between the
1493ed7166aSKyle Evans 	 * machdep callers of cninit() and MI probing/initialization of consoles
1503ed7166aSKyle Evans 	 * here.
1513ed7166aSKyle Evans 	 */
1523ed7166aSKyle Evans 	kbdinit();
1533ed7166aSKyle Evans 
1543ed7166aSKyle Evans 	/*
155c9dba40cSEd Schouten 	 * Find the first console with the highest priority.
156c9dba40cSEd Schouten 	 */
157c9dba40cSEd Schouten 	best_cn = NULL;
158c9dba40cSEd Schouten 	SET_FOREACH(list, cons_set) {
159c9dba40cSEd Schouten 		cn = *list;
160c9dba40cSEd Schouten 		cnremove(cn);
1612992abe0SEd Schouten 		/* Skip cons_consdev. */
1622992abe0SEd Schouten 		if (cn->cn_ops == NULL)
163c9dba40cSEd Schouten 			continue;
1642992abe0SEd Schouten 		cn->cn_ops->cn_probe(cn);
165c9dba40cSEd Schouten 		if (cn->cn_pri == CN_DEAD)
166c9dba40cSEd Schouten 			continue;
167c9dba40cSEd Schouten 		if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
168c9dba40cSEd Schouten 			best_cn = cn;
169c9dba40cSEd Schouten 		if (boothowto & RB_MULTIPLE) {
170c9dba40cSEd Schouten 			/*
171c9dba40cSEd Schouten 			 * Initialize console, and attach to it.
172c9dba40cSEd Schouten 			 */
1732992abe0SEd Schouten 			cn->cn_ops->cn_init(cn);
174c9dba40cSEd Schouten 			cnadd(cn);
175c9dba40cSEd Schouten 		}
176c9dba40cSEd Schouten 	}
177c9dba40cSEd Schouten 	if (best_cn == NULL)
178c9dba40cSEd Schouten 		return;
179c9dba40cSEd Schouten 	if ((boothowto & RB_MULTIPLE) == 0) {
1802992abe0SEd Schouten 		best_cn->cn_ops->cn_init(best_cn);
181c9dba40cSEd Schouten 		cnadd(best_cn);
182c9dba40cSEd Schouten 	}
183c9dba40cSEd Schouten 	if (boothowto & RB_PAUSE)
1842bfdc1eeSWarner Losh 		console_pausing = true;
185c9dba40cSEd Schouten 	/*
186c9dba40cSEd Schouten 	 * Make the best console the preferred console.
187c9dba40cSEd Schouten 	 */
188c9dba40cSEd Schouten 	cnselect(best_cn);
189dc61566fSZbigniew Bodek 
190dc61566fSZbigniew Bodek #ifdef EARLY_PRINTF
191dc61566fSZbigniew Bodek 	/*
192dc61566fSZbigniew Bodek 	 * Release early console.
193dc61566fSZbigniew Bodek 	 */
194dc61566fSZbigniew Bodek 	early_putc = NULL;
195dc61566fSZbigniew Bodek #endif
1969d6ae1e3SColin Percival 	TSEXIT();
197c9dba40cSEd Schouten }
198c9dba40cSEd Schouten 
199c9dba40cSEd Schouten void
2009806e82aSDimitry Andric cninit_finish(void)
201c9dba40cSEd Schouten {
2022bfdc1eeSWarner Losh 	console_pausing = false;
203c9dba40cSEd Schouten }
204c9dba40cSEd Schouten 
205c9dba40cSEd Schouten /* add a new physical console to back the virtual console */
206c9dba40cSEd Schouten int
207c9dba40cSEd Schouten cnadd(struct consdev *cn)
208c9dba40cSEd Schouten {
209c9dba40cSEd Schouten 	struct cn_device *cnd;
210c9dba40cSEd Schouten 	int i;
211c9dba40cSEd Schouten 
212c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
213c9dba40cSEd Schouten 		if (cnd->cnd_cn == cn)
214c9dba40cSEd Schouten 			return (0);
215c9dba40cSEd Schouten 	for (i = 0; i < CNDEVTAB_SIZE; i++) {
216c9dba40cSEd Schouten 		cnd = &cn_devtab[i];
217c9dba40cSEd Schouten 		if (cnd->cnd_cn == NULL)
218c9dba40cSEd Schouten 			break;
219c9dba40cSEd Schouten 	}
220c9dba40cSEd Schouten 	if (cnd->cnd_cn != NULL)
221c9dba40cSEd Schouten 		return (ENOMEM);
222c9dba40cSEd Schouten 	cnd->cnd_cn = cn;
223c9dba40cSEd Schouten 	if (cn->cn_name[0] == '\0') {
224c9dba40cSEd Schouten 		/* XXX: it is unclear if/where this print might output */
225c9dba40cSEd Schouten 		printf("WARNING: console at %p has no name\n", cn);
226c9dba40cSEd Schouten 	}
227c9dba40cSEd Schouten 	STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
228c9dba40cSEd Schouten 	if (STAILQ_FIRST(&cn_devlist) == cnd)
229c9dba40cSEd Schouten 		ttyconsdev_select(cnd->cnd_cn->cn_name);
230c9dba40cSEd Schouten 
231c9dba40cSEd Schouten 	/* Add device to the active mask. */
232c9dba40cSEd Schouten 	cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
233c9dba40cSEd Schouten 
234c9dba40cSEd Schouten 	return (0);
235c9dba40cSEd Schouten }
236c9dba40cSEd Schouten 
237c9dba40cSEd Schouten void
238c9dba40cSEd Schouten cnremove(struct consdev *cn)
239c9dba40cSEd Schouten {
240c9dba40cSEd Schouten 	struct cn_device *cnd;
241c9dba40cSEd Schouten 	int i;
242c9dba40cSEd Schouten 
243c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
244*a68d5a66SKyle Evans 		if (cnd->cnd_cn != cn)
245c9dba40cSEd Schouten 			continue;
246*a68d5a66SKyle Evans 		if (STAILQ_FIRST(&cn_devlist) == cnd)
247*a68d5a66SKyle Evans 			ttyconsdev_select(NULL);
248c9dba40cSEd Schouten 		STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
249c9dba40cSEd Schouten 		cnd->cnd_cn = NULL;
250c9dba40cSEd Schouten 
251c9dba40cSEd Schouten 		/* Remove this device from available mask. */
252c9dba40cSEd Schouten 		for (i = 0; i < CNDEVTAB_SIZE; i++)
253c9dba40cSEd Schouten 			if (cnd == &cn_devtab[i]) {
254c9dba40cSEd Schouten 				cons_avail_mask &= ~(1 << i);
255c9dba40cSEd Schouten 				break;
256c9dba40cSEd Schouten 			}
257c9dba40cSEd Schouten #if 0
258c9dba40cSEd Schouten 		/*
259c9dba40cSEd Schouten 		 * XXX
260c9dba40cSEd Schouten 		 * syscons gets really confused if console resources are
261c9dba40cSEd Schouten 		 * freed after the system has initialized.
262c9dba40cSEd Schouten 		 */
263c9dba40cSEd Schouten 		if (cn->cn_term != NULL)
2642992abe0SEd Schouten 			cn->cn_ops->cn_term(cn);
265c9dba40cSEd Schouten #endif
266c9dba40cSEd Schouten 		return;
267c9dba40cSEd Schouten 	}
268c9dba40cSEd Schouten }
269c9dba40cSEd Schouten 
270c9dba40cSEd Schouten void
271c9dba40cSEd Schouten cnselect(struct consdev *cn)
272c9dba40cSEd Schouten {
273c9dba40cSEd Schouten 	struct cn_device *cnd;
274c9dba40cSEd Schouten 
275c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
276c9dba40cSEd Schouten 		if (cnd->cnd_cn != cn)
277c9dba40cSEd Schouten 			continue;
278c9dba40cSEd Schouten 		if (cnd == STAILQ_FIRST(&cn_devlist))
279c9dba40cSEd Schouten 			return;
280c9dba40cSEd Schouten 		STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
281c9dba40cSEd Schouten 		STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
282c9dba40cSEd Schouten 		ttyconsdev_select(cnd->cnd_cn->cn_name);
283c9dba40cSEd Schouten 		return;
284c9dba40cSEd Schouten 	}
285c9dba40cSEd Schouten }
286c9dba40cSEd Schouten 
287c9dba40cSEd Schouten void
288c9dba40cSEd Schouten cnavailable(struct consdev *cn, int available)
289c9dba40cSEd Schouten {
290c9dba40cSEd Schouten 	int i;
291c9dba40cSEd Schouten 
292c9dba40cSEd Schouten 	for (i = 0; i < CNDEVTAB_SIZE; i++) {
293c9dba40cSEd Schouten 		if (cn_devtab[i].cnd_cn == cn)
294c9dba40cSEd Schouten 			break;
295c9dba40cSEd Schouten 	}
296c9dba40cSEd Schouten 	if (available) {
297c9dba40cSEd Schouten 		if (i < CNDEVTAB_SIZE)
298c9dba40cSEd Schouten 			cons_avail_mask |= (1 << i);
299c9dba40cSEd Schouten 		cn->cn_flags &= ~CN_FLAG_NOAVAIL;
300c9dba40cSEd Schouten 	} else {
301c9dba40cSEd Schouten 		if (i < CNDEVTAB_SIZE)
302c9dba40cSEd Schouten 			cons_avail_mask &= ~(1 << i);
303c9dba40cSEd Schouten 		cn->cn_flags |= CN_FLAG_NOAVAIL;
304c9dba40cSEd Schouten 	}
305c9dba40cSEd Schouten }
306c9dba40cSEd Schouten 
307c9dba40cSEd Schouten int
308c9dba40cSEd Schouten cnunavailable(void)
309c9dba40cSEd Schouten {
310c9dba40cSEd Schouten 
311c9dba40cSEd Schouten 	return (cons_avail_mask == 0);
312c9dba40cSEd Schouten }
313c9dba40cSEd Schouten 
314c9dba40cSEd Schouten /*
315c9dba40cSEd Schouten  * sysctl_kern_console() provides output parseable in conscontrol(1).
316c9dba40cSEd Schouten  */
317c9dba40cSEd Schouten static int
318c9dba40cSEd Schouten sysctl_kern_console(SYSCTL_HANDLER_ARGS)
319c9dba40cSEd Schouten {
320c9dba40cSEd Schouten 	struct cn_device *cnd;
321c9dba40cSEd Schouten 	struct consdev *cp, **list;
322c9dba40cSEd Schouten 	char *p;
3232bfdc1eeSWarner Losh 	bool delete;
3242bfdc1eeSWarner Losh 	int error;
325c9dba40cSEd Schouten 	struct sbuf *sb;
326c9dba40cSEd Schouten 
327657282e0SIan Lepore 	sb = sbuf_new(NULL, NULL, CNDEVPATHMAX * 2, SBUF_AUTOEXTEND |
328657282e0SIan Lepore 	    SBUF_INCLUDENUL);
329c9dba40cSEd Schouten 	if (sb == NULL)
330c9dba40cSEd Schouten 		return (ENOMEM);
331c9dba40cSEd Schouten 	sbuf_clear(sb);
332c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
333c9dba40cSEd Schouten 		sbuf_printf(sb, "%s,", cnd->cnd_cn->cn_name);
3340a713948SAlexander Motin 	sbuf_putc(sb, '/');
335c9dba40cSEd Schouten 	SET_FOREACH(list, cons_set) {
336c9dba40cSEd Schouten 		cp = *list;
337c9dba40cSEd Schouten 		if (cp->cn_name[0] != '\0')
338c9dba40cSEd Schouten 			sbuf_printf(sb, "%s,", cp->cn_name);
339c9dba40cSEd Schouten 	}
340c9dba40cSEd Schouten 	sbuf_finish(sb);
341c9dba40cSEd Schouten 	error = sysctl_handle_string(oidp, sbuf_data(sb), sbuf_len(sb), req);
342c9dba40cSEd Schouten 	if (error == 0 && req->newptr != NULL) {
343c9dba40cSEd Schouten 		p = sbuf_data(sb);
344c9dba40cSEd Schouten 		error = ENXIO;
3452bfdc1eeSWarner Losh 		delete = false;
346c9dba40cSEd Schouten 		if (*p == '-') {
3472bfdc1eeSWarner Losh 			delete = true;
348c9dba40cSEd Schouten 			p++;
349c9dba40cSEd Schouten 		}
350c9dba40cSEd Schouten 		SET_FOREACH(list, cons_set) {
351c9dba40cSEd Schouten 			cp = *list;
352c9dba40cSEd Schouten 			if (strcmp(p, cp->cn_name) != 0)
353c9dba40cSEd Schouten 				continue;
354c9dba40cSEd Schouten 			if (delete) {
355c9dba40cSEd Schouten 				cnremove(cp);
356c9dba40cSEd Schouten 				error = 0;
357c9dba40cSEd Schouten 			} else {
358c9dba40cSEd Schouten 				error = cnadd(cp);
359*a68d5a66SKyle Evans 				if (error == 0)
360*a68d5a66SKyle Evans 					cnselect(cp);
361c9dba40cSEd Schouten 			}
362c9dba40cSEd Schouten 			break;
363c9dba40cSEd Schouten 		}
364c9dba40cSEd Schouten 	}
365c9dba40cSEd Schouten 	sbuf_delete(sb);
366c9dba40cSEd Schouten 	return (error);
367c9dba40cSEd Schouten }
368c9dba40cSEd Schouten 
3697029da5cSPawel Biernacki SYSCTL_PROC(_kern, OID_AUTO, console,
3707029da5cSPawel Biernacki     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0,
3717029da5cSPawel Biernacki     sysctl_kern_console, "A",
3727029da5cSPawel Biernacki     "Console device control");
373c9dba40cSEd Schouten 
3749976156fSAndriy Gapon void
3759806e82aSDimitry Andric cngrab(void)
3769976156fSAndriy Gapon {
3779976156fSAndriy Gapon 	struct cn_device *cnd;
3789976156fSAndriy Gapon 	struct consdev *cn;
3799976156fSAndriy Gapon 
3809976156fSAndriy Gapon 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
3819976156fSAndriy Gapon 		cn = cnd->cnd_cn;
3829976156fSAndriy Gapon 		if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
3839976156fSAndriy Gapon 			cn->cn_ops->cn_grab(cn);
3849976156fSAndriy Gapon 	}
3859976156fSAndriy Gapon }
3869976156fSAndriy Gapon 
3879976156fSAndriy Gapon void
3889806e82aSDimitry Andric cnungrab(void)
3899976156fSAndriy Gapon {
3909976156fSAndriy Gapon 	struct cn_device *cnd;
3919976156fSAndriy Gapon 	struct consdev *cn;
3929976156fSAndriy Gapon 
3939976156fSAndriy Gapon 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
3949976156fSAndriy Gapon 		cn = cnd->cnd_cn;
3959976156fSAndriy Gapon 		if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
3969976156fSAndriy Gapon 			cn->cn_ops->cn_ungrab(cn);
3979976156fSAndriy Gapon 	}
3989976156fSAndriy Gapon }
3999976156fSAndriy Gapon 
400ec6faf94SAndriy Gapon void
4019806e82aSDimitry Andric cnresume(void)
402ec6faf94SAndriy Gapon {
403ec6faf94SAndriy Gapon 	struct cn_device *cnd;
404ec6faf94SAndriy Gapon 	struct consdev *cn;
405ec6faf94SAndriy Gapon 
406ec6faf94SAndriy Gapon 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
407ec6faf94SAndriy Gapon 		cn = cnd->cnd_cn;
408ec6faf94SAndriy Gapon 		if (cn->cn_ops->cn_resume != NULL)
409ec6faf94SAndriy Gapon 			cn->cn_ops->cn_resume(cn);
410ec6faf94SAndriy Gapon 	}
411ec6faf94SAndriy Gapon }
412ec6faf94SAndriy Gapon 
413c9dba40cSEd Schouten /*
414c9dba40cSEd Schouten  * Low level console routines.
415c9dba40cSEd Schouten  */
416c9dba40cSEd Schouten int
417c9dba40cSEd Schouten cngetc(void)
418c9dba40cSEd Schouten {
419c9dba40cSEd Schouten 	int c;
420c9dba40cSEd Schouten 
421c9dba40cSEd Schouten 	if (cn_mute)
422c9dba40cSEd Schouten 		return (-1);
423c9dba40cSEd Schouten 	while ((c = cncheckc()) == -1)
424298fbd16SAndriy Gapon 		cpu_spinwait();
425c9dba40cSEd Schouten 	if (c == '\r')
426c9dba40cSEd Schouten 		c = '\n';		/* console input is always ICRNL */
427c9dba40cSEd Schouten 	return (c);
428c9dba40cSEd Schouten }
429c9dba40cSEd Schouten 
430c9dba40cSEd Schouten int
431c9dba40cSEd Schouten cncheckc(void)
432c9dba40cSEd Schouten {
433c9dba40cSEd Schouten 	struct cn_device *cnd;
434c9dba40cSEd Schouten 	struct consdev *cn;
435c9dba40cSEd Schouten 	int c;
436c9dba40cSEd Schouten 
437c9dba40cSEd Schouten 	if (cn_mute)
438c9dba40cSEd Schouten 		return (-1);
439c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
440c9dba40cSEd Schouten 		cn = cnd->cnd_cn;
441c9dba40cSEd Schouten 		if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
4422992abe0SEd Schouten 			c = cn->cn_ops->cn_getc(cn);
4432992abe0SEd Schouten 			if (c != -1)
444c9dba40cSEd Schouten 				return (c);
445c9dba40cSEd Schouten 		}
446c9dba40cSEd Schouten 	}
447c9dba40cSEd Schouten 	return (-1);
448c9dba40cSEd Schouten }
449c9dba40cSEd Schouten 
450c9dba40cSEd Schouten void
4518e628542SAndriy Gapon cngets(char *cp, size_t size, int visible)
4528e628542SAndriy Gapon {
4538e628542SAndriy Gapon 	char *lp, *end;
4548e628542SAndriy Gapon 	int c;
4558e628542SAndriy Gapon 
4568e628542SAndriy Gapon 	cngrab();
4578e628542SAndriy Gapon 
4588e628542SAndriy Gapon 	lp = cp;
4598e628542SAndriy Gapon 	end = cp + size - 1;
4608e628542SAndriy Gapon 	for (;;) {
4618e628542SAndriy Gapon 		c = cngetc() & 0177;
4628e628542SAndriy Gapon 		switch (c) {
4638e628542SAndriy Gapon 		case '\n':
4648e628542SAndriy Gapon 		case '\r':
4658e628542SAndriy Gapon 			cnputc(c);
4668e628542SAndriy Gapon 			*lp = '\0';
4678e628542SAndriy Gapon 			cnungrab();
4688e628542SAndriy Gapon 			return;
4698e628542SAndriy Gapon 		case '\b':
4708e628542SAndriy Gapon 		case '\177':
4718e628542SAndriy Gapon 			if (lp > cp) {
4729520f952SWarner Losh 				if (visible)
4739520f952SWarner Losh 					cnputs("\b \b");
4748e628542SAndriy Gapon 				lp--;
4758e628542SAndriy Gapon 			}
4768e628542SAndriy Gapon 			continue;
4778e628542SAndriy Gapon 		case '\0':
4788e628542SAndriy Gapon 			continue;
4798e628542SAndriy Gapon 		default:
4808e628542SAndriy Gapon 			if (lp < end) {
4818e628542SAndriy Gapon 				switch (visible) {
4828e628542SAndriy Gapon 				case GETS_NOECHO:
4838e628542SAndriy Gapon 					break;
4848e628542SAndriy Gapon 				case GETS_ECHOPASS:
4858e628542SAndriy Gapon 					cnputc('*');
4868e628542SAndriy Gapon 					break;
4878e628542SAndriy Gapon 				default:
4888e628542SAndriy Gapon 					cnputc(c);
4898e628542SAndriy Gapon 					break;
4908e628542SAndriy Gapon 				}
4918e628542SAndriy Gapon 				*lp++ = c;
4928e628542SAndriy Gapon 			}
4938e628542SAndriy Gapon 		}
4948e628542SAndriy Gapon 	}
4958e628542SAndriy Gapon }
4968e628542SAndriy Gapon 
4978e628542SAndriy Gapon void
498c9dba40cSEd Schouten cnputc(int c)
499c9dba40cSEd Schouten {
500c9dba40cSEd Schouten 	struct cn_device *cnd;
501c9dba40cSEd Schouten 	struct consdev *cn;
502fe20aaecSRyan Libby 	const char *cp;
503c9dba40cSEd Schouten 
50442c8459bSIan Lepore #ifdef EARLY_PRINTF
50542c8459bSIan Lepore 	if (early_putc != NULL) {
50642c8459bSIan Lepore 		if (c == '\n')
50742c8459bSIan Lepore 			early_putc('\r');
50842c8459bSIan Lepore 		early_putc(c);
50942c8459bSIan Lepore 		return;
51042c8459bSIan Lepore 	}
51142c8459bSIan Lepore #endif
51242c8459bSIan Lepore 
513c9dba40cSEd Schouten 	if (cn_mute || c == '\0')
514c9dba40cSEd Schouten 		return;
515c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
516c9dba40cSEd Schouten 		cn = cnd->cnd_cn;
517c9dba40cSEd Schouten 		if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
518c9dba40cSEd Schouten 			if (c == '\n')
5192992abe0SEd Schouten 				cn->cn_ops->cn_putc(cn, '\r');
5202992abe0SEd Schouten 			cn->cn_ops->cn_putc(cn, c);
521c9dba40cSEd Schouten 		}
522c9dba40cSEd Schouten 	}
523c9dba40cSEd Schouten 	if (console_pausing && c == '\n' && !kdb_active) {
524c9dba40cSEd Schouten 		for (cp = console_pausestr; *cp != '\0'; cp++)
525c9dba40cSEd Schouten 			cnputc(*cp);
526bf8696b4SAndriy Gapon 		cngrab();
527c9dba40cSEd Schouten 		if (cngetc() == '.')
5282bfdc1eeSWarner Losh 			console_pausing = false;
529bf8696b4SAndriy Gapon 		cnungrab();
530c9dba40cSEd Schouten 		cnputc('\r');
531c9dba40cSEd Schouten 		for (cp = console_pausestr; *cp != '\0'; cp++)
532c9dba40cSEd Schouten 			cnputc(' ');
533c9dba40cSEd Schouten 		cnputc('\r');
534c9dba40cSEd Schouten 	}
535c9dba40cSEd Schouten }
536c9dba40cSEd Schouten 
537c9dba40cSEd Schouten void
5386858c2ccSConrad Meyer cnputsn(const char *p, size_t n)
539c9dba40cSEd Schouten {
5406858c2ccSConrad Meyer 	size_t i;
5412bfdc1eeSWarner Losh 	bool unlock_reqd = false;
542c9dba40cSEd Schouten 
543bd6085c6SAlexander Motin 	if (mtx_initialized(&cnputs_mtx)) {
54404a8159dSHans Petter Selasky 		/*
54504a8159dSHans Petter Selasky 		 * NOTE: Debug prints and/or witness printouts in
54604a8159dSHans Petter Selasky 		 * console driver clients can cause the "cnputs_mtx"
54704a8159dSHans Petter Selasky 		 * mutex to recurse. Simply return if that happens.
54804a8159dSHans Petter Selasky 		 */
54904a8159dSHans Petter Selasky 		if (mtx_owned(&cnputs_mtx))
55004a8159dSHans Petter Selasky 			return;
551c9dba40cSEd Schouten 		mtx_lock_spin(&cnputs_mtx);
5522bfdc1eeSWarner Losh 		unlock_reqd = true;
553c9dba40cSEd Schouten 	}
554c9dba40cSEd Schouten 
5556858c2ccSConrad Meyer 	for (i = 0; i < n; i++)
5566858c2ccSConrad Meyer 		cnputc(p[i]);
557c9dba40cSEd Schouten 
558c9dba40cSEd Schouten 	if (unlock_reqd)
559c9dba40cSEd Schouten 		mtx_unlock_spin(&cnputs_mtx);
560c9dba40cSEd Schouten }
561c9dba40cSEd Schouten 
5626858c2ccSConrad Meyer void
563fe20aaecSRyan Libby cnputs(const char *p)
5646858c2ccSConrad Meyer {
5656858c2ccSConrad Meyer 	cnputsn(p, strlen(p));
5666858c2ccSConrad Meyer }
5676858c2ccSConrad Meyer 
568bd6085c6SAlexander Motin static unsigned int consmsgbuf_size = 65536;
569bd6085c6SAlexander Motin SYSCTL_UINT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RWTUN, &consmsgbuf_size,
570bd6085c6SAlexander Motin     0, "Console tty buffer size");
571c9dba40cSEd Schouten 
572c9dba40cSEd Schouten /*
573c9dba40cSEd Schouten  * Redirect console output to a tty.
574c9dba40cSEd Schouten  */
575bd6085c6SAlexander Motin int
576c9dba40cSEd Schouten constty_set(struct tty *tp)
577c9dba40cSEd Schouten {
578bd6085c6SAlexander Motin 	int size = consmsgbuf_size;
579bd6085c6SAlexander Motin 	void *buf = NULL;
580c9dba40cSEd Schouten 
581bd6085c6SAlexander Motin 	tty_assert_locked(tp);
582bd6085c6SAlexander Motin 	if (constty == tp)
583bd6085c6SAlexander Motin 		return (0);
584bd6085c6SAlexander Motin 	if (constty != NULL)
585bd6085c6SAlexander Motin 		return (EBUSY);
586bd6085c6SAlexander Motin 
587c9dba40cSEd Schouten 	if (consbuf == NULL) {
588bd6085c6SAlexander Motin 		tty_unlock(tp);
589bd6085c6SAlexander Motin 		buf = malloc(size, M_TTYCONS, M_WAITOK);
590bd6085c6SAlexander Motin 		tty_lock(tp);
591c9dba40cSEd Schouten 	}
592bd6085c6SAlexander Motin 	mtx_lock(&constty_mtx);
593bd6085c6SAlexander Motin 	if (constty != NULL) {
594bd6085c6SAlexander Motin 		mtx_unlock(&constty_mtx);
595bd6085c6SAlexander Motin 		free(buf, M_TTYCONS);
596bd6085c6SAlexander Motin 		return (EBUSY);
597bd6085c6SAlexander Motin 	}
598bd6085c6SAlexander Motin 	if (consbuf == NULL) {
599bd6085c6SAlexander Motin 		consbuf = buf;
600bd6085c6SAlexander Motin 		msgbuf_init(&consmsgbuf, buf, size);
601bd6085c6SAlexander Motin 	} else
602bd6085c6SAlexander Motin 		free(buf, M_TTYCONS);
603c9dba40cSEd Schouten 	constty = tp;
604bd6085c6SAlexander Motin 	mtx_unlock(&constty_mtx);
605bd6085c6SAlexander Motin 
606bd6085c6SAlexander Motin 	callout_init_mtx(&conscallout, tty_getlock(tp), 0);
607bd6085c6SAlexander Motin 	constty_timeout(tp);
608bd6085c6SAlexander Motin 	return (0);
609c9dba40cSEd Schouten }
610c9dba40cSEd Schouten 
611c9dba40cSEd Schouten /*
612c9dba40cSEd Schouten  * Disable console redirection to a tty.
613c9dba40cSEd Schouten  */
614bd6085c6SAlexander Motin int
615bd6085c6SAlexander Motin constty_clear(struct tty *tp)
616c9dba40cSEd Schouten {
617c9dba40cSEd Schouten 	int c;
618c9dba40cSEd Schouten 
619bd6085c6SAlexander Motin 	tty_assert_locked(tp);
620bd6085c6SAlexander Motin 	if (constty != tp)
621bd6085c6SAlexander Motin 		return (ENXIO);
622c9dba40cSEd Schouten 	callout_stop(&conscallout);
623bd6085c6SAlexander Motin 	mtx_lock(&constty_mtx);
624bd6085c6SAlexander Motin 	constty = NULL;
625bd6085c6SAlexander Motin 	mtx_unlock(&constty_mtx);
626c9dba40cSEd Schouten 	while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
627c9dba40cSEd Schouten 		cnputc(c);
628bd6085c6SAlexander Motin 	/* We never free consbuf because it can still be in use. */
629bd6085c6SAlexander Motin 	return (0);
630c9dba40cSEd Schouten }
631c9dba40cSEd Schouten 
632c9dba40cSEd Schouten /* Times per second to check for pending console tty messages. */
633bd6085c6SAlexander Motin static int constty_wakeups_per_second = 15;
634c9dba40cSEd Schouten SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
635a0c87b74SGavin Atkinson     &constty_wakeups_per_second, 0,
636a0c87b74SGavin Atkinson     "Times per second to check for pending console tty messages");
637c9dba40cSEd Schouten 
638c9dba40cSEd Schouten static void
639c9dba40cSEd Schouten constty_timeout(void *arg)
640c9dba40cSEd Schouten {
641bd6085c6SAlexander Motin 	struct tty *tp = arg;
642c9dba40cSEd Schouten 	int c;
643c9dba40cSEd Schouten 
644bd6085c6SAlexander Motin 	tty_assert_locked(tp);
645c9dba40cSEd Schouten 	while ((c = msgbuf_getchar(&consmsgbuf)) != -1) {
646bd6085c6SAlexander Motin 		if (tty_putchar(tp, c) < 0) {
647bd6085c6SAlexander Motin 			constty_clear(tp);
648bd6085c6SAlexander Motin 			return;
649c9dba40cSEd Schouten 		}
650c9dba40cSEd Schouten 	}
651bd6085c6SAlexander Motin 	callout_reset_sbt(&conscallout, SBT_1S / constty_wakeups_per_second,
652bd6085c6SAlexander Motin 	    0, constty_timeout, tp, C_PREL(1));
653c9dba40cSEd Schouten }
654c9dba40cSEd Schouten 
655c9dba40cSEd Schouten /*
656c9dba40cSEd Schouten  * Sysbeep(), if we have hardware for it
657c9dba40cSEd Schouten  */
658c9dba40cSEd Schouten 
659c9dba40cSEd Schouten #ifdef HAS_TIMER_SPKR
660c9dba40cSEd Schouten 
661072d5b98SWarner Losh static bool beeping;
662fadf3fb9SJohn Baldwin static struct callout beeping_timer;
663c9dba40cSEd Schouten 
664c9dba40cSEd Schouten static void
665c9dba40cSEd Schouten sysbeepstop(void *chan)
666c9dba40cSEd Schouten {
667c9dba40cSEd Schouten 
668c9dba40cSEd Schouten 	timer_spkr_release();
669072d5b98SWarner Losh 	beeping = false;
670c9dba40cSEd Schouten }
671c9dba40cSEd Schouten 
672c9dba40cSEd Schouten int
673072d5b98SWarner Losh sysbeep(int pitch, sbintime_t duration)
674c9dba40cSEd Schouten {
675c9dba40cSEd Schouten 
676c9dba40cSEd Schouten 	if (timer_spkr_acquire()) {
677c9dba40cSEd Schouten 		if (!beeping) {
678c9dba40cSEd Schouten 			/* Something else owns it. */
679c9dba40cSEd Schouten 			return (EBUSY);
680c9dba40cSEd Schouten 		}
681c9dba40cSEd Schouten 	}
682c9dba40cSEd Schouten 	timer_spkr_setfreq(pitch);
683c9dba40cSEd Schouten 	if (!beeping) {
684072d5b98SWarner Losh 		beeping = true;
685072d5b98SWarner Losh 		callout_reset_sbt(&beeping_timer, duration, 0, sysbeepstop,
686072d5b98SWarner Losh 		    NULL, C_PREL(5));
687c9dba40cSEd Schouten 	}
688c9dba40cSEd Schouten 	return (0);
689c9dba40cSEd Schouten }
690c9dba40cSEd Schouten 
691fadf3fb9SJohn Baldwin static void
692fadf3fb9SJohn Baldwin sysbeep_init(void *unused)
693fadf3fb9SJohn Baldwin {
694fadf3fb9SJohn Baldwin 
695fd90e2edSJung-uk Kim 	callout_init(&beeping_timer, 1);
696fadf3fb9SJohn Baldwin }
697fadf3fb9SJohn Baldwin SYSINIT(sysbeep, SI_SUB_SOFTINTR, SI_ORDER_ANY, sysbeep_init, NULL);
698c9dba40cSEd Schouten #else
699c9dba40cSEd Schouten 
700c9dba40cSEd Schouten /*
701c9dba40cSEd Schouten  * No hardware, no sound
702c9dba40cSEd Schouten  */
703c9dba40cSEd Schouten 
704c9dba40cSEd Schouten int
705072d5b98SWarner Losh sysbeep(int pitch __unused, sbintime_t duration __unused)
706c9dba40cSEd Schouten {
707c9dba40cSEd Schouten 
708c9dba40cSEd Schouten 	return (ENODEV);
709c9dba40cSEd Schouten }
710c9dba40cSEd Schouten 
711c9dba40cSEd Schouten #endif
712c9dba40cSEd Schouten 
71359644098SEd Maste /*
71459644098SEd Maste  * Temporary support for sc(4) to vt(4) transition.
71559644098SEd Maste  */
716018147eeSEd Maste static unsigned vty_prefer;
717af3b2549SHans Petter Selasky static char vty_name[16];
718af3b2549SHans Petter Selasky SYSCTL_STRING(_kern, OID_AUTO, vty, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, vty_name,
719af3b2549SHans Petter Selasky     0, "Console vty driver");
72059644098SEd Maste 
72159644098SEd Maste int
72259644098SEd Maste vty_enabled(unsigned vty)
72359644098SEd Maste {
72459644098SEd Maste 	static unsigned vty_selected = 0;
72559644098SEd Maste 
72659644098SEd Maste 	if (vty_selected == 0) {
72759644098SEd Maste 		TUNABLE_STR_FETCH("kern.vty", vty_name, sizeof(vty_name));
72859644098SEd Maste 		do {
72959644098SEd Maste #if defined(DEV_SC)
73059644098SEd Maste 			if (strcmp(vty_name, "sc") == 0) {
73159644098SEd Maste 				vty_selected = VTY_SC;
73259644098SEd Maste 				break;
73359644098SEd Maste 			}
73459644098SEd Maste #endif
73559644098SEd Maste #if defined(DEV_VT)
73659644098SEd Maste 			if (strcmp(vty_name, "vt") == 0) {
73759644098SEd Maste 				vty_selected = VTY_VT;
73859644098SEd Maste 				break;
73959644098SEd Maste 			}
74059644098SEd Maste #endif
741018147eeSEd Maste 			if (vty_prefer != 0) {
742018147eeSEd Maste 				vty_selected = vty_prefer;
743018147eeSEd Maste 				break;
744018147eeSEd Maste 			}
7452d6f6d63SJean-Sébastien Pédron #if defined(DEV_VT)
74659644098SEd Maste 			vty_selected = VTY_VT;
7472d6f6d63SJean-Sébastien Pédron #elif defined(DEV_SC)
7482d6f6d63SJean-Sébastien Pédron 			vty_selected = VTY_SC;
74959644098SEd Maste #endif
75059644098SEd Maste 		} while (0);
75159644098SEd Maste 
75259644098SEd Maste 		if (vty_selected == VTY_VT)
75359644098SEd Maste 			strcpy(vty_name, "vt");
75459644098SEd Maste 		else if (vty_selected == VTY_SC)
75559644098SEd Maste 			strcpy(vty_name, "sc");
75659644098SEd Maste 	}
75759644098SEd Maste 	return ((vty_selected & vty) != 0);
75859644098SEd Maste }
75959644098SEd Maste 
760018147eeSEd Maste void
761018147eeSEd Maste vty_set_preferred(unsigned vty)
762018147eeSEd Maste {
763018147eeSEd Maste 
764018147eeSEd Maste 	vty_prefer = vty;
765018147eeSEd Maste #if !defined(DEV_SC)
766969d3cc2SEd Maste 	vty_prefer &= ~VTY_SC;
767018147eeSEd Maste #endif
768018147eeSEd Maste #if !defined(DEV_VT)
769969d3cc2SEd Maste 	vty_prefer &= ~VTY_VT;
770018147eeSEd Maste #endif
771018147eeSEd Maste }
772