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