2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991 The Regents of the University of California.
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
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 * $DragonFly: src/sys/kern/tty_cons.c,v 1.21 2007/05/07 05:21:41 dillon Exp $
45 #include <sys/param.h>
46 #include <sys/systm.h>
49 #include <sys/kernel.h>
52 #include <sys/reboot.h>
53 #include <sys/sysctl.h>
56 #include <sys/msgport.h>
57 #include <sys/msgport2.h>
58 #include <sys/device.h>
62 #include <machine/cpu.h>
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_poll_t cnpoll
;
70 static d_kqfilter_t cnkqfilter
;
72 static int cnintercept(struct dev_generic_args
*ap
);
75 static struct dev_ops cn_ops
= {
76 { "console", CDEV_MAJOR
, D_TTY
| D_KQFILTER
},
83 .d_kqfilter
= cnkqfilter
,
86 static struct dev_ops cn_iops
= {
87 { "intercept", CDEV_MAJOR
, D_TTY
| D_KQFILTER
},
88 .d_default
= cnintercept
91 static struct dev_ops
*cn_fwd_ops
;
94 //XXX: get this shit out! (alexh)
96 SYSCTL_OPAQUE(_machdep
, CPU_CONSDEV
, consdev
, CTLFLAG_RD
,
97 &cn_udev
, sizeof cn_udev
, "T,udev_t", "");
102 int cons_unavail
= 0; /* XXX:
103 * physical console not available for
104 * input (i.e., it is in graphics mode)
107 static u_char cn_is_open
; /* nonzero if logical console is open */
108 static int openmode
, openflag
; /* how /dev/console was openned */
109 static cdev_t cn_devfsdev
; /* represents the device private info */
110 static u_char cn_phys_is_open
; /* nonzero if physical device is open */
111 static u_char console_pausing
; /* pause after each line during probe */
112 static char *console_pausestr
=
113 "<pause; press any key to proceed to next line or '.' to end pause mode>";
115 struct consdev
*cn_tab
; /* physical console device info */
116 struct consdev
*gdb_tab
; /* physical gdb debugger device info */
118 CONS_DRIVER(cons
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
119 SET_DECLARE(cons_set
, struct consdev
);
124 struct consdev
*best_cp
, *cp
, **list
;
127 * Check if we should mute the console (for security reasons perhaps)
128 * It can be changes dynamically using sysctl kern.consmute
129 * once we are up and going.
132 cn_mute
= ((boothowto
& (RB_MUTE
136 |RB_CONFIG
)) == RB_MUTE
);
139 * Find the first console with the highest priority.
142 SET_FOREACH(list
, cons_set
) {
144 if (cp
->cn_probe
== NULL
)
147 if (cp
->cn_pri
> CN_DEAD
&& cp
->cn_probegood
&&
148 (best_cp
== NULL
|| cp
->cn_pri
> best_cp
->cn_pri
))
154 * If no console, give up.
156 if (best_cp
== NULL
) {
157 if (cn_tab
!= NULL
&& cn_tab
->cn_term
!= NULL
)
158 (*cn_tab
->cn_term
)(cn_tab
);
164 * Initialize console, then attach to it. This ordering allows
165 * debugging using the previous console, if any.
167 (*best_cp
->cn_init
)(best_cp
);
168 if (cn_tab
!= NULL
&& cn_tab
!= best_cp
) {
169 /* Turn off the previous console. */
170 if (cn_tab
->cn_term
!= NULL
)
171 (*cn_tab
->cn_term
)(cn_tab
);
173 if (boothowto
& RB_PAUSE
)
180 * Hook the open and close functions on the selected device.
185 if ((cn_tab
== NULL
) || cn_mute
)
187 if (cn_tab
->cn_dev
== NULL
) {
188 cn_tab
->cn_init_fini(cn_tab
);
189 if (cn_tab
->cn_dev
== NULL
) {
190 kprintf("Unable to hook console! cn_tab %p\n", cn_tab
);
195 cn_fwd_ops
= dev_ops_intercept(cn_tab
->cn_dev
, &cn_iops
);
196 cn_dev
= cn_tab
->cn_dev
;
206 dev_ops_restore(cn_tab
->cn_dev
, cn_fwd_ops
);
213 * User has changed the state of the console muting.
214 * This may require us to open or close the device in question.
217 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS
)
223 error
= sysctl_handle_int(oidp
, &cn_mute
, 0, req
);
224 if((error
== 0) && (cn_tab
!= NULL
) && (req
->newptr
!= NULL
)) {
225 if(ocn_mute
&& !cn_mute
) {
227 * going from muted to unmuted.. open the physical dev
228 * if the console has been openned
232 /* XXX curproc is not what we want really */
233 error
= dev_dopen(cn_dev
, openflag
,
234 openmode
, curproc
->p_ucred
);
236 /* if it failed, back it out */
237 if ( error
!= 0) cnuninit();
238 } else if (!ocn_mute
&& cn_mute
) {
240 * going from unmuted to muted.. close the physical dev
241 * if it's only open via /dev/console
244 error
= dev_dclose(cn_dev
, openflag
,
252 * back out the change if there was an error
260 SYSCTL_PROC(_kern
, OID_AUTO
, consmute
, CTLTYPE_INT
|CTLFLAG_RW
,
261 0, sizeof cn_mute
, sysctl_kern_consmute
, "I", "");
265 * We intercept the OPEN and CLOSE calls on the original device, and
266 * forward the rest through.
269 cnintercept(struct dev_generic_args
*ap
)
273 if (ap
->a_desc
== &dev_open_desc
) {
274 error
= cnopen((struct dev_open_args
*)ap
);
275 } else if (ap
->a_desc
== &dev_close_desc
) {
276 error
= cnclose((struct dev_close_args
*)ap
);
277 } else if (cn_fwd_ops
) {
278 error
= dev_doperate_ops(cn_fwd_ops
, ap
);
286 * cnopen() is called as an intercept function (dev will be that of the
287 * actual physical device representing our console), and also called from
288 * the muting code and from the /dev/console switch (dev will have the
292 cnopen(struct dev_open_args
*ap
)
294 cdev_t dev
= ap
->a_head
.a_dev
;
295 int flag
= ap
->a_oflags
;
296 int mode
= ap
->a_devtype
;
301 if (cn_tab
== NULL
|| cn_fwd_ops
== NULL
)
304 cndev
= cn_tab
->cn_dev
; /* actual physical device */
305 physdev
= (dev
== cn_devfsdev
) ? cndev
: dev
;
308 * If mute is active, then non console opens don't get here
309 * so we don't need to check for that. They bypass this and go
310 * straight to the device.
312 * It is important to note that due to our intercept and the fact
313 * that we might be called via the original (intercepted) device,
314 * the original device's ops may point to us, so to avoid an
315 * infinite recursion we have to forward through cn_fwd_ops.
316 * This is a severe hack that really needs to be fixed XXX.
318 * XXX at the moment we assume that the port forwarding function
319 * is synchronous for open.
322 ap
->a_head
.a_dev
= physdev
;
323 retval
= dev_doperate_ops(cn_fwd_ops
, &ap
->a_head
);
327 * check if we openned it via /dev/console or
328 * via the physical entry (e.g. /dev/sio0).
332 } else if (physdev
== cndev
) {
337 dev
->si_tty
= cndev
->si_tty
;
343 * cnclose() is called as a port intercept function (dev will be that of the
344 * actual physical device representing our console), and also called from
345 * the muting code and from the /dev/console switch (dev will have the
349 cnclose(struct dev_close_args
*ap
)
354 cdev_t dev
= ap
->a_head
.a_dev
;
356 if (cn_tab
== NULL
|| cn_fwd_ops
== NULL
)
358 cndev
= cn_tab
->cn_dev
;
359 cn_tp
= cndev
->si_tty
;
360 physdev
= (dev
== cn_devfsdev
) ? cndev
: dev
;
363 * act appropriatly depending on whether it's /dev/console
364 * or the pysical device (e.g. /dev/sio) that's being closed.
365 * in either case, don't actually close the device unless
369 /* the physical device is about to be closed */
373 /* perform a ttyhalfclose() */
374 /* reset session and proc group */
375 ttyclearsession(cn_tp
);
379 } else if (physdev
== cndev
) {
380 /* the logical console is about to be closed */
387 ap
->a_head
.a_dev
= dev
;
388 return (dev_doperate_ops(cn_fwd_ops
, &ap
->a_head
));
394 * The following functions are dispatched solely from the /dev/console
395 * device. Their job is primarily to forward the request through.
396 * If the console is not attached to anything then write()'s are sunk
397 * to null and reads return 0 (mostly).
400 cnread(struct dev_read_args
*ap
)
402 if (cn_tab
== NULL
|| cn_fwd_ops
== NULL
)
404 ap
->a_head
.a_dev
= cn_tab
->cn_dev
;
405 return (dev_doperate(&ap
->a_head
));
409 cnwrite(struct dev_write_args
*ap
)
411 struct uio
*uio
= ap
->a_uio
;
414 if (cn_tab
== NULL
|| cn_fwd_ops
== NULL
) {
415 uio
->uio_resid
= 0; /* dump the data */
419 dev
= constty
->t_dev
;
421 dev
= cn_tab
->cn_dev
;
423 ap
->a_head
.a_dev
= dev
;
424 return (dev_doperate(&ap
->a_head
));
428 cnioctl(struct dev_ioctl_args
*ap
)
432 if (cn_tab
== NULL
|| cn_fwd_ops
== NULL
)
434 KKASSERT(curproc
!= NULL
);
436 * Superuser can always use this to wrest control of console
437 * output from the "virtual" console.
439 if (ap
->a_cmd
== TIOCCONS
&& constty
) {
441 error
= priv_check_cred(ap
->a_cred
, PRIV_ROOT
, 0);
448 ap
->a_head
.a_dev
= cn_tab
->cn_dev
;
449 return (dev_doperate(&ap
->a_head
));
453 cnpoll(struct dev_poll_args
*ap
)
455 if ((cn_tab
== NULL
) || cn_mute
|| cn_fwd_ops
== NULL
)
457 ap
->a_head
.a_dev
= cn_tab
->cn_dev
;
458 return (dev_doperate(&ap
->a_head
));
462 cnkqfilter(struct dev_kqfilter_args
*ap
)
464 if ((cn_tab
== NULL
) || cn_mute
|| cn_fwd_ops
== NULL
)
466 ap
->a_head
.a_dev
= cn_tab
->cn_dev
;
467 return (dev_doperate(&ap
->a_head
));
471 * These synchronous functions are primarily used the kernel needs to
472 * access the keyboard (e.g. when running the debugger), or output data
473 * directly to the console.
479 if ((cn_tab
== NULL
) || cn_mute
)
481 c
= (*cn_tab
->cn_getc
)(cn_tab
->cn_dev
);
482 if (c
== '\r') c
= '\n'; /* console input is always ICRNL */
489 if ((cn_tab
== NULL
) || cn_mute
)
491 return ((*cn_tab
->cn_checkc
)(cn_tab
->cn_dev
));
499 if ((cn_tab
== NULL
) || cn_mute
)
503 (*cn_tab
->cn_putc
)(cn_tab
->cn_dev
, '\r');
504 (*cn_tab
->cn_putc
)(cn_tab
->cn_dev
, c
);
506 if (console_pausing
&& !db_active
&& (c
== '\n')) {
508 if (console_pausing
&& (c
== '\n')) {
510 for(cp
=console_pausestr
; *cp
!= '\0'; cp
++)
511 (*cn_tab
->cn_putc
)(cn_tab
->cn_dev
, *cp
);
514 (*cn_tab
->cn_putc
)(cn_tab
->cn_dev
, '\r');
515 for(cp
=console_pausestr
; *cp
!= '\0'; cp
++)
516 (*cn_tab
->cn_putc
)(cn_tab
->cn_dev
, ' ');
517 (*cn_tab
->cn_putc
)(cn_tab
->cn_dev
, '\r');
531 if (refcount
== 0 && cn_tab
->cn_dbctl
!= NULL
)
532 (*cn_tab
->cn_dbctl
)(cn_tab
->cn_dev
, on
);
538 cn_drvinit(void *unused
)
540 cn_devfsdev
= make_only_devfs_dev(&cn_ops
, 0, UID_ROOT
, GID_WHEEL
,
544 SYSINIT(cndev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR
,cn_drvinit
,NULL
)