xref: /dflybsd-src/sys/kern/tty_cons.c (revision 5b991541a99aa38e5ca17ac8e6abee49bd57ac56)
1 /*
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1991 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Systems Programming Group of the University of Utah Computer
8  * Science Department.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	from: @(#)cons.c	7.2 (Berkeley) 5/9/91
39  * $FreeBSD: src/sys/kern/tty_cons.c,v 1.81.2.4 2001/12/17 18:44:41 guido Exp $
40  */
41 
42 #include "opt_ddb.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/conf.h>
47 #include <sys/cons.h>
48 #include <sys/kernel.h>
49 #include <sys/proc.h>
50 #include <sys/priv.h>
51 #include <sys/reboot.h>
52 #include <sys/sysctl.h>
53 #include <sys/tty.h>
54 #include <sys/uio.h>
55 #include <sys/msgport.h>
56 #include <sys/msgport2.h>
57 #include <sys/device.h>
58 
59 #include <ddb/ddb.h>
60 
61 #include <machine/cpu.h>
62 
63 static d_open_t cnopen;
64 static d_close_t cnclose;
65 static d_read_t cnread;
66 static d_write_t cnwrite;
67 static d_ioctl_t cnioctl;
68 static d_kqfilter_t cnkqfilter;
69 
70 static int cnintercept(struct dev_generic_args *ap);
71 
72 #define	CDEV_MAJOR	0
73 static struct dev_ops cn_ops = {
74 	{ "console", 0, D_TTY },
75 	.d_open =	cnopen,
76 	.d_close =	cnclose,
77 	.d_read =	cnread,
78 	.d_write =	cnwrite,
79 	.d_ioctl =	cnioctl,
80 	.d_kqfilter =	cnkqfilter,
81 };
82 
83 static struct dev_ops cn_iops = {
84 	{ "intercept", 0, D_TTY },
85 	.d_default =    cnintercept
86 };
87 
88 static struct dev_ops *cn_fwd_ops;
89 static cdev_t	cn_dev;
90 
91 //XXX: get this shit out! (alexh)
92 #if 0
93 SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD,
94 	&cn_udev, sizeof cn_udev, "T,udev_t", "");
95 #endif
96 
97 static int cn_mute;
98 
99 int	cons_unavail = 0;	/* XXX:
100 				 * physical console not available for
101 				 * input (i.e., it is in graphics mode)
102 				 */
103 int	sysbeep_enable = 1;
104 
105 static u_char cn_is_open;		/* nonzero if logical console is open */
106 static int openmode, openflag;		/* how /dev/console was openned */
107 static cdev_t cn_devfsdev;		/* represents the device private info */
108 static u_char cn_phys_is_open;		/* nonzero if physical device is open */
109 static u_char console_pausing;		/* pause after each line during probe */
110 static char *console_pausestr=
111 "<pause; press any key to proceed to next line or '.' to end pause mode>";
112 
113 struct consdev *cn_tab;		/* physical console device info */
114 struct consdev *gdb_tab;	/* physical gdb debugger device info */
115 
116 SYSCTL_INT(_kern, OID_AUTO, sysbeep_enable, CTLFLAG_RW, &sysbeep_enable, 0, "");
117 
118 CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
119 SET_DECLARE(cons_set, struct consdev);
120 
121 void
122 cninit(void)
123 {
124 	struct consdev *best_cp, *cp, **list;
125 
126 	/*
127 	 * Workaround for token acquisition and releases during the
128 	 * console init.  For some reason if lwkt_gettoken()'s mpcount
129 	 * optimization is turned off the console init blows up.  It
130 	 * might be trying to kprintf() something in the middle of
131 	 * its init.
132 	 */
133 	lwkt_gettoken(&tty_token);
134 
135 	/*
136 	 * Check if we should mute the console (for security reasons perhaps)
137 	 * It can be changes dynamically using sysctl kern.consmute
138 	 * once we are up and going.
139 	 *
140 	 */
141 	cn_mute = ((boothowto & (RB_MUTE
142 			|RB_SINGLE
143 			|RB_VERBOSE
144 			|RB_ASKNAME
145 			|RB_CONFIG)) == RB_MUTE);
146 
147 	/*
148 	 * Find the first console with the highest priority.
149 	 */
150 	best_cp = NULL;
151 	SET_FOREACH(list, cons_set) {
152 		cp = *list;
153 		if (cp->cn_probe == NULL)
154 			continue;
155 		(*cp->cn_probe)(cp);
156 		if (cp->cn_pri > CN_DEAD && cp->cn_probegood &&
157 		    (best_cp == NULL || cp->cn_pri > best_cp->cn_pri))
158 			best_cp = cp;
159 	}
160 
161 
162 	/*
163 	 * If no console, give up.
164 	 */
165 	if (best_cp == NULL) {
166 		if (cn_tab != NULL && cn_tab->cn_term != NULL)
167 			(*cn_tab->cn_term)(cn_tab);
168 		goto done;
169 	}
170 
171 	/*
172 	 * Initialize console, then attach to it.  This ordering allows
173 	 * debugging using the previous console, if any.
174 	 */
175 	(*best_cp->cn_init)(best_cp);
176 	if (cn_tab != NULL && cn_tab != best_cp) {
177 		/* Turn off the previous console.  */
178 		if (cn_tab->cn_term != NULL)
179 			(*cn_tab->cn_term)(cn_tab);
180 	}
181 	if (boothowto & RB_PAUSE)
182 		console_pausing = 1;
183 done:
184 	cn_tab = best_cp;
185 
186 	/*
187 	 * We can safely release the token after the init is done.
188 	 * Also assert that the mpcount is still correct or otherwise
189 	 * the SMP/AP boot will blow up on us.
190 	 */
191 	lwkt_reltoken(&tty_token);
192 #ifdef SMP
193 	KKASSERT(curthread->td_mpcount == 1);
194 #endif
195 }
196 
197 
198 /*
199  * Hook the open and close functions on the selected device.
200  */
201 void
202 cninit_finish(void)
203 {
204 	if ((cn_tab == NULL) || cn_mute)
205 		return;
206 	if (cn_tab->cn_dev == NULL) {
207 		cn_tab->cn_init_fini(cn_tab);
208 		if (cn_tab->cn_dev == NULL) {
209 			kprintf("Unable to hook console! cn_tab %p\n", cn_tab);
210 			return;
211 		}
212 	}
213 
214 	cn_fwd_ops = dev_ops_intercept(cn_tab->cn_dev, &cn_iops);
215 	cn_dev = cn_tab->cn_dev;
216 	console_pausing = 0;
217 }
218 
219 static void
220 cnuninit(void)
221 {
222 	if (cn_tab == NULL)
223 		return;
224 	if (cn_fwd_ops)
225 		dev_ops_restore(cn_tab->cn_dev, cn_fwd_ops);
226 	cn_fwd_ops = NULL;
227 	cn_dev = NULL;
228 }
229 
230 
231 /*
232  * User has changed the state of the console muting.
233  * This may require us to open or close the device in question.
234  */
235 static int
236 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
237 {
238 	int error;
239 	int ocn_mute;
240 
241 	ocn_mute = cn_mute;
242 	error = sysctl_handle_int(oidp, &cn_mute, 0, req);
243 	if((error == 0) && (cn_tab != NULL) && (req->newptr != NULL)) {
244 		if(ocn_mute && !cn_mute) {
245 			/*
246 			 * going from muted to unmuted.. open the physical dev
247 			 * if the console has been openned
248 			 */
249 			cninit_finish();
250 			if (cn_is_open) {
251 				/* XXX curproc is not what we want really */
252 				error = dev_dopen(cn_dev, openflag,
253 						openmode, curproc->p_ucred);
254 			}
255 			/* if it failed, back it out */
256 			if ( error != 0) cnuninit();
257 		} else if (!ocn_mute && cn_mute) {
258 			/*
259 			 * going from unmuted to muted.. close the physical dev
260 			 * if it's only open via /dev/console
261 			 */
262 			if (cn_is_open) {
263 				error = dev_dclose(cn_dev, openflag,
264 						   openmode);
265 			}
266 			if (error == 0)
267 				cnuninit();
268 		}
269 		if (error != 0) {
270 			/*
271 	 		 * back out the change if there was an error
272 			 */
273 			cn_mute = ocn_mute;
274 		}
275 	}
276 	return (error);
277 }
278 
279 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
280 	0, sizeof cn_mute, sysctl_kern_consmute, "I", "");
281 
282 
283 /*
284  * We intercept the OPEN and CLOSE calls on the original device, and
285  * forward the rest through.
286  */
287 static int
288 cnintercept(struct dev_generic_args *ap)
289 {
290 	int error;
291 
292 	if (ap->a_desc == &dev_open_desc) {
293 		error = cnopen((struct dev_open_args *)ap);
294 	} else if (ap->a_desc == &dev_close_desc) {
295 		error = cnclose((struct dev_close_args *)ap);
296 	} else if (cn_fwd_ops) {
297 		error = dev_doperate_ops(cn_fwd_ops, ap);
298 	} else {
299 		error = ENXIO;
300 	}
301 	return (error);
302 }
303 
304 /*
305  * cnopen() is called as an intercept function (dev will be that of the
306  * actual physical device representing our console), and also called from
307  * the muting code and from the /dev/console switch (dev will have the
308  * console's cdevsw).
309  */
310 static int
311 cnopen(struct dev_open_args *ap)
312 {
313 	cdev_t dev = ap->a_head.a_dev;
314 	int flag = ap->a_oflags;
315 	int mode = ap->a_devtype;
316 	cdev_t cndev;
317 	cdev_t physdev;
318 	int retval = 0;
319 
320 	if (cn_tab == NULL || cn_fwd_ops == NULL)
321 		return (0);
322 
323 	cndev = cn_tab->cn_dev;		/* actual physical device */
324 	physdev = (dev == cn_devfsdev) ? cndev : dev;
325 
326 	/*
327 	 * If mute is active, then non console opens don't get here
328 	 * so we don't need to check for that. They bypass this and go
329 	 * straight to the device.
330 	 *
331 	 * It is important to note that due to our intercept and the fact
332 	 * that we might be called via the original (intercepted) device,
333 	 * the original device's ops may point to us, so to avoid an
334 	 * infinite recursion we have to forward through cn_fwd_ops.
335 	 * This is a severe hack that really needs to be fixed XXX.
336 	 *
337 	 * XXX at the moment we assume that the port forwarding function
338 	 * is synchronous for open.
339 	 */
340 	if (!cn_mute) {
341 		ap->a_head.a_dev = physdev;
342 		retval = dev_doperate_ops(cn_fwd_ops, &ap->a_head);
343 	}
344 	if (retval == 0) {
345 		/*
346 		 * check if we openned it via /dev/console or
347 		 * via the physical entry (e.g. /dev/sio0).
348 		 */
349 		if (dev == cndev) {
350 			cn_phys_is_open = 1;
351 		} else if (physdev == cndev) {
352 			openmode = mode;
353 			openflag = flag;
354 			cn_is_open = 1;
355 		}
356 		dev->si_tty = cndev->si_tty;
357 	}
358 	return (retval);
359 }
360 
361 /*
362  * cnclose() is called as a port intercept function (dev will be that of the
363  * actual physical device representing our console), and also called from
364  * the muting code and from the /dev/console switch (dev will have the
365  * console's cdevsw).
366  */
367 static int
368 cnclose(struct dev_close_args *ap)
369 {
370 	struct tty *cn_tp;
371 	cdev_t cndev;
372 	cdev_t physdev;
373 	cdev_t dev = ap->a_head.a_dev;
374 
375 	if (cn_tab == NULL || cn_fwd_ops == NULL)
376 		return(0);
377 	cndev = cn_tab->cn_dev;
378 	cn_tp = cndev->si_tty;
379 	physdev = (dev == cn_devfsdev) ? cndev : dev;
380 
381 	/*
382 	 * act appropriatly depending on whether it's /dev/console
383 	 * or the pysical device (e.g. /dev/sio) that's being closed.
384 	 * in either case, don't actually close the device unless
385 	 * both are closed.
386 	 */
387 	if (dev == cndev) {
388 		/* the physical device is about to be closed */
389 		cn_phys_is_open = 0;
390 		if (cn_is_open) {
391 			if (cn_tp) {
392 				/* perform a ttyhalfclose() */
393 				/* reset session and proc group */
394 				ttyclearsession(cn_tp);
395 			}
396 			return(0);
397 		}
398 	} else if (physdev == cndev) {
399 		/* the logical console is about to be closed */
400 		cn_is_open = 0;
401 		if (cn_phys_is_open)
402 			return(0);
403 		dev = cndev;
404 	}
405 	if (cn_fwd_ops) {
406 		ap->a_head.a_dev = dev;
407 		return (dev_doperate_ops(cn_fwd_ops, &ap->a_head));
408 	}
409 	return (0);
410 }
411 
412 /*
413  * The following functions are dispatched solely from the /dev/console
414  * device.  Their job is primarily to forward the request through.
415  * If the console is not attached to anything then write()'s are sunk
416  * to null and reads return 0 (mostly).
417  */
418 static int
419 cnread(struct dev_read_args *ap)
420 {
421 	if (cn_tab == NULL || cn_fwd_ops == NULL)
422 		return (0);
423 	ap->a_head.a_dev = cn_tab->cn_dev;
424 	return (dev_doperate(&ap->a_head));
425 }
426 
427 static int
428 cnwrite(struct dev_write_args *ap)
429 {
430 	struct uio *uio = ap->a_uio;
431 	cdev_t dev;
432 
433 	if (cn_tab == NULL || cn_fwd_ops == NULL) {
434 		uio->uio_resid = 0; /* dump the data */
435 		return (0);
436 	}
437 	if (constty)
438 		dev = constty->t_dev;
439 	else
440 		dev = cn_tab->cn_dev;
441 	log_console(uio);
442 	ap->a_head.a_dev = dev;
443 	return (dev_doperate(&ap->a_head));
444 }
445 
446 static int
447 cnioctl(struct dev_ioctl_args *ap)
448 {
449 	int error;
450 
451 	if (cn_tab == NULL || cn_fwd_ops == NULL)
452 		return (0);
453 	KKASSERT(curproc != NULL);
454 	/*
455 	 * Superuser can always use this to wrest control of console
456 	 * output from the "virtual" console.
457 	 */
458 	if (ap->a_cmd == TIOCCONS && constty) {
459 		if (ap->a_cred) {
460 			error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
461 			if (error)
462 				return (error);
463 		}
464 		constty = NULL;
465 		return (0);
466 	}
467 	ap->a_head.a_dev = cn_tab->cn_dev;
468 	return (dev_doperate(&ap->a_head));
469 }
470 
471 static int
472 cnkqfilter(struct dev_kqfilter_args *ap)
473 {
474 	if ((cn_tab == NULL) || cn_mute || cn_fwd_ops == NULL)
475 		return (1);
476 	ap->a_head.a_dev = cn_tab->cn_dev;
477 	return (dev_doperate(&ap->a_head));
478 }
479 
480 /*
481  * These synchronous functions are primarily used the kernel needs to
482  * access the keyboard (e.g. when running the debugger), or output data
483  * directly to the console.
484  */
485 int
486 cngetc(void)
487 {
488 	int c;
489 	if ((cn_tab == NULL) || cn_mute)
490 		return (-1);
491 	c = (*cn_tab->cn_getc)(cn_tab->cn_dev);
492 	if (c == '\r') c = '\n'; /* console input is always ICRNL */
493 	return (c);
494 }
495 
496 int
497 cncheckc(void)
498 {
499 	if ((cn_tab == NULL) || cn_mute)
500 		return (-1);
501 	return ((*cn_tab->cn_checkc)(cn_tab->cn_dev));
502 }
503 
504 void
505 cnputc(int c)
506 {
507 	char *cp;
508 
509 	if ((cn_tab == NULL) || cn_mute)
510 		return;
511 	if (c) {
512 		if (c == '\n')
513 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
514 		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
515 #ifdef DDB
516 		if (console_pausing && !db_active && (c == '\n')) {
517 #else
518 		if (console_pausing && (c == '\n')) {
519 #endif
520 			for(cp=console_pausestr; *cp != '\0'; cp++)
521 			    (*cn_tab->cn_putc)(cn_tab->cn_dev, *cp);
522 			if (cngetc() == '.')
523 				console_pausing = 0;
524 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
525 			for(cp=console_pausestr; *cp != '\0'; cp++)
526 			    (*cn_tab->cn_putc)(cn_tab->cn_dev, ' ');
527 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
528 		}
529 	}
530 }
531 
532 void
533 cndbctl(int on)
534 {
535 	static int refcount;
536 
537 	if (cn_tab == NULL)
538 		return;
539 	if (!on)
540 		refcount--;
541 	if (refcount == 0 && cn_tab->cn_dbctl != NULL)
542 		(*cn_tab->cn_dbctl)(cn_tab->cn_dev, on);
543 	if (on)
544 		refcount++;
545 }
546 
547 static void
548 cn_drvinit(void *unused)
549 {
550 	cn_devfsdev = make_only_devfs_dev(&cn_ops, 0, UID_ROOT, GID_WHEEL,
551 					  0600, "console");
552 }
553 
554 SYSINIT(cndev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,cn_drvinit,NULL)
555