vkernel - Sync to recent API changes
[dragonfly.git] / sys / platform / vkernel64 / platform / console.c
blob1c1d4275f195cc442e227bc246ba13a6519b436d
1 /*
2 * (MPSAFE)
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
11 * are met:
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.
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.
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/conf.h>
41 #include <sys/cons.h>
42 #include <sys/tty.h>
43 #include <sys/fcntl.h>
44 #include <sys/signalvar.h>
45 #include <sys/eventhandler.h>
46 #include <sys/interrupt.h>
47 #include <sys/bus.h>
48 #include <machine/md_var.h>
49 #include <unistd.h>
50 #include <termios.h>
51 #include <stdlib.h>
53 static int console_stolen_by_kernel;
54 static struct kqueue_info *kqueue_console_info;
55 static struct tty *kqueue_console_tty;
57 /************************************************************************
58 * CONSOLE DEVICE *
59 ************************************************************************
63 static int vcons_tty_param(struct tty *tp, struct termios *tio);
64 static void vcons_tty_start(struct tty *tp);
65 static void vcons_hardintr(void *tpx, struct intrframe *frame __unused);
67 static d_open_t vcons_open;
68 static d_close_t vcons_close;
69 static d_ioctl_t vcons_ioctl;
71 static struct dev_ops vcons_ops = {
72 { "vcons", 0, D_TTY },
73 .d_open = vcons_open,
74 .d_close = vcons_close,
75 .d_read = ttyread,
76 .d_write = ttywrite,
77 .d_ioctl = vcons_ioctl,
78 .d_kqfilter = ttykqfilter
81 static int
82 vcons_open(struct dev_open_args *ap)
84 cdev_t dev = ap->a_head.a_dev;
85 struct tty *tp;
86 int error;
88 lwkt_gettoken(&tty_token);
89 tp = dev->si_tty = ttymalloc(dev->si_tty);
91 #define ISSET(t, f) ((t) & (f))
93 if ((tp->t_state & TS_ISOPEN) == 0) {
94 tp->t_oproc = vcons_tty_start;
95 tp->t_param = vcons_tty_param;
96 tp->t_stop = nottystop;
97 tp->t_dev = dev;
99 tp->t_state |= TS_CARR_ON | TS_CONNECTED;
100 ttychars(tp);
101 tp->t_iflag = TTYDEF_IFLAG;
102 tp->t_oflag = TTYDEF_OFLAG;
103 tp->t_cflag = TTYDEF_CFLAG;
104 tp->t_lflag = TTYDEF_LFLAG;
105 tp->t_ispeed = TTYDEF_SPEED;
106 tp->t_ospeed = TTYDEF_SPEED;
107 ttsetwater(tp);
109 if (minor(dev) == 0) {
110 error = (*linesw[tp->t_line].l_open)(dev, tp);
111 ioctl(0, TIOCGWINSZ, &tp->t_winsize);
113 if (kqueue_console_info == NULL) {
114 kqueue_console_tty = tp;
115 kqueue_console_info = kqueue_add(0, vcons_hardintr, tp);
117 } else {
118 /* dummy up other minors so the installer will run */
119 error = 0;
121 lwkt_reltoken(&tty_token);
122 return(error);
125 static int
126 vcons_close(struct dev_close_args *ap)
128 cdev_t dev = ap->a_head.a_dev;
129 struct tty *tp;
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);
139 static int
140 vcons_ioctl(struct dev_ioctl_args *ap)
142 cdev_t dev = ap->a_head.a_dev;
143 struct tty *tp;
144 int error;
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);
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);
159 lwkt_reltoken(&tty_token);
160 return (ENOTTY);
163 static int
164 vcons_tty_param(struct tty *tp, struct termios *tio)
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);
174 static void
175 vcons_tty_start(struct tty *tp)
177 int n;
178 char buf[64];
180 lwkt_gettoken(&tty_token);
181 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
182 ttwwakeup(tp);
183 lwkt_reltoken(&tty_token);
184 return;
186 tp->t_state |= TS_BUSY;
187 while ((n = q_to_b(&tp->t_outq, buf, sizeof(buf))) > 0) {
189 * Dummy up ttyv1, etc.
191 if (minor(tp->t_dev) == 0) {
192 pwrite(1, buf, n, -1);
195 tp->t_state &= ~TS_BUSY;
196 lwkt_reltoken(&tty_token);
197 ttwwakeup(tp);
200 static
201 void
202 vcons_hardintr(void *tpx, struct intrframe *frame __unused)
204 if (console_stolen_by_kernel == 0)
205 signalintr(4);
208 /************************************************************************
209 * KERNEL CONSOLE INTERFACE *
210 ************************************************************************
212 * Kernel direct-call interface console driver
214 static cn_probe_t vconsprobe;
215 static cn_init_t vconsinit;
216 static cn_init_fini_t vconsinit_fini;
217 static cn_term_t vconsterm;
218 static cn_getc_t vconsgetc;
219 static cn_checkc_t vconscheckc;
220 static cn_putc_t vconsputc;
222 CONS_DRIVER(vcons, vconsprobe, vconsinit, vconsinit_fini, vconsterm, vconsgetc,
223 vconscheckc, vconsputc, NULL, NULL);
225 static struct termios init_tio;
226 static struct consdev *vconsole;
228 static void
229 vconsprobe(struct consdev *cp)
231 cp->cn_pri = CN_NORMAL;
232 cp->cn_probegood = 1;
236 * This is a little bulky handler to set proper terminal
237 * settings in the case of a signal which might lead to
238 * termination or suspension.
240 static void
241 vconssignal(int sig)
243 struct termios curtio;
244 struct sigaction sa, osa;
245 sigset_t ss, oss;
247 tcgetattr(0, &curtio);
248 tcsetattr(0, TCSAFLUSH, &init_tio);
249 bzero(&sa, sizeof(sa));
250 sigemptyset(&sa.sa_mask);
251 sa.sa_handler = SIG_DFL;
252 sigaction(sig, &sa, &osa);
253 sigemptyset(&ss);
254 sigaddset(&ss, sig);
255 sigprocmask(SIG_UNBLOCK, &ss, &oss);
256 raise(sig); /* now hand down the sig */
257 sigprocmask(SIG_SETMASK, &oss, NULL);
258 sigaction(sig, &osa, NULL);
259 tcsetattr(0, TCSAFLUSH, &curtio);
262 static void
263 vconswinchsig(int __unused sig)
265 signalintr(3);
268 static void
269 vconswinch_intr(void *arg __unused, void *frame __unused)
271 struct winsize newsize;
273 if (vconsole != NULL && vconsole->cn_dev->si_tty != NULL) {
274 ioctl(0, TIOCGWINSZ, &newsize);
276 * ttioctl(vconsole->cn_dev->si_tty, TIOCSWINSZ, &newsize, 0);
277 * I wished. Unfortunately this needs a curproc, so do it
278 * manually.
280 if (bcmp(&newsize, &vconsole->cn_dev->si_tty->t_winsize,
281 sizeof(newsize)) != 0) {
282 vconsole->cn_dev->si_tty->t_winsize = newsize;
283 pgsignal(vconsole->cn_dev->si_tty->t_pgrp, SIGWINCH, 1);
289 * This has to be an interrupt thread and not a hard interrupt.
291 static
292 void
293 vconsvirt_intr(void *arg __unused, void *frame __unused)
295 struct tty *tp;
296 unsigned char buf[32];
297 int i;
298 int n;
300 if (kqueue_console_info == NULL)
301 return;
302 tp = kqueue_console_tty;
304 lwkt_gettoken(&tty_token);
306 * If we aren't open we only have synchronous traffic via the
307 * debugger and do not need to poll.
309 if ((tp->t_state & TS_ISOPEN) == 0) {
310 lwkt_reltoken(&tty_token);
311 return;
315 * Only poll if we are open and haven't been stolen by the debugger.
317 if (console_stolen_by_kernel == 0 && (tp->t_state & TS_ISOPEN)) {
318 do {
319 n = extpread(0, buf, sizeof(buf), O_FNONBLOCKING, -1LL);
320 for (i = 0; i < n; ++i)
321 (*linesw[tp->t_line].l_rint)(buf[i], tp);
322 } while (n > 0);
324 lwkt_reltoken(&tty_token);
329 static void
330 vconscleanup(void)
333 * We might catch stray SIGIOs, so try hard.
335 while (tcsetattr(0, TCSAFLUSH, &init_tio) != 0 && errno == EINTR)
336 /* NOTHING */;
339 static void
340 vconsinit(struct consdev *cp)
342 struct sigaction sa;
344 vconsole = cp;
346 tcgetattr(0, &init_tio);
347 bzero(&sa, sizeof(sa));
348 sigemptyset(&sa.sa_mask);
349 sa.sa_handler = vconssignal;
350 sigaction(SIGTSTP, &sa, NULL);
351 sigaction(SIGINT, &sa, NULL);
352 sigaction(SIGTERM, &sa, NULL);
353 atexit(vconscleanup);
354 vcons_set_mode(0);
357 static void
358 vconsinit_fini(struct consdev *cp)
360 struct sigaction sa;
361 cdev_t dev;
362 int i;
365 * We have to do this here rather then in early boot to be able
366 * to use the interrupt subsystem.
368 register_int_virtual(3, vconswinch_intr, NULL, "swinch", NULL,
369 INTR_MPSAFE);
370 register_int_virtual(4, vconsvirt_intr, NULL, "vintr", NULL,
371 INTR_MPSAFE);
372 bzero(&sa, sizeof(sa));
373 sigemptyset(&sa.sa_mask);
374 sa.sa_handler = vconswinchsig;
375 sigaction(SIGWINCH, &sa, NULL);
378 * Implement ttyv0-ttyv7. At the moment ttyv1-7 are sink nulls.
380 for (i = 0; i < 8; ++i) {
381 dev = make_dev(&vcons_ops, i,
382 UID_ROOT, GID_WHEEL, 0600, "ttyv%d", i);
383 if (i == 0) {
384 cp->cn_dev = dev;
387 EVENTHANDLER_REGISTER(shutdown_final, vconscleanup, NULL, SHUTDOWN_PRI_LAST);
390 static void
391 vconsterm(struct consdev *vp)
393 vconsole = NULL;
394 vconscleanup();
397 static int
398 vconsgetc(void *private)
400 unsigned char c;
401 ssize_t n;
403 console_stolen_by_kernel = 1;
404 for (;;) {
405 n = pread(0, &c, 1, -1);
406 if (n == 1)
407 break;
408 if (n < 0 && errno == EINTR)
409 continue;
410 panic("vconsgetc: EOF on console %jd %d", (intmax_t)n, errno);
412 console_stolen_by_kernel = 0;
413 return((int)c);
416 static int
417 vconscheckc(void *private)
419 unsigned char c;
421 if (extpread(0, &c, 1, O_FNONBLOCKING, -1LL) == 1)
422 return((int)c);
423 return(-1);
426 static void
427 vconsputc(void *private, int c)
429 char cc = c;
431 pwrite(1, &cc, 1, -1);
434 void
435 vcons_set_mode(int in_debugger)
437 struct termios tio;
439 if (tcgetattr(0, &tio) < 0) {
440 return;
442 cfmakeraw(&tio);
443 tio.c_oflag |= OPOST | ONLCR;
444 tio.c_lflag |= ISIG;
445 if (in_debugger) {
446 tio.c_cc[VINTR] = init_tio.c_cc[VINTR];
447 tio.c_cc[VSUSP] = init_tio.c_cc[VSUSP];
448 tio.c_cc[VSTATUS] = init_tio.c_cc[VSTATUS];
449 } else {
450 tio.c_cc[VINTR] = _POSIX_VDISABLE;
451 tio.c_cc[VSUSP] = _POSIX_VDISABLE;
452 tio.c_cc[VSTATUS] = _POSIX_VDISABLE;
454 tcsetattr(0, TCSAFLUSH, &tio);