Merge branch 'vendor/BZIP'
[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  *                          CONSOLE DEVICE                              *
59  ************************************************************************
60  *
61  */
62
63 #define CDEV_MAJOR      12      /* steal the same major as /dev/ttyv* */
64
65 static int vcons_tty_param(struct tty *tp, struct termios *tio);
66 static void vcons_tty_start(struct tty *tp);
67 static void vcons_intr(void *tpx, struct intrframe *frame __unused);
68
69 static d_open_t         vcons_open;
70 static d_close_t        vcons_close;
71 static d_ioctl_t        vcons_ioctl;
72
73 static struct dev_ops vcons_ops = {
74         { "vcons", CDEV_MAJOR, D_TTY },
75         .d_open =       vcons_open,
76         .d_close =      vcons_close,
77         .d_read =       ttyread,
78         .d_write =      ttywrite,
79         .d_ioctl =      vcons_ioctl,
80         .d_kqfilter =   ttykqfilter
81 };
82
83 static int
84 vcons_open(struct dev_open_args *ap)
85 {
86         cdev_t dev = ap->a_head.a_dev;
87         struct tty *tp;
88         int error;
89
90         lwkt_gettoken(&tty_token);
91         tp = dev->si_tty = ttymalloc(dev->si_tty);
92
93 #define ISSET(t, f)     ((t) & (f))
94
95         if ((tp->t_state & TS_ISOPEN) == 0) {
96                 tp->t_oproc = vcons_tty_start;
97                 tp->t_param = vcons_tty_param;
98                 tp->t_stop = nottystop;
99                 tp->t_dev = dev;
100
101                 tp->t_state |= TS_CARR_ON | TS_CONNECTED;
102                 ttychars(tp);
103                 tp->t_iflag = TTYDEF_IFLAG;
104                 tp->t_oflag = TTYDEF_OFLAG;
105                 tp->t_cflag = TTYDEF_CFLAG;
106                 tp->t_lflag = TTYDEF_LFLAG;
107                 tp->t_ispeed = TTYDEF_SPEED;
108                 tp->t_ospeed = TTYDEF_SPEED;
109                 ttsetwater(tp);
110         }
111         if (minor(dev) == 0) {
112                 error = (*linesw[tp->t_line].l_open)(dev, tp);
113                 ioctl(0, TIOCGWINSZ, &tp->t_winsize);
114
115                 if (kqueue_console_info == NULL)
116                         kqueue_console_info = kqueue_add(0, vcons_intr, tp);
117         } else {
118                 /* dummy up other minors so the installer will run */
119                 error = 0;
120         }
121         lwkt_reltoken(&tty_token);
122         return(error);
123 }
124
125 static int
126 vcons_close(struct dev_close_args *ap)
127 {
128         cdev_t dev = ap->a_head.a_dev;
129         struct tty *tp;
130
131         lwkt_gettoken(&tty_token);
132         tp = dev->si_tty;
133         (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
134         ttyclose(tp);
135         lwkt_reltoken(&tty_token);
136         return(0);
137 }
138
139 static int
140 vcons_ioctl(struct dev_ioctl_args *ap)
141 {
142         cdev_t dev = ap->a_head.a_dev;
143         struct tty *tp;
144         int error;
145
146         lwkt_gettoken(&tty_token);
147         tp = dev->si_tty;
148         error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
149                                               ap->a_fflag, ap->a_cred);
150         if (error != ENOIOCTL) {
151                 lwkt_reltoken(&tty_token);
152                 return (error);
153         }
154         error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
155         if (error != ENOIOCTL) {
156                 lwkt_reltoken(&tty_token);
157                 return (error);
158         }
159         lwkt_reltoken(&tty_token);
160         return (ENOTTY);
161 }
162
163 static int
164 vcons_tty_param(struct tty *tp, struct termios *tio)
165 {
166         lwkt_gettoken(&tty_token);
167         tp->t_ispeed = tio->c_ispeed;
168         tp->t_ospeed = tio->c_ospeed;
169         tp->t_cflag = tio->c_cflag;
170         lwkt_reltoken(&tty_token);
171         return(0);
172 }
173
174 static void
175 vcons_tty_start(struct tty *tp)
176 {
177         int n;
178         char buf[64];
179
180         lwkt_gettoken(&tty_token);
181         if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
182                 ttwwakeup(tp);
183                 lwkt_reltoken(&tty_token);
184                 return;
185         }
186         tp->t_state |= TS_BUSY;
187         while ((n = q_to_b(&tp->t_outq, buf, sizeof(buf))) > 0) {
188                 /*
189                  * Dummy up ttyv1, etc.
190                  */
191                 if (minor(tp->t_dev) == 0) {
192                         pwrite(1, buf, n, -1);
193                 }
194         }
195         tp->t_state &= ~TS_BUSY;
196         lwkt_reltoken(&tty_token);
197         ttwwakeup(tp);
198 }
199
200 static
201 void
202 vcons_intr(void *tpx, struct intrframe *frame __unused)
203 {
204         struct tty *tp = tpx;
205         unsigned char buf[32];
206         int i;
207         int n;
208
209         lwkt_gettoken(&tty_token);
210         /*
211          * If we aren't open we only have synchronous traffic via the
212          * debugger and do not need to poll.
213          */
214         if ((tp->t_state & TS_ISOPEN) == 0) {
215                 lwkt_reltoken(&tty_token);
216                 return;
217         }
218
219         /*
220          * Only poll if we are open and haven't been stolen by the debugger.
221          */
222         if (console_stolen_by_kernel == 0 && (tp->t_state & TS_ISOPEN)) {
223                 do {
224                         n = extpread(0, buf, sizeof(buf), O_FNONBLOCKING, -1LL);
225                         for (i = 0; i < n; ++i)
226                                 (*linesw[tp->t_line].l_rint)(buf[i], tp);
227                 } while (n > 0);
228         }
229         lwkt_reltoken(&tty_token);
230 }
231
232 /************************************************************************
233  *                      KERNEL CONSOLE INTERFACE                        *
234  ************************************************************************
235  *
236  * Kernel direct-call interface console driver
237  */
238 static cn_probe_t       vconsprobe;
239 static cn_init_t        vconsinit;
240 static cn_init_fini_t   vconsinit_fini;
241 static cn_term_t        vconsterm;
242 static cn_getc_t        vconsgetc;
243 static cn_checkc_t      vconscheckc;
244 static cn_putc_t        vconsputc;
245
246 CONS_DRIVER(vcons, vconsprobe, vconsinit, vconsinit_fini, vconsterm, vconsgetc,
247                 vconscheckc, vconsputc, NULL);
248
249 static struct termios init_tio;
250 static struct consdev *vconsole;
251
252 static void
253 vconsprobe(struct consdev *cp)
254 {
255         cp->cn_pri = CN_NORMAL;
256         cp->cn_probegood = 1;
257 }
258
259 /*
260  * This is a little bulky handler to set proper terminal
261  * settings in the case of a signal which might lead to
262  * termination or suspension.
263  */
264 static void
265 vconssignal(int sig)
266 {
267         struct termios curtio;
268         struct sigaction sa, osa;
269         sigset_t ss, oss;
270
271         tcgetattr(0, &curtio);
272         tcsetattr(0, TCSAFLUSH, &init_tio);
273         bzero(&sa, sizeof(sa));
274         sigemptyset(&sa.sa_mask);
275         sa.sa_handler = SIG_DFL;
276         sigaction(sig, &sa, &osa);
277         sigemptyset(&ss);
278         sigaddset(&ss, sig);
279         sigprocmask(SIG_UNBLOCK, &ss, &oss);
280         raise(sig);     /* now hand down the sig */
281         sigprocmask(SIG_SETMASK, &oss, NULL);
282         sigaction(sig, &osa, NULL);
283         tcsetattr(0, TCSAFLUSH, &curtio);
284 }
285
286 static void
287 vconswinchsig(int __unused sig)
288 {
289         signalintr(3);
290 }
291
292 static void
293 vconswinch_intr(void *arg __unused, void *frame __unused)
294 {
295         struct winsize newsize;
296
297         if (vconsole != NULL && vconsole->cn_dev->si_tty != NULL) {
298                 ioctl(0, TIOCGWINSZ, &newsize);
299                 /*
300                  * ttioctl(vconsole->cn_dev->si_tty, TIOCSWINSZ, &newsize, 0);
301                  * I wished.  Unfortunately this needs a curproc, so do it
302                  * manually.
303                  */
304                 if (bcmp(&newsize, &vconsole->cn_dev->si_tty->t_winsize,
305                          sizeof(newsize)) != 0) {
306                         vconsole->cn_dev->si_tty->t_winsize = newsize;
307                         pgsignal(vconsole->cn_dev->si_tty->t_pgrp, SIGWINCH, 1);
308                 }
309         }
310 }
311
312 static void
313 vconscleanup(void)
314 {
315         /*
316          * We might catch stray SIGIOs, so try hard.
317          */
318         while (tcsetattr(0, TCSAFLUSH, &init_tio) != 0 && errno == EINTR)
319                 /* NOTHING */;
320 }
321
322 static void
323 vconsinit(struct consdev *cp)
324 {
325         struct sigaction sa;
326
327         vconsole = cp;
328
329         tcgetattr(0, &init_tio);
330         bzero(&sa, sizeof(sa));
331         sigemptyset(&sa.sa_mask);
332         sa.sa_handler = vconssignal;
333         sigaction(SIGTSTP, &sa, NULL);
334         sigaction(SIGINT, &sa, NULL);
335         sigaction(SIGTERM, &sa, NULL);
336         atexit(vconscleanup);
337         vcons_set_mode(0);
338 }
339
340 static void
341 vconsinit_fini(struct consdev *cp)
342 {
343         struct sigaction sa;
344         cdev_t dev;
345         int i;
346
347         /*
348          * We have to do this here rather then in early boot to be able
349          * to use the interrupt subsystem.
350          */
351         register_int(3, vconswinch_intr, NULL, "swinch", NULL, 0);
352         bzero(&sa, sizeof(sa));
353         sigemptyset(&sa.sa_mask);
354         sa.sa_handler = vconswinchsig;
355         sigaction(SIGWINCH, &sa, NULL);
356
357         /*
358          * Implement ttyv0-ttyv7.  At the moment ttyv1-7 are sink nulls.
359          */
360         for (i = 0; i < 8; ++i) {
361                 dev = make_dev(&vcons_ops, i,
362                                UID_ROOT, GID_WHEEL, 0600, "ttyv%d", i);
363                 if (i == 0) {
364                         cp->cn_dev = dev;
365                 }
366         }
367         EVENTHANDLER_REGISTER(shutdown_final, vconscleanup, NULL, SHUTDOWN_PRI_LAST);
368 }
369
370 static void
371 vconsterm(struct consdev *vp)
372 {
373         vconsole = NULL;
374         vconscleanup();
375 }
376
377 static int
378 vconsgetc(void *private)
379 {
380         unsigned char c;
381         ssize_t n;
382
383         console_stolen_by_kernel = 1;
384         for (;;) {
385                 n = pread(0, &c, 1, -1);
386                 if (n == 1)
387                         break;
388                 if (n < 0 && errno == EINTR)
389                         continue;
390                 panic("vconsgetc: EOF on console %jd %d", (intmax_t)n, errno);
391         }
392         console_stolen_by_kernel = 0;
393         return((int)c);
394 }
395
396 static int
397 vconscheckc(void *private)
398 {
399         unsigned char c;
400
401         if (extpread(0, &c, 1, O_FNONBLOCKING, -1LL) == 1)
402                 return((int)c);
403         return(-1);
404 }
405
406 static void
407 vconsputc(void *private, int c)
408 {
409         char cc = c;
410
411         pwrite(1, &cc, 1, -1);
412 }
413
414 void
415 vcons_set_mode(int in_debugger)
416 {
417         struct termios tio;
418
419         if (tcgetattr(0, &tio) < 0) {
420                 return;
421         }
422         cfmakeraw(&tio);
423         tio.c_oflag |= OPOST | ONLCR;
424         tio.c_lflag |= ISIG;
425         if (in_debugger) {
426                 tio.c_cc[VINTR] = init_tio.c_cc[VINTR];
427                 tio.c_cc[VSUSP] = init_tio.c_cc[VSUSP];
428                 tio.c_cc[VSTATUS] = init_tio.c_cc[VSTATUS];
429         } else {
430                 tio.c_cc[VINTR] = _POSIX_VDISABLE;
431                 tio.c_cc[VSUSP] = _POSIX_VDISABLE;
432                 tio.c_cc[VSTATUS] = _POSIX_VDISABLE;
433         }
434         tcsetattr(0, TCSAFLUSH, &tio);
435 }