4 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com>
9 * Redistribution and use in source and binary forms, with or without
10 * 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
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $DragonFly: src/sys/platform/vkernel/platform/console.c,v 1.17 2007/07/02 04:19:14 dillon Exp $
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
44 #include <sys/termios.h>
45 #include <sys/fcntl.h>
46 #include <sys/signalvar.h>
47 #include <sys/eventhandler.h>
48 #include <sys/interrupt.h>
49 #include <machine/md_var.h>
54 static int console_stolen_by_kernel;
55 static struct kqueue_info *kqueue_console_info;
58 * Global console locking functions
70 /************************************************************************
72 ************************************************************************
76 #define CDEV_MAJOR 12 /* steal the same major as /dev/ttyv* */
78 static int vcons_tty_param(struct tty *tp, struct termios *tio);
79 static void vcons_tty_start(struct tty *tp);
80 static void vcons_intr(void *tpx, struct intrframe *frame __unused);
82 static d_open_t vcons_open;
83 static d_close_t vcons_close;
84 static d_ioctl_t vcons_ioctl;
86 static struct dev_ops vcons_ops = {
87 { "vcons", CDEV_MAJOR, D_TTY },
89 .d_close = vcons_close,
92 .d_ioctl = vcons_ioctl,
93 .d_kqfilter = ttykqfilter,
97 vcons_open(struct dev_open_args *ap)
99 cdev_t dev = ap->a_head.a_dev;
103 lwkt_gettoken(&tty_token);
104 tp = dev->si_tty = ttymalloc(dev->si_tty);
106 #define ISSET(t, f) ((t) & (f))
108 if ((tp->t_state & TS_ISOPEN) == 0) {
109 tp->t_oproc = vcons_tty_start;
110 tp->t_param = vcons_tty_param;
111 tp->t_stop = nottystop;
114 tp->t_state |= TS_CARR_ON | TS_CONNECTED;
116 tp->t_iflag = TTYDEF_IFLAG;
117 tp->t_oflag = TTYDEF_OFLAG;
118 tp->t_cflag = TTYDEF_CFLAG;
119 tp->t_lflag = TTYDEF_LFLAG;
120 tp->t_ispeed = TTYDEF_SPEED;
121 tp->t_ospeed = TTYDEF_SPEED;
124 if (minor(dev) == 0) {
125 error = (*linesw[tp->t_line].l_open)(dev, tp);
126 ioctl(0, TIOCGWINSZ, &tp->t_winsize);
128 if (kqueue_console_info == NULL)
129 kqueue_console_info = kqueue_add(0, vcons_intr, tp);
131 /* dummy up other minors so the installer will run */
134 lwkt_reltoken(&tty_token);
139 vcons_close(struct dev_close_args *ap)
141 cdev_t dev = ap->a_head.a_dev;
144 lwkt_gettoken(&tty_token);
146 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
148 lwkt_reltoken(&tty_token);
153 vcons_ioctl(struct dev_ioctl_args *ap)
155 cdev_t dev = ap->a_head.a_dev;
159 lwkt_gettoken(&tty_token);
161 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
162 ap->a_fflag, ap->a_cred);
163 if (error != ENOIOCTL) {
164 lwkt_reltoken(&tty_token);
167 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
168 if (error != ENOIOCTL) {
169 lwkt_reltoken(&tty_token);
172 lwkt_reltoken(&tty_token);
177 vcons_tty_param(struct tty *tp, struct termios *tio)
179 lwkt_gettoken(&tty_token);
180 tp->t_ispeed = tio->c_ispeed;
181 tp->t_ospeed = tio->c_ospeed;
182 tp->t_cflag = tio->c_cflag;
183 lwkt_reltoken(&tty_token);
188 vcons_tty_start(struct tty *tp)
193 lwkt_gettoken(&tty_token);
194 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
196 lwkt_reltoken(&tty_token);
199 tp->t_state |= TS_BUSY;
200 while ((n = q_to_b(&tp->t_outq, buf, sizeof(buf))) > 0) {
202 * Dummy up ttyv1, etc.
204 if (minor(tp->t_dev) == 0) {
205 pwrite(1, buf, n, -1);
208 tp->t_state &= ~TS_BUSY;
210 lwkt_reltoken(&tty_token);
215 vcons_intr(void *tpx, struct intrframe *frame __unused)
217 struct tty *tp = tpx;
218 unsigned char buf[32];
222 lwkt_gettoken(&tty_token);
224 * If we aren't open we only have synchronous traffic via the
225 * debugger and do not need to poll.
227 if ((tp->t_state & TS_ISOPEN) == 0) {
228 lwkt_reltoken(&tty_token);
233 * Only poll if we are open and haven't been stolen by the debugger.
235 if (console_stolen_by_kernel == 0 && (tp->t_state & TS_ISOPEN)) {
237 n = extpread(0, buf, sizeof(buf), O_FNONBLOCKING, -1LL);
238 for (i = 0; i < n; ++i)
239 (*linesw[tp->t_line].l_rint)(buf[i], tp);
242 lwkt_reltoken(&tty_token);
245 /************************************************************************
246 * KERNEL CONSOLE INTERFACE *
247 ************************************************************************
249 * Kernel direct-call interface console driver
251 static cn_probe_t vconsprobe;
252 static cn_init_t vconsinit;
253 static cn_init_fini_t vconsinit_fini;
254 static cn_term_t vconsterm;
255 static cn_getc_t vconsgetc;
256 static cn_checkc_t vconscheckc;
257 static cn_putc_t vconsputc;
259 CONS_DRIVER(vcons, vconsprobe, vconsinit, vconsinit_fini, vconsterm, vconsgetc,
260 vconscheckc, vconsputc, NULL);
262 static struct termios init_tio;
263 static struct consdev *vconsole;
266 vconsprobe(struct consdev *cp)
268 lwkt_gettoken(&tty_token);
269 cp->cn_pri = CN_NORMAL;
270 cp->cn_probegood = 1;
271 lwkt_reltoken(&tty_token);
275 * This is a little bulky handler to set proper terminal
276 * settings in the case of a signal which might lead to
277 * termination or suspension.
282 struct termios curtio;
283 struct sigaction sa, osa;
286 lwkt_gettoken(&tty_token);
287 tcgetattr(0, &curtio);
288 tcsetattr(0, TCSAFLUSH, &init_tio);
289 bzero(&sa, sizeof(sa));
290 sigemptyset(&sa.sa_mask);
291 sa.sa_handler = SIG_DFL;
292 sigaction(sig, &sa, &osa);
295 sigprocmask(SIG_UNBLOCK, &ss, &oss);
296 raise(sig); /* now hand down the sig */
297 sigprocmask(SIG_SETMASK, &oss, NULL);
298 sigaction(sig, &osa, NULL);
299 tcsetattr(0, TCSAFLUSH, &curtio);
300 lwkt_reltoken(&tty_token);
304 vconswinchsig(int __unused sig)
310 vconswinch_intr(void *arg __unused, void *frame __unused)
312 struct winsize newsize;
314 lwkt_gettoken(&tty_token);
315 if (vconsole != NULL && vconsole->cn_dev->si_tty != NULL) {
316 ioctl(0, TIOCGWINSZ, &newsize);
318 * ttioctl(vconsole->cn_dev->si_tty, TIOCSWINSZ, &newsize, 0);
319 * I wished. Unfortunately this needs a curproc, so do it
322 if (bcmp(&newsize, &vconsole->cn_dev->si_tty->t_winsize,
323 sizeof(newsize)) != 0) {
324 vconsole->cn_dev->si_tty->t_winsize = newsize;
325 pgsignal(vconsole->cn_dev->si_tty->t_pgrp, SIGWINCH, 1);
328 lwkt_reltoken(&tty_token);
335 * We might catch stray SIGIOs, so try hard.
337 lwkt_gettoken(&tty_token);
338 while (tcsetattr(0, TCSAFLUSH, &init_tio) != 0 && errno == EINTR)
340 lwkt_reltoken(&tty_token);
344 vconsinit(struct consdev *cp)
350 lwkt_gettoken(&tty_token);
351 tcgetattr(0, &init_tio);
352 bzero(&sa, sizeof(sa));
353 sigemptyset(&sa.sa_mask);
354 sa.sa_handler = vconssignal;
355 sigaction(SIGTSTP, &sa, NULL);
356 sigaction(SIGINT, &sa, NULL);
357 sigaction(SIGTERM, &sa, NULL);
358 atexit(vconscleanup);
360 lwkt_reltoken(&tty_token);
364 vconsinit_fini(struct consdev *cp)
371 * We have to do this here rather then in early boot to be able
372 * to use the interrupt subsystem.
374 register_int(3, vconswinch_intr, NULL, "swinch", NULL, 0);
375 bzero(&sa, sizeof(sa));
376 sigemptyset(&sa.sa_mask);
377 sa.sa_handler = vconswinchsig;
378 sigaction(SIGWINCH, &sa, NULL);
381 * Implement ttyv0-ttyv7. At the moment ttyv1-7 are sink nulls.
383 for (i = 0; i < 8; ++i) {
384 dev = make_dev(&vcons_ops, i,
385 UID_ROOT, GID_WHEEL, 0600, "ttyv%d", i);
390 EVENTHANDLER_REGISTER(shutdown_final, vconscleanup, NULL, SHUTDOWN_PRI_LAST);
394 vconsterm(struct consdev *vp)
401 vconsgetc(void *private)
406 lwkt_gettoken(&tty_token);
407 console_stolen_by_kernel = 1;
409 n = pread(0, &c, 1, -1);
412 if (n < 0 && errno == EINTR)
414 panic("vconsgetc: EOF on console %d %d", n ,errno);
416 console_stolen_by_kernel = 0;
417 lwkt_reltoken(&tty_token);
422 vconscheckc(void *private)
426 if (extpread(0, &c, 1, O_FNONBLOCKING, -1LL) == 1)
432 vconsputc(void *private, int c)
436 pwrite(1, &cc, 1, -1);
440 vcons_set_mode(int in_debugger)
444 lwkt_gettoken(&tty_token);
445 if (tcgetattr(0, &tio) < 0) {
446 lwkt_reltoken(&tty_token);
450 tio.c_oflag |= OPOST | ONLCR;
453 tio.c_cc[VINTR] = init_tio.c_cc[VINTR];
454 tio.c_cc[VSUSP] = init_tio.c_cc[VSUSP];
455 tio.c_cc[VSTATUS] = init_tio.c_cc[VSTATUS];
457 tio.c_cc[VINTR] = _POSIX_VDISABLE;
458 tio.c_cc[VSUSP] = _POSIX_VDISABLE;
459 tio.c_cc[VSTATUS] = _POSIX_VDISABLE;
461 tcsetattr(0, TCSAFLUSH, &tio);
462 lwkt_reltoken(&tty_token);