MPSAFE - TTY & related drivers
[dragonfly.git] / sys / platform / vkernel / platform / console.c
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 2006 The DragonFly Project.  All rights reserved.
5  * 
6  * This code is derived from software contributed to The DragonFly Project
7  * by Matthew Dillon <dillon@backplane.com>
8  * 
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 
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
18  *    distribution.
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.
22  * 
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
34  * SUCH DAMAGE.
35  * 
36  * $DragonFly: src/sys/platform/vkernel/platform/console.c,v 1.17 2007/07/02 04:19:14 dillon Exp $
37  */
38
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/conf.h>
42 #include <sys/cons.h>
43 #include <sys/tty.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>
50 #include <unistd.h>
51 #include <termios.h>
52 #include <stdlib.h>
53
54 static int console_stolen_by_kernel;
55 static struct kqueue_info *kqueue_console_info;
56
57 /*
58  * Global console locking functions
59  */
60 void
61 cons_lock(void)
62 {
63 }
64
65 void
66 cons_unlock(void)
67 {
68 }
69
70 /************************************************************************
71  *                          CONSOLE DEVICE                              *
72  ************************************************************************
73  *
74  */
75
76 #define CDEV_MAJOR      12      /* steal the same major as /dev/ttyv* */
77
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);
81
82 static d_open_t         vcons_open;
83 static d_close_t        vcons_close;
84 static d_ioctl_t        vcons_ioctl;
85
86 static struct dev_ops vcons_ops = {
87         { "vcons", CDEV_MAJOR, D_TTY },
88         .d_open =       vcons_open,
89         .d_close =      vcons_close,
90         .d_read =       ttyread,
91         .d_write =      ttywrite,
92         .d_ioctl =      vcons_ioctl,
93         .d_kqfilter =   ttykqfilter,
94 };
95
96 static int
97 vcons_open(struct dev_open_args *ap)
98 {
99         cdev_t dev = ap->a_head.a_dev;
100         struct tty *tp;
101         int error;
102
103         lwkt_gettoken(&tty_token);
104         tp = dev->si_tty = ttymalloc(dev->si_tty);
105
106 #define ISSET(t, f)     ((t) & (f))
107
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;
112                 tp->t_dev = dev;
113
114                 tp->t_state |= TS_CARR_ON | TS_CONNECTED;
115                 ttychars(tp);
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;
122                 ttsetwater(tp);
123         }
124         if (minor(dev) == 0) {
125                 error = (*linesw[tp->t_line].l_open)(dev, tp);
126                 ioctl(0, TIOCGWINSZ, &tp->t_winsize);
127
128                 if (kqueue_console_info == NULL)
129                         kqueue_console_info = kqueue_add(0, vcons_intr, tp);
130         } else {
131                 /* dummy up other minors so the installer will run */
132                 error = 0;
133         }
134         lwkt_reltoken(&tty_token);
135         return(error);
136 }
137
138 static int
139 vcons_close(struct dev_close_args *ap)
140 {
141         cdev_t dev = ap->a_head.a_dev;
142         struct tty *tp;
143
144         lwkt_gettoken(&tty_token);
145         tp = dev->si_tty;
146         (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
147         ttyclose(tp);
148         lwkt_reltoken(&tty_token);
149         return(0);
150 }
151
152 static int
153 vcons_ioctl(struct dev_ioctl_args *ap)
154 {
155         cdev_t dev = ap->a_head.a_dev;
156         struct tty *tp;
157         int error;
158
159         lwkt_gettoken(&tty_token);
160         tp = dev->si_tty;
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);
165                 return (error);
166         }
167         error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
168         if (error != ENOIOCTL) {
169                 lwkt_reltoken(&tty_token);
170                 return (error);
171         }
172         lwkt_reltoken(&tty_token);
173         return (ENOTTY);
174 }
175
176 static int
177 vcons_tty_param(struct tty *tp, struct termios *tio)
178 {
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);
184         return(0);
185 }
186
187 static void
188 vcons_tty_start(struct tty *tp)
189 {
190         int n;
191         char buf[64];
192
193         lwkt_gettoken(&tty_token);
194         if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
195                 ttwwakeup(tp);
196                 lwkt_reltoken(&tty_token);
197                 return;
198         }
199         tp->t_state |= TS_BUSY;
200         while ((n = q_to_b(&tp->t_outq, buf, sizeof(buf))) > 0) {
201                 /*
202                  * Dummy up ttyv1, etc.
203                  */
204                 if (minor(tp->t_dev) == 0) {
205                         pwrite(1, buf, n, -1);
206                 }
207         }
208         tp->t_state &= ~TS_BUSY;
209         ttwwakeup(tp);
210         lwkt_reltoken(&tty_token);
211 }
212
213 static
214 void
215 vcons_intr(void *tpx, struct intrframe *frame __unused)
216 {
217         struct tty *tp = tpx;
218         unsigned char buf[32];
219         int i;
220         int n;
221
222         lwkt_gettoken(&tty_token);
223         /*
224          * If we aren't open we only have synchronous traffic via the
225          * debugger and do not need to poll.
226          */
227         if ((tp->t_state & TS_ISOPEN) == 0) {
228                 lwkt_reltoken(&tty_token);
229                 return;
230         }
231
232         /*
233          * Only poll if we are open and haven't been stolen by the debugger.
234          */
235         if (console_stolen_by_kernel == 0 && (tp->t_state & TS_ISOPEN)) {
236                 do {
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);
240                 } while (n > 0);
241         }
242         lwkt_reltoken(&tty_token);
243 }
244
245 /************************************************************************
246  *                      KERNEL CONSOLE INTERFACE                        *
247  ************************************************************************
248  *
249  * Kernel direct-call interface console driver
250  */
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;
258
259 CONS_DRIVER(vcons, vconsprobe, vconsinit, vconsinit_fini, vconsterm, vconsgetc, 
260                 vconscheckc, vconsputc, NULL);
261
262 static struct termios init_tio;
263 static struct consdev *vconsole;
264
265 static void
266 vconsprobe(struct consdev *cp)
267 {
268         lwkt_gettoken(&tty_token);
269         cp->cn_pri = CN_NORMAL;
270         cp->cn_probegood = 1;
271         lwkt_reltoken(&tty_token);
272 }
273
274 /*
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.
278  */
279 static void
280 vconssignal(int sig)
281 {
282         struct termios curtio;
283         struct sigaction sa, osa;
284         sigset_t ss, oss;
285
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);
293         sigemptyset(&ss);
294         sigaddset(&ss, sig);
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);
301 }
302
303 static void
304 vconswinchsig(int __unused sig)
305 {
306         signalintr(3);
307 }
308
309 static void
310 vconswinch_intr(void *arg __unused, void *frame __unused)
311 {
312         struct winsize newsize;
313
314         lwkt_gettoken(&tty_token);
315         if (vconsole != NULL && vconsole->cn_dev->si_tty != NULL) {
316                 ioctl(0, TIOCGWINSZ, &newsize);
317                 /*
318                  * ttioctl(vconsole->cn_dev->si_tty, TIOCSWINSZ, &newsize, 0);
319                  * I wished.  Unfortunately this needs a curproc, so do it
320                  * manually.
321                  */
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);
326                 }
327         }
328         lwkt_reltoken(&tty_token);
329 }
330
331 static void
332 vconscleanup(void)
333 {
334         /*
335          * We might catch stray SIGIOs, so try hard.
336          */
337         lwkt_gettoken(&tty_token);
338         while (tcsetattr(0, TCSAFLUSH, &init_tio) != 0 && errno == EINTR)
339                 /* NOTHING */;
340         lwkt_reltoken(&tty_token);
341 }
342
343 static void
344 vconsinit(struct consdev *cp)
345 {
346         struct sigaction sa;
347
348         vconsole = cp;
349
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);
359         vcons_set_mode(0);
360         lwkt_reltoken(&tty_token);
361 }
362
363 static void
364 vconsinit_fini(struct consdev *cp)
365 {
366         struct sigaction sa;
367         cdev_t dev;
368         int i;
369
370         /*
371          * We have to do this here rather then in early boot to be able
372          * to use the interrupt subsystem.
373          */
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);
379
380         /*
381          * Implement ttyv0-ttyv7.  At the moment ttyv1-7 are sink nulls.
382          */
383         for (i = 0; i < 8; ++i) {
384                 dev = make_dev(&vcons_ops, i,
385                                UID_ROOT, GID_WHEEL, 0600, "ttyv%d", i);
386                 if (i == 0) {
387                         cp->cn_dev = dev;
388                 }
389         }
390         EVENTHANDLER_REGISTER(shutdown_final, vconscleanup, NULL, SHUTDOWN_PRI_LAST);
391 }
392
393 static void
394 vconsterm(struct consdev *vp)
395 {
396         vconsole = NULL;
397         vconscleanup();
398 }
399
400 static int
401 vconsgetc(void *private)
402 {
403         unsigned char c;
404         ssize_t n;
405
406         lwkt_gettoken(&tty_token);
407         console_stolen_by_kernel = 1;
408         for (;;) {
409                 n = pread(0, &c, 1, -1);
410                 if (n == 1)
411                         break;
412                 if (n < 0 && errno == EINTR)
413                         continue;
414                 panic("vconsgetc: EOF on console %d %d", n ,errno);
415         }
416         console_stolen_by_kernel = 0;
417         lwkt_reltoken(&tty_token);
418         return((int)c);
419 }
420
421 static int
422 vconscheckc(void *private)
423 {
424         unsigned char c;
425
426         if (extpread(0, &c, 1, O_FNONBLOCKING, -1LL) == 1)
427                 return((int)c);
428         return(-1);
429 }
430
431 static void
432 vconsputc(void *private, int c)
433 {
434         char cc = c;
435
436         pwrite(1, &cc, 1, -1);
437 }
438
439 void
440 vcons_set_mode(int in_debugger)
441 {
442         struct termios tio;
443
444         lwkt_gettoken(&tty_token);
445         if (tcgetattr(0, &tio) < 0) {
446                 lwkt_reltoken(&tty_token);
447                 return;
448         }
449         cfmakeraw(&tio);
450         tio.c_oflag |= OPOST | ONLCR;
451         tio.c_lflag |= ISIG;
452         if (in_debugger) {
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];
456         } else {
457                 tio.c_cc[VINTR] = _POSIX_VDISABLE;
458                 tio.c_cc[VSUSP] = _POSIX_VDISABLE;
459                 tio.c_cc[VSTATUS] = _POSIX_VDISABLE;
460         }
461         tcsetattr(0, TCSAFLUSH, &tio);
462         lwkt_reltoken(&tty_token);
463 }