xref: /freebsd/sys/kern/kern_cons.c (revision ec6faf94c489e4217207bb2cbc1c36d73f07b522)
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>
55c9dba40cSEd Schouten #include <sys/kdb.h>
56c9dba40cSEd Schouten #include <sys/kernel.h>
57c9dba40cSEd Schouten #include <sys/malloc.h>
58c9dba40cSEd Schouten #include <sys/msgbuf.h>
59c9dba40cSEd Schouten #include <sys/namei.h>
60c9dba40cSEd Schouten #include <sys/priv.h>
61c9dba40cSEd Schouten #include <sys/proc.h>
62c9dba40cSEd Schouten #include <sys/queue.h>
63c9dba40cSEd Schouten #include <sys/reboot.h>
64c9dba40cSEd Schouten #include <sys/sysctl.h>
65c9dba40cSEd Schouten #include <sys/sbuf.h>
66c9dba40cSEd Schouten #include <sys/tty.h>
67c9dba40cSEd Schouten #include <sys/uio.h>
68c9dba40cSEd Schouten #include <sys/vnode.h>
69c9dba40cSEd Schouten 
70c9dba40cSEd Schouten #include <ddb/ddb.h>
71c9dba40cSEd Schouten 
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 				 */
93c9dba40cSEd Schouten static int cn_mute;
94c9dba40cSEd Schouten static char *consbuf;			/* buffer used by `consmsgbuf' */
95c9dba40cSEd Schouten static struct callout conscallout;	/* callout for outputting to constty */
96c9dba40cSEd Schouten struct msgbuf consmsgbuf;		/* message buffer for console tty */
97c9dba40cSEd Schouten static u_char console_pausing;		/* pause after each line during probe */
98c9dba40cSEd Schouten static char *console_pausestr=
99c9dba40cSEd Schouten "<pause; press any key to proceed to next line or '.' to end pause mode>";
100c9dba40cSEd Schouten struct tty *constty;			/* pointer to console "window" tty */
101c9dba40cSEd Schouten static struct mtx cnputs_mtx;		/* Mutex for cnputs(). */
102c9dba40cSEd Schouten static int use_cnputs_mtx = 0;		/* != 0 if cnputs_mtx locking reqd. */
103c9dba40cSEd Schouten 
104c9dba40cSEd Schouten static void constty_timeout(void *arg);
105c9dba40cSEd Schouten 
106c9dba40cSEd Schouten static struct consdev cons_consdev;
107c9dba40cSEd Schouten DATA_SET(cons_set, cons_consdev);
108c9dba40cSEd Schouten SET_DECLARE(cons_set, struct consdev);
109c9dba40cSEd Schouten 
110c9dba40cSEd Schouten void
111c9dba40cSEd Schouten cninit(void)
112c9dba40cSEd Schouten {
113c9dba40cSEd Schouten 	struct consdev *best_cn, *cn, **list;
114c9dba40cSEd Schouten 
115c9dba40cSEd Schouten 	/*
116c9dba40cSEd Schouten 	 * Check if we should mute the console (for security reasons perhaps)
117c9dba40cSEd Schouten 	 * It can be changes dynamically using sysctl kern.consmute
118c9dba40cSEd Schouten 	 * once we are up and going.
119c9dba40cSEd Schouten 	 *
120c9dba40cSEd Schouten 	 */
121c9dba40cSEd Schouten         cn_mute = ((boothowto & (RB_MUTE
122c9dba40cSEd Schouten 			|RB_SINGLE
123c9dba40cSEd Schouten 			|RB_VERBOSE
124c9dba40cSEd Schouten 			|RB_ASKNAME)) == RB_MUTE);
125c9dba40cSEd Schouten 
126c9dba40cSEd Schouten 	/*
127c9dba40cSEd Schouten 	 * Find the first console with the highest priority.
128c9dba40cSEd Schouten 	 */
129c9dba40cSEd Schouten 	best_cn = NULL;
130c9dba40cSEd Schouten 	SET_FOREACH(list, cons_set) {
131c9dba40cSEd Schouten 		cn = *list;
132c9dba40cSEd Schouten 		cnremove(cn);
1332992abe0SEd Schouten 		/* Skip cons_consdev. */
1342992abe0SEd Schouten 		if (cn->cn_ops == NULL)
135c9dba40cSEd Schouten 			continue;
1362992abe0SEd Schouten 		cn->cn_ops->cn_probe(cn);
137c9dba40cSEd Schouten 		if (cn->cn_pri == CN_DEAD)
138c9dba40cSEd Schouten 			continue;
139c9dba40cSEd Schouten 		if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
140c9dba40cSEd Schouten 			best_cn = cn;
141c9dba40cSEd Schouten 		if (boothowto & RB_MULTIPLE) {
142c9dba40cSEd Schouten 			/*
143c9dba40cSEd Schouten 			 * Initialize console, and attach to it.
144c9dba40cSEd Schouten 			 */
1452992abe0SEd Schouten 			cn->cn_ops->cn_init(cn);
146c9dba40cSEd Schouten 			cnadd(cn);
147c9dba40cSEd Schouten 		}
148c9dba40cSEd Schouten 	}
149c9dba40cSEd Schouten 	if (best_cn == NULL)
150c9dba40cSEd Schouten 		return;
151c9dba40cSEd Schouten 	if ((boothowto & RB_MULTIPLE) == 0) {
1522992abe0SEd Schouten 		best_cn->cn_ops->cn_init(best_cn);
153c9dba40cSEd Schouten 		cnadd(best_cn);
154c9dba40cSEd Schouten 	}
155c9dba40cSEd Schouten 	if (boothowto & RB_PAUSE)
156c9dba40cSEd Schouten 		console_pausing = 1;
157c9dba40cSEd Schouten 	/*
158c9dba40cSEd Schouten 	 * Make the best console the preferred console.
159c9dba40cSEd Schouten 	 */
160c9dba40cSEd Schouten 	cnselect(best_cn);
161dc61566fSZbigniew Bodek 
162dc61566fSZbigniew Bodek #ifdef EARLY_PRINTF
163dc61566fSZbigniew Bodek 	/*
164dc61566fSZbigniew Bodek 	 * Release early console.
165dc61566fSZbigniew Bodek 	 */
166dc61566fSZbigniew Bodek 	early_putc = NULL;
167dc61566fSZbigniew Bodek #endif
168c9dba40cSEd Schouten }
169c9dba40cSEd Schouten 
170c9dba40cSEd Schouten void
171c9dba40cSEd Schouten cninit_finish()
172c9dba40cSEd Schouten {
173c9dba40cSEd Schouten 	console_pausing = 0;
174c9dba40cSEd Schouten }
175c9dba40cSEd Schouten 
176c9dba40cSEd Schouten /* add a new physical console to back the virtual console */
177c9dba40cSEd Schouten int
178c9dba40cSEd Schouten cnadd(struct consdev *cn)
179c9dba40cSEd Schouten {
180c9dba40cSEd Schouten 	struct cn_device *cnd;
181c9dba40cSEd Schouten 	int i;
182c9dba40cSEd Schouten 
183c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
184c9dba40cSEd Schouten 		if (cnd->cnd_cn == cn)
185c9dba40cSEd Schouten 			return (0);
186c9dba40cSEd Schouten 	for (i = 0; i < CNDEVTAB_SIZE; i++) {
187c9dba40cSEd Schouten 		cnd = &cn_devtab[i];
188c9dba40cSEd Schouten 		if (cnd->cnd_cn == NULL)
189c9dba40cSEd Schouten 			break;
190c9dba40cSEd Schouten 	}
191c9dba40cSEd Schouten 	if (cnd->cnd_cn != NULL)
192c9dba40cSEd Schouten 		return (ENOMEM);
193c9dba40cSEd Schouten 	cnd->cnd_cn = cn;
194c9dba40cSEd Schouten 	if (cn->cn_name[0] == '\0') {
195c9dba40cSEd Schouten 		/* XXX: it is unclear if/where this print might output */
196c9dba40cSEd Schouten 		printf("WARNING: console at %p has no name\n", cn);
197c9dba40cSEd Schouten 	}
198c9dba40cSEd Schouten 	STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
199c9dba40cSEd Schouten 	if (STAILQ_FIRST(&cn_devlist) == cnd)
200c9dba40cSEd Schouten 		ttyconsdev_select(cnd->cnd_cn->cn_name);
201c9dba40cSEd Schouten 
202c9dba40cSEd Schouten 	/* Add device to the active mask. */
203c9dba40cSEd Schouten 	cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
204c9dba40cSEd Schouten 
205c9dba40cSEd Schouten 	return (0);
206c9dba40cSEd Schouten }
207c9dba40cSEd Schouten 
208c9dba40cSEd Schouten void
209c9dba40cSEd Schouten cnremove(struct consdev *cn)
210c9dba40cSEd Schouten {
211c9dba40cSEd Schouten 	struct cn_device *cnd;
212c9dba40cSEd Schouten 	int i;
213c9dba40cSEd Schouten 
214c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
215c9dba40cSEd Schouten 		if (cnd->cnd_cn != cn)
216c9dba40cSEd Schouten 			continue;
217c9dba40cSEd Schouten 		if (STAILQ_FIRST(&cn_devlist) == cnd)
218c9dba40cSEd Schouten 			ttyconsdev_select(NULL);
219c9dba40cSEd Schouten 		STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
220c9dba40cSEd Schouten 		cnd->cnd_cn = NULL;
221c9dba40cSEd Schouten 
222c9dba40cSEd Schouten 		/* Remove this device from available mask. */
223c9dba40cSEd Schouten 		for (i = 0; i < CNDEVTAB_SIZE; i++)
224c9dba40cSEd Schouten 			if (cnd == &cn_devtab[i]) {
225c9dba40cSEd Schouten 				cons_avail_mask &= ~(1 << i);
226c9dba40cSEd Schouten 				break;
227c9dba40cSEd Schouten 			}
228c9dba40cSEd Schouten #if 0
229c9dba40cSEd Schouten 		/*
230c9dba40cSEd Schouten 		 * XXX
231c9dba40cSEd Schouten 		 * syscons gets really confused if console resources are
232c9dba40cSEd Schouten 		 * freed after the system has initialized.
233c9dba40cSEd Schouten 		 */
234c9dba40cSEd Schouten 		if (cn->cn_term != NULL)
2352992abe0SEd Schouten 			cn->cn_ops->cn_term(cn);
236c9dba40cSEd Schouten #endif
237c9dba40cSEd Schouten 		return;
238c9dba40cSEd Schouten 	}
239c9dba40cSEd Schouten }
240c9dba40cSEd Schouten 
241c9dba40cSEd Schouten void
242c9dba40cSEd Schouten cnselect(struct consdev *cn)
243c9dba40cSEd Schouten {
244c9dba40cSEd Schouten 	struct cn_device *cnd;
245c9dba40cSEd Schouten 
246c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
247c9dba40cSEd Schouten 		if (cnd->cnd_cn != cn)
248c9dba40cSEd Schouten 			continue;
249c9dba40cSEd Schouten 		if (cnd == STAILQ_FIRST(&cn_devlist))
250c9dba40cSEd Schouten 			return;
251c9dba40cSEd Schouten 		STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
252c9dba40cSEd Schouten 		STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
253c9dba40cSEd Schouten 		ttyconsdev_select(cnd->cnd_cn->cn_name);
254c9dba40cSEd Schouten 		return;
255c9dba40cSEd Schouten 	}
256c9dba40cSEd Schouten }
257c9dba40cSEd Schouten 
258c9dba40cSEd Schouten void
259c9dba40cSEd Schouten cnavailable(struct consdev *cn, int available)
260c9dba40cSEd Schouten {
261c9dba40cSEd Schouten 	int i;
262c9dba40cSEd Schouten 
263c9dba40cSEd Schouten 	for (i = 0; i < CNDEVTAB_SIZE; i++) {
264c9dba40cSEd Schouten 		if (cn_devtab[i].cnd_cn == cn)
265c9dba40cSEd Schouten 			break;
266c9dba40cSEd Schouten 	}
267c9dba40cSEd Schouten 	if (available) {
268c9dba40cSEd Schouten 		if (i < CNDEVTAB_SIZE)
269c9dba40cSEd Schouten 			cons_avail_mask |= (1 << i);
270c9dba40cSEd Schouten 		cn->cn_flags &= ~CN_FLAG_NOAVAIL;
271c9dba40cSEd Schouten 	} else {
272c9dba40cSEd Schouten 		if (i < CNDEVTAB_SIZE)
273c9dba40cSEd Schouten 			cons_avail_mask &= ~(1 << i);
274c9dba40cSEd Schouten 		cn->cn_flags |= CN_FLAG_NOAVAIL;
275c9dba40cSEd Schouten 	}
276c9dba40cSEd Schouten }
277c9dba40cSEd Schouten 
278c9dba40cSEd Schouten int
279c9dba40cSEd Schouten cnunavailable(void)
280c9dba40cSEd Schouten {
281c9dba40cSEd Schouten 
282c9dba40cSEd Schouten 	return (cons_avail_mask == 0);
283c9dba40cSEd Schouten }
284c9dba40cSEd Schouten 
285c9dba40cSEd Schouten /*
286c9dba40cSEd Schouten  * sysctl_kern_console() provides output parseable in conscontrol(1).
287c9dba40cSEd Schouten  */
288c9dba40cSEd Schouten static int
289c9dba40cSEd Schouten sysctl_kern_console(SYSCTL_HANDLER_ARGS)
290c9dba40cSEd Schouten {
291c9dba40cSEd Schouten 	struct cn_device *cnd;
292c9dba40cSEd Schouten 	struct consdev *cp, **list;
293c9dba40cSEd Schouten 	char *p;
294c9dba40cSEd Schouten 	int delete, error;
295c9dba40cSEd Schouten 	struct sbuf *sb;
296c9dba40cSEd Schouten 
297657282e0SIan Lepore 	sb = sbuf_new(NULL, NULL, CNDEVPATHMAX * 2, SBUF_AUTOEXTEND |
298657282e0SIan Lepore 	    SBUF_INCLUDENUL);
299c9dba40cSEd Schouten 	if (sb == NULL)
300c9dba40cSEd Schouten 		return (ENOMEM);
301c9dba40cSEd Schouten 	sbuf_clear(sb);
302c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
303c9dba40cSEd Schouten 		sbuf_printf(sb, "%s,", cnd->cnd_cn->cn_name);
304c9dba40cSEd Schouten 	sbuf_printf(sb, "/");
305c9dba40cSEd Schouten 	SET_FOREACH(list, cons_set) {
306c9dba40cSEd Schouten 		cp = *list;
307c9dba40cSEd Schouten 		if (cp->cn_name[0] != '\0')
308c9dba40cSEd Schouten 			sbuf_printf(sb, "%s,", cp->cn_name);
309c9dba40cSEd Schouten 	}
310c9dba40cSEd Schouten 	sbuf_finish(sb);
311c9dba40cSEd Schouten 	error = sysctl_handle_string(oidp, sbuf_data(sb), sbuf_len(sb), req);
312c9dba40cSEd Schouten 	if (error == 0 && req->newptr != NULL) {
313c9dba40cSEd Schouten 		p = sbuf_data(sb);
314c9dba40cSEd Schouten 		error = ENXIO;
315c9dba40cSEd Schouten 		delete = 0;
316c9dba40cSEd Schouten 		if (*p == '-') {
317c9dba40cSEd Schouten 			delete = 1;
318c9dba40cSEd Schouten 			p++;
319c9dba40cSEd Schouten 		}
320c9dba40cSEd Schouten 		SET_FOREACH(list, cons_set) {
321c9dba40cSEd Schouten 			cp = *list;
322c9dba40cSEd Schouten 			if (strcmp(p, cp->cn_name) != 0)
323c9dba40cSEd Schouten 				continue;
324c9dba40cSEd Schouten 			if (delete) {
325c9dba40cSEd Schouten 				cnremove(cp);
326c9dba40cSEd Schouten 				error = 0;
327c9dba40cSEd Schouten 			} else {
328c9dba40cSEd Schouten 				error = cnadd(cp);
329c9dba40cSEd Schouten 				if (error == 0)
330c9dba40cSEd Schouten 					cnselect(cp);
331c9dba40cSEd Schouten 			}
332c9dba40cSEd Schouten 			break;
333c9dba40cSEd Schouten 		}
334c9dba40cSEd Schouten 	}
335c9dba40cSEd Schouten 	sbuf_delete(sb);
336c9dba40cSEd Schouten 	return (error);
337c9dba40cSEd Schouten }
338c9dba40cSEd Schouten 
339c9dba40cSEd Schouten SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW,
340c9dba40cSEd Schouten 	0, 0, sysctl_kern_console, "A", "Console device control");
341c9dba40cSEd Schouten 
342c9dba40cSEd Schouten /*
343c9dba40cSEd Schouten  * User has changed the state of the console muting.
344c9dba40cSEd Schouten  * This may require us to open or close the device in question.
345c9dba40cSEd Schouten  */
346c9dba40cSEd Schouten static int
347c9dba40cSEd Schouten sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
348c9dba40cSEd Schouten {
349c9dba40cSEd Schouten 	int error;
350c9dba40cSEd Schouten 
351c9dba40cSEd Schouten 	error = sysctl_handle_int(oidp, &cn_mute, 0, req);
352c9dba40cSEd Schouten 	if (error != 0 || req->newptr == NULL)
353c9dba40cSEd Schouten 		return (error);
354c9dba40cSEd Schouten 	return (error);
355c9dba40cSEd Schouten }
356c9dba40cSEd Schouten 
357c9dba40cSEd Schouten SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
358a0c87b74SGavin Atkinson 	0, sizeof(cn_mute), sysctl_kern_consmute, "I",
359a0c87b74SGavin Atkinson 	"State of the console muting");
360c9dba40cSEd Schouten 
3619976156fSAndriy Gapon void
3629976156fSAndriy Gapon cngrab()
3639976156fSAndriy Gapon {
3649976156fSAndriy Gapon 	struct cn_device *cnd;
3659976156fSAndriy Gapon 	struct consdev *cn;
3669976156fSAndriy Gapon 
3679976156fSAndriy Gapon 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
3689976156fSAndriy Gapon 		cn = cnd->cnd_cn;
3699976156fSAndriy Gapon 		if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
3709976156fSAndriy Gapon 			cn->cn_ops->cn_grab(cn);
3719976156fSAndriy Gapon 	}
3729976156fSAndriy Gapon }
3739976156fSAndriy Gapon 
3749976156fSAndriy Gapon void
3759976156fSAndriy Gapon cnungrab()
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_ungrab(cn);
3849976156fSAndriy Gapon 	}
3859976156fSAndriy Gapon }
3869976156fSAndriy Gapon 
387*ec6faf94SAndriy Gapon void
388*ec6faf94SAndriy Gapon cnresume()
389*ec6faf94SAndriy Gapon {
390*ec6faf94SAndriy Gapon 	struct cn_device *cnd;
391*ec6faf94SAndriy Gapon 	struct consdev *cn;
392*ec6faf94SAndriy Gapon 
393*ec6faf94SAndriy Gapon 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
394*ec6faf94SAndriy Gapon 		cn = cnd->cnd_cn;
395*ec6faf94SAndriy Gapon 		if (cn->cn_ops->cn_resume != NULL)
396*ec6faf94SAndriy Gapon 			cn->cn_ops->cn_resume(cn);
397*ec6faf94SAndriy Gapon 	}
398*ec6faf94SAndriy Gapon }
399*ec6faf94SAndriy Gapon 
400c9dba40cSEd Schouten /*
401c9dba40cSEd Schouten  * Low level console routines.
402c9dba40cSEd Schouten  */
403c9dba40cSEd Schouten int
404c9dba40cSEd Schouten cngetc(void)
405c9dba40cSEd Schouten {
406c9dba40cSEd Schouten 	int c;
407c9dba40cSEd Schouten 
408c9dba40cSEd Schouten 	if (cn_mute)
409c9dba40cSEd Schouten 		return (-1);
410c9dba40cSEd Schouten 	while ((c = cncheckc()) == -1)
411298fbd16SAndriy Gapon 		cpu_spinwait();
412c9dba40cSEd Schouten 	if (c == '\r')
413c9dba40cSEd Schouten 		c = '\n';		/* console input is always ICRNL */
414c9dba40cSEd Schouten 	return (c);
415c9dba40cSEd Schouten }
416c9dba40cSEd Schouten 
417c9dba40cSEd Schouten int
418c9dba40cSEd Schouten cncheckc(void)
419c9dba40cSEd Schouten {
420c9dba40cSEd Schouten 	struct cn_device *cnd;
421c9dba40cSEd Schouten 	struct consdev *cn;
422c9dba40cSEd Schouten 	int c;
423c9dba40cSEd Schouten 
424c9dba40cSEd Schouten 	if (cn_mute)
425c9dba40cSEd Schouten 		return (-1);
426c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
427c9dba40cSEd Schouten 		cn = cnd->cnd_cn;
428c9dba40cSEd Schouten 		if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
4292992abe0SEd Schouten 			c = cn->cn_ops->cn_getc(cn);
4302992abe0SEd Schouten 			if (c != -1)
431c9dba40cSEd Schouten 				return (c);
432c9dba40cSEd Schouten 		}
433c9dba40cSEd Schouten 	}
434c9dba40cSEd Schouten 	return (-1);
435c9dba40cSEd Schouten }
436c9dba40cSEd Schouten 
437c9dba40cSEd Schouten void
4388e628542SAndriy Gapon cngets(char *cp, size_t size, int visible)
4398e628542SAndriy Gapon {
4408e628542SAndriy Gapon 	char *lp, *end;
4418e628542SAndriy Gapon 	int c;
4428e628542SAndriy Gapon 
4438e628542SAndriy Gapon 	cngrab();
4448e628542SAndriy Gapon 
4458e628542SAndriy Gapon 	lp = cp;
4468e628542SAndriy Gapon 	end = cp + size - 1;
4478e628542SAndriy Gapon 	for (;;) {
4488e628542SAndriy Gapon 		c = cngetc() & 0177;
4498e628542SAndriy Gapon 		switch (c) {
4508e628542SAndriy Gapon 		case '\n':
4518e628542SAndriy Gapon 		case '\r':
4528e628542SAndriy Gapon 			cnputc(c);
4538e628542SAndriy Gapon 			*lp = '\0';
4548e628542SAndriy Gapon 			cnungrab();
4558e628542SAndriy Gapon 			return;
4568e628542SAndriy Gapon 		case '\b':
4578e628542SAndriy Gapon 		case '\177':
4588e628542SAndriy Gapon 			if (lp > cp) {
4599520f952SWarner Losh 				if (visible)
4609520f952SWarner Losh 					cnputs("\b \b");
4618e628542SAndriy Gapon 				lp--;
4628e628542SAndriy Gapon 			}
4638e628542SAndriy Gapon 			continue;
4648e628542SAndriy Gapon 		case '\0':
4658e628542SAndriy Gapon 			continue;
4668e628542SAndriy Gapon 		default:
4678e628542SAndriy Gapon 			if (lp < end) {
4688e628542SAndriy Gapon 				switch (visible) {
4698e628542SAndriy Gapon 				case GETS_NOECHO:
4708e628542SAndriy Gapon 					break;
4718e628542SAndriy Gapon 				case GETS_ECHOPASS:
4728e628542SAndriy Gapon 					cnputc('*');
4738e628542SAndriy Gapon 					break;
4748e628542SAndriy Gapon 				default:
4758e628542SAndriy Gapon 					cnputc(c);
4768e628542SAndriy Gapon 					break;
4778e628542SAndriy Gapon 				}
4788e628542SAndriy Gapon 				*lp++ = c;
4798e628542SAndriy Gapon 			}
4808e628542SAndriy Gapon 		}
4818e628542SAndriy Gapon 	}
4828e628542SAndriy Gapon }
4838e628542SAndriy Gapon 
4848e628542SAndriy Gapon void
485c9dba40cSEd Schouten cnputc(int c)
486c9dba40cSEd Schouten {
487c9dba40cSEd Schouten 	struct cn_device *cnd;
488c9dba40cSEd Schouten 	struct consdev *cn;
489c9dba40cSEd Schouten 	char *cp;
490c9dba40cSEd Schouten 
49142c8459bSIan Lepore #ifdef EARLY_PRINTF
49242c8459bSIan Lepore 	if (early_putc != NULL) {
49342c8459bSIan Lepore 		if (c == '\n')
49442c8459bSIan Lepore 			early_putc('\r');
49542c8459bSIan Lepore 		early_putc(c);
49642c8459bSIan Lepore 		return;
49742c8459bSIan Lepore 	}
49842c8459bSIan Lepore #endif
49942c8459bSIan Lepore 
500c9dba40cSEd Schouten 	if (cn_mute || c == '\0')
501c9dba40cSEd Schouten 		return;
502c9dba40cSEd Schouten 	STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
503c9dba40cSEd Schouten 		cn = cnd->cnd_cn;
504c9dba40cSEd Schouten 		if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
505c9dba40cSEd Schouten 			if (c == '\n')
5062992abe0SEd Schouten 				cn->cn_ops->cn_putc(cn, '\r');
5072992abe0SEd Schouten 			cn->cn_ops->cn_putc(cn, c);
508c9dba40cSEd Schouten 		}
509c9dba40cSEd Schouten 	}
510c9dba40cSEd Schouten 	if (console_pausing && c == '\n' && !kdb_active) {
511c9dba40cSEd Schouten 		for (cp = console_pausestr; *cp != '\0'; cp++)
512c9dba40cSEd Schouten 			cnputc(*cp);
513bf8696b4SAndriy Gapon 		cngrab();
514c9dba40cSEd Schouten 		if (cngetc() == '.')
515c9dba40cSEd Schouten 			console_pausing = 0;
516bf8696b4SAndriy Gapon 		cnungrab();
517c9dba40cSEd Schouten 		cnputc('\r');
518c9dba40cSEd Schouten 		for (cp = console_pausestr; *cp != '\0'; cp++)
519c9dba40cSEd Schouten 			cnputc(' ');
520c9dba40cSEd Schouten 		cnputc('\r');
521c9dba40cSEd Schouten 	}
522c9dba40cSEd Schouten }
523c9dba40cSEd Schouten 
524c9dba40cSEd Schouten void
525c9dba40cSEd Schouten cnputs(char *p)
526c9dba40cSEd Schouten {
527c9dba40cSEd Schouten 	int c;
528c9dba40cSEd Schouten 	int unlock_reqd = 0;
529c9dba40cSEd Schouten 
530c9dba40cSEd Schouten 	if (use_cnputs_mtx) {
53104a8159dSHans Petter Selasky 	  	/*
53204a8159dSHans Petter Selasky 		 * NOTE: Debug prints and/or witness printouts in
53304a8159dSHans Petter Selasky 		 * console driver clients can cause the "cnputs_mtx"
53404a8159dSHans Petter Selasky 		 * mutex to recurse. Simply return if that happens.
53504a8159dSHans Petter Selasky 		 */
53604a8159dSHans Petter Selasky 		if (mtx_owned(&cnputs_mtx))
53704a8159dSHans Petter Selasky 			return;
538c9dba40cSEd Schouten 		mtx_lock_spin(&cnputs_mtx);
539c9dba40cSEd Schouten 		unlock_reqd = 1;
540c9dba40cSEd Schouten 	}
541c9dba40cSEd Schouten 
542c9dba40cSEd Schouten 	while ((c = *p++) != '\0')
543c9dba40cSEd Schouten 		cnputc(c);
544c9dba40cSEd Schouten 
545c9dba40cSEd Schouten 	if (unlock_reqd)
546c9dba40cSEd Schouten 		mtx_unlock_spin(&cnputs_mtx);
547c9dba40cSEd Schouten }
548c9dba40cSEd Schouten 
549c9dba40cSEd Schouten static int consmsgbuf_size = 8192;
550c9dba40cSEd Schouten SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
551a0c87b74SGavin Atkinson     "Console tty buffer size");
552c9dba40cSEd Schouten 
553c9dba40cSEd Schouten /*
554c9dba40cSEd Schouten  * Redirect console output to a tty.
555c9dba40cSEd Schouten  */
556c9dba40cSEd Schouten void
557c9dba40cSEd Schouten constty_set(struct tty *tp)
558c9dba40cSEd Schouten {
559c9dba40cSEd Schouten 	int size;
560c9dba40cSEd Schouten 
561c9dba40cSEd Schouten 	KASSERT(tp != NULL, ("constty_set: NULL tp"));
562c9dba40cSEd Schouten 	if (consbuf == NULL) {
563c9dba40cSEd Schouten 		size = consmsgbuf_size;
564c9dba40cSEd Schouten 		consbuf = malloc(size, M_TTYCONS, M_WAITOK);
565c9dba40cSEd Schouten 		msgbuf_init(&consmsgbuf, consbuf, size);
566c9dba40cSEd Schouten 		callout_init(&conscallout, 0);
567c9dba40cSEd Schouten 	}
568c9dba40cSEd Schouten 	constty = tp;
569c9dba40cSEd Schouten 	constty_timeout(NULL);
570c9dba40cSEd Schouten }
571c9dba40cSEd Schouten 
572c9dba40cSEd Schouten /*
573c9dba40cSEd Schouten  * Disable console redirection to a tty.
574c9dba40cSEd Schouten  */
575c9dba40cSEd Schouten void
576c9dba40cSEd Schouten constty_clear(void)
577c9dba40cSEd Schouten {
578c9dba40cSEd Schouten 	int c;
579c9dba40cSEd Schouten 
580c9dba40cSEd Schouten 	constty = NULL;
581c9dba40cSEd Schouten 	if (consbuf == NULL)
582c9dba40cSEd Schouten 		return;
583c9dba40cSEd Schouten 	callout_stop(&conscallout);
584c9dba40cSEd Schouten 	while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
585c9dba40cSEd Schouten 		cnputc(c);
586c9dba40cSEd Schouten 	free(consbuf, M_TTYCONS);
587c9dba40cSEd Schouten 	consbuf = NULL;
588c9dba40cSEd Schouten }
589c9dba40cSEd Schouten 
590c9dba40cSEd Schouten /* Times per second to check for pending console tty messages. */
591c9dba40cSEd Schouten static int constty_wakeups_per_second = 5;
592c9dba40cSEd Schouten SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
593a0c87b74SGavin Atkinson     &constty_wakeups_per_second, 0,
594a0c87b74SGavin Atkinson     "Times per second to check for pending console tty messages");
595c9dba40cSEd Schouten 
596c9dba40cSEd Schouten static void
597c9dba40cSEd Schouten constty_timeout(void *arg)
598c9dba40cSEd Schouten {
599c9dba40cSEd Schouten 	int c;
600c9dba40cSEd Schouten 
601c9dba40cSEd Schouten 	if (constty != NULL) {
602c9dba40cSEd Schouten 		tty_lock(constty);
603c9dba40cSEd Schouten 		while ((c = msgbuf_getchar(&consmsgbuf)) != -1) {
604c9dba40cSEd Schouten 			if (tty_putchar(constty, c) < 0) {
605c9dba40cSEd Schouten 				tty_unlock(constty);
606c9dba40cSEd Schouten 				constty = NULL;
607c9dba40cSEd Schouten 				break;
608c9dba40cSEd Schouten 			}
609c9dba40cSEd Schouten 		}
610c9dba40cSEd Schouten 
611c9dba40cSEd Schouten 		if (constty != NULL)
612c9dba40cSEd Schouten 			tty_unlock(constty);
613c9dba40cSEd Schouten 	}
614c9dba40cSEd Schouten 	if (constty != NULL) {
615c9dba40cSEd Schouten 		callout_reset(&conscallout, hz / constty_wakeups_per_second,
616c9dba40cSEd Schouten 		    constty_timeout, NULL);
617c9dba40cSEd Schouten 	} else {
618c9dba40cSEd Schouten 		/* Deallocate the constty buffer memory. */
619c9dba40cSEd Schouten 		constty_clear();
620c9dba40cSEd Schouten 	}
621c9dba40cSEd Schouten }
622c9dba40cSEd Schouten 
623c9dba40cSEd Schouten static void
624c9dba40cSEd Schouten cn_drvinit(void *unused)
625c9dba40cSEd Schouten {
626c9dba40cSEd Schouten 
62704a8159dSHans Petter Selasky 	mtx_init(&cnputs_mtx, "cnputs_mtx", NULL, MTX_SPIN | MTX_NOWITNESS);
628c9dba40cSEd Schouten 	use_cnputs_mtx = 1;
629c9dba40cSEd Schouten }
630c9dba40cSEd Schouten 
631c9dba40cSEd Schouten SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL);
632c9dba40cSEd Schouten 
633c9dba40cSEd Schouten /*
634c9dba40cSEd Schouten  * Sysbeep(), if we have hardware for it
635c9dba40cSEd Schouten  */
636c9dba40cSEd Schouten 
637c9dba40cSEd Schouten #ifdef HAS_TIMER_SPKR
638c9dba40cSEd Schouten 
639c9dba40cSEd Schouten static int beeping;
640fadf3fb9SJohn Baldwin static struct callout beeping_timer;
641c9dba40cSEd Schouten 
642c9dba40cSEd Schouten static void
643c9dba40cSEd Schouten sysbeepstop(void *chan)
644c9dba40cSEd Schouten {
645c9dba40cSEd Schouten 
646c9dba40cSEd Schouten 	timer_spkr_release();
647c9dba40cSEd Schouten 	beeping = 0;
648c9dba40cSEd Schouten }
649c9dba40cSEd Schouten 
650c9dba40cSEd Schouten int
651c9dba40cSEd Schouten sysbeep(int pitch, int period)
652c9dba40cSEd Schouten {
653c9dba40cSEd Schouten 
654c9dba40cSEd Schouten 	if (timer_spkr_acquire()) {
655c9dba40cSEd Schouten 		if (!beeping) {
656c9dba40cSEd Schouten 			/* Something else owns it. */
657c9dba40cSEd Schouten 			return (EBUSY);
658c9dba40cSEd Schouten 		}
659c9dba40cSEd Schouten 	}
660c9dba40cSEd Schouten 	timer_spkr_setfreq(pitch);
661c9dba40cSEd Schouten 	if (!beeping) {
662c9dba40cSEd Schouten 		beeping = period;
663fadf3fb9SJohn Baldwin 		callout_reset(&beeping_timer, period, sysbeepstop, NULL);
664c9dba40cSEd Schouten 	}
665c9dba40cSEd Schouten 	return (0);
666c9dba40cSEd Schouten }
667c9dba40cSEd Schouten 
668fadf3fb9SJohn Baldwin static void
669fadf3fb9SJohn Baldwin sysbeep_init(void *unused)
670fadf3fb9SJohn Baldwin {
671fadf3fb9SJohn Baldwin 
672fd90e2edSJung-uk Kim 	callout_init(&beeping_timer, 1);
673fadf3fb9SJohn Baldwin }
674fadf3fb9SJohn Baldwin SYSINIT(sysbeep, SI_SUB_SOFTINTR, SI_ORDER_ANY, sysbeep_init, NULL);
675c9dba40cSEd Schouten #else
676c9dba40cSEd Schouten 
677c9dba40cSEd Schouten /*
678c9dba40cSEd Schouten  * No hardware, no sound
679c9dba40cSEd Schouten  */
680c9dba40cSEd Schouten 
681c9dba40cSEd Schouten int
682c9dba40cSEd Schouten sysbeep(int pitch __unused, int period __unused)
683c9dba40cSEd Schouten {
684c9dba40cSEd Schouten 
685c9dba40cSEd Schouten 	return (ENODEV);
686c9dba40cSEd Schouten }
687c9dba40cSEd Schouten 
688c9dba40cSEd Schouten #endif
689c9dba40cSEd Schouten 
69059644098SEd Maste /*
69159644098SEd Maste  * Temporary support for sc(4) to vt(4) transition.
69259644098SEd Maste  */
693018147eeSEd Maste static unsigned vty_prefer;
694af3b2549SHans Petter Selasky static char vty_name[16];
695af3b2549SHans Petter Selasky SYSCTL_STRING(_kern, OID_AUTO, vty, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, vty_name,
696af3b2549SHans Petter Selasky     0, "Console vty driver");
69759644098SEd Maste 
69859644098SEd Maste int
69959644098SEd Maste vty_enabled(unsigned vty)
70059644098SEd Maste {
70159644098SEd Maste 	static unsigned vty_selected = 0;
70259644098SEd Maste 
70359644098SEd Maste 	if (vty_selected == 0) {
70459644098SEd Maste 		TUNABLE_STR_FETCH("kern.vty", vty_name, sizeof(vty_name));
70559644098SEd Maste 		do {
70659644098SEd Maste #if defined(DEV_SC)
70759644098SEd Maste 			if (strcmp(vty_name, "sc") == 0) {
70859644098SEd Maste 				vty_selected = VTY_SC;
70959644098SEd Maste 				break;
71059644098SEd Maste 			}
71159644098SEd Maste #endif
71259644098SEd Maste #if defined(DEV_VT)
71359644098SEd Maste 			if (strcmp(vty_name, "vt") == 0) {
71459644098SEd Maste 				vty_selected = VTY_VT;
71559644098SEd Maste 				break;
71659644098SEd Maste 			}
71759644098SEd Maste #endif
718018147eeSEd Maste 			if (vty_prefer != 0) {
719018147eeSEd Maste 				vty_selected = vty_prefer;
720018147eeSEd Maste 				break;
721018147eeSEd Maste 			}
7222d6f6d63SJean-Sébastien Pédron #if defined(DEV_VT)
72359644098SEd Maste 			vty_selected = VTY_VT;
7242d6f6d63SJean-Sébastien Pédron #elif defined(DEV_SC)
7252d6f6d63SJean-Sébastien Pédron 			vty_selected = VTY_SC;
72659644098SEd Maste #endif
72759644098SEd Maste 		} while (0);
72859644098SEd Maste 
72959644098SEd Maste 		if (vty_selected == VTY_VT)
73059644098SEd Maste 			strcpy(vty_name, "vt");
73159644098SEd Maste 		else if (vty_selected == VTY_SC)
73259644098SEd Maste 			strcpy(vty_name, "sc");
73359644098SEd Maste 	}
73459644098SEd Maste 	return ((vty_selected & vty) != 0);
73559644098SEd Maste }
73659644098SEd Maste 
737018147eeSEd Maste void
738018147eeSEd Maste vty_set_preferred(unsigned vty)
739018147eeSEd Maste {
740018147eeSEd Maste 
741018147eeSEd Maste 	vty_prefer = vty;
742018147eeSEd Maste #if !defined(DEV_SC)
743969d3cc2SEd Maste 	vty_prefer &= ~VTY_SC;
744018147eeSEd Maste #endif
745018147eeSEd Maste #if !defined(DEV_VT)
746969d3cc2SEd Maste 	vty_prefer &= ~VTY_VT;
747018147eeSEd Maste #endif
748018147eeSEd Maste }
749018147eeSEd Maste 
750