MPSAFE TTY - get rid of tokens in console path
[dragonfly.git] / sys / platform / vkernel64 / 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         lwkt_reltoken(&tty_token);
210         ttwwakeup(tp);
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         cp->cn_pri = CN_NORMAL;
269         cp->cn_probegood = 1;
270 }
271
272 /*
273  * This is a little bulky handler to set proper terminal
274  * settings in the case of a signal which might lead to
275  * termination or suspension.
276  */
277 static void
278 vconssignal(int sig)
279 {
280         struct termios curtio;
281         struct sigaction sa, osa;
282         sigset_t ss, oss;
283
284         tcgetattr(0, &curtio);
285         tcsetattr(0, TCSAFLUSH, &init_tio);
286         bzero(&sa, sizeof(sa));
287         sigemptyset(&sa.sa_mask);
288         sa.sa_handler = SIG_DFL;
289         sigaction(sig, &sa, &osa);
290         sigemptyset(&ss);
291         sigaddset(&ss, sig);
292         sigprocmask(SIG_UNBLOCK, &ss, &oss);
293         raise(sig);     /* now hand down the sig */
294         sigprocmask(SIG_SETMASK, &oss, NULL);
295         sigaction(sig, &osa, NULL);
296         tcsetattr(0, TCSAFLUSH, &curtio);
297 }
298
299 static void
300 vconswinchsig(int __unused sig)
301 {
302         signalintr(3);
303 }
304
305 static void
306 vconswinch_intr(void *arg __unused, void *frame __unused)
307 {
308         struct winsize newsize;
309
310         if (vconsole != NULL && vconsole->cn_dev->si_tty != NULL) {
311                 ioctl(0, TIOCGWINSZ, &newsize);
312                 /*
313                  * ttioctl(vconsole->cn_dev->si_tty, TIOCSWINSZ, &newsize, 0);
314                  * I wished.  Unfortunately this needs a curproc, so do it
315                  * manually.
316                  */
317                 if (bcmp(&newsize, &vconsole->cn_dev->si_tty->t_winsize,
318                          sizeof(newsize)) != 0) {
319                         vconsole->cn_dev->si_tty->t_winsize = newsize;
320                         pgsignal(vconsole->cn_dev->si_tty->t_pgrp, SIGWINCH, 1);
321                 }
322         }
323 }
324
325 static void
326 vconscleanup(void)
327 {
328         /*
329          * We might catch stray SIGIOs, so try hard.
330          */
331         while (tcsetattr(0, TCSAFLUSH, &init_tio) != 0 && errno == EINTR)
332                 /* NOTHING */;
333 }
334
335 static void
336 vconsinit(struct consdev *cp)
337 {
338         struct sigaction sa;
339
340         vconsole = cp;
341
342         tcgetattr(0, &init_tio);
343         bzero(&sa, sizeof(sa));
344         sigemptyset(&sa.sa_mask);
345         sa.sa_handler = vconssignal;
346         sigaction(SIGTSTP, &sa, NULL);
347         sigaction(SIGINT, &sa, NULL);
348         sigaction(SIGTERM, &sa, NULL);
349         atexit(vconscleanup);
350         vcons_set_mode(0);
351 }
352
353 static void
354 vconsinit_fini(struct consdev *cp)
355 {
356         struct sigaction sa;
357         cdev_t dev;
358         int i;
359
360         /*
361          * We have to do this here rather then in early boot to be able
362          * to use the interrupt subsystem.
363          */
364         register_int(3, vconswinch_intr, NULL, "swinch", NULL, 0);
365         bzero(&sa, sizeof(sa));
366         sigemptyset(&sa.sa_mask);
367         sa.sa_handler = vconswinchsig;
368         sigaction(SIGWINCH, &sa, NULL);
369
370         /*
371          * Implement ttyv0-ttyv7.  At the moment ttyv1-7 are sink nulls.
372          */
373         for (i = 0; i < 8; ++i) {
374                 dev = make_dev(&vcons_ops, i,
375                                UID_ROOT, GID_WHEEL, 0600, "ttyv%d", i);
376                 if (i == 0) {
377                         cp->cn_dev = dev;
378                 }
379         }
380         EVENTHANDLER_REGISTER(shutdown_final, vconscleanup, NULL, SHUTDOWN_PRI_LAST);
381 }
382
383 static void
384 vconsterm(struct consdev *vp)
385 {
386         vconsole = NULL;
387         vconscleanup();
388 }
389
390 static int
391 vconsgetc(void *private)
392 {
393         unsigned char c;
394         ssize_t n;
395
396         console_stolen_by_kernel = 1;
397         for (;;) {
398                 n = pread(0, &c, 1, -1);
399                 if (n == 1)
400                         break;
401                 if (n < 0 && errno == EINTR)
402                         continue;
403                 panic("vconsgetc: EOF on console %jd %d", (intmax_t)n, errno);
404         }
405         console_stolen_by_kernel = 0;
406         return((int)c);
407 }
408
409 static int
410 vconscheckc(void *private)
411 {
412         unsigned char c;
413
414         if (extpread(0, &c, 1, O_FNONBLOCKING, -1LL) == 1)
415                 return((int)c);
416         return(-1);
417 }
418
419 static void
420 vconsputc(void *private, int c)
421 {
422         char cc = c;
423
424         pwrite(1, &cc, 1, -1);
425 }
426
427 void
428 vcons_set_mode(int in_debugger)
429 {
430         struct termios tio;
431
432         if (tcgetattr(0, &tio) < 0) {
433                 return;
434         }
435         cfmakeraw(&tio);
436         tio.c_oflag |= OPOST | ONLCR;
437         tio.c_lflag |= ISIG;
438         if (in_debugger) {
439                 tio.c_cc[VINTR] = init_tio.c_cc[VINTR];
440                 tio.c_cc[VSUSP] = init_tio.c_cc[VSUSP];
441                 tio.c_cc[VSTATUS] = init_tio.c_cc[VSTATUS];
442         } else {
443                 tio.c_cc[VINTR] = _POSIX_VDISABLE;
444                 tio.c_cc[VSUSP] = _POSIX_VDISABLE;
445                 tio.c_cc[VSTATUS] = _POSIX_VDISABLE;
446         }
447         tcsetattr(0, TCSAFLUSH, &tio);
448 }