xref: /dragonfly/sys/dev/misc/dcons/dcons_os.c (revision 2b3f93ea6d1f70880f3e87f3c2cbe0dc0bfc9332)
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (C) 2003,2004
5  *        Hidetoshi Shimokawa. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *
18  *        This product includes software developed by Hidetoshi Shimokawa.
19  *
20  * 4. Neither the name of the author nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD: src/sys/dev/dcons/dcons_os.c,v 1.4 2004/10/24 12:41:04 simokawa Exp $
37  */
38 
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/systm.h>
43 #include <sys/types.h>
44 #include <sys/conf.h>
45 #include <sys/cons.h>
46 #include <sys/consio.h>
47 #include <sys/tty.h>
48 #include <sys/ttydefaults.h>  /* for TTYDEF_* */
49 #include <sys/malloc.h>
50 #include <sys/proc.h>
51 #include <sys/caps.h>
52 #include <sys/ucred.h>
53 #include <sys/bus.h>
54 
55 #include "dcons.h"
56 #include "dcons_os.h"
57 
58 #include <ddb/ddb.h>
59 #include <sys/reboot.h>
60 
61 #include <sys/sysctl.h>
62 
63 #include <vm/vm.h>
64 #include <vm/vm_param.h>
65 #include <vm/pmap.h>
66 
67 #include "opt_ddb.h"
68 #include "opt_comconsole.h"
69 #include "opt_dcons.h"
70 
71 #ifndef DCONS_POLL_HZ
72 #define DCONS_POLL_HZ         100
73 #endif
74 
75 #ifndef DCONS_BUF_SIZE
76 #define DCONS_BUF_SIZE (16*1024)
77 #endif
78 
79 #ifndef DCONS_FORCE_CONSOLE
80 #define DCONS_FORCE_CONSOLE   0         /* Mostly for FreeBSD-4/DragonFly */
81 #endif
82 
83 #ifndef DCONS_FORCE_GDB
84 #define DCONS_FORCE_GDB       1
85 #endif
86 
87 static d_open_t               dcons_open;
88 static d_close_t    dcons_close;
89 static d_ioctl_t    dcons_ioctl;
90 
91 static struct dev_ops dcons_ops = {
92           { "dcons", 0, D_TTY },
93           .d_open = dcons_open,
94           .d_close =          dcons_close,
95           .d_read = ttyread,
96           .d_write =          ttywrite,
97           .d_ioctl =          dcons_ioctl,
98           .d_kqfilter =       ttykqfilter,
99           .d_revoke =         ttyrevoke
100 };
101 
102 #ifndef KLD_MODULE
103 static char bssbuf[DCONS_BUF_SIZE];     /* buf in bss */
104 #endif
105 
106 /* global data */
107 static struct dcons_global dg;
108 struct dcons_global *dcons_conf;
109 static int poll_hz = DCONS_POLL_HZ;
110 
111 static struct dcons_softc sc[DCONS_NPORT];
112 
113 SYSCTL_NODE(_kern, OID_AUTO, dcons, CTLFLAG_RD, 0, "Dumb Console");
114 SYSCTL_INT(_kern_dcons, OID_AUTO, poll_hz, CTLFLAG_RW, &poll_hz, 0,
115                                         "dcons polling rate");
116 
117 static int drv_init = 0;
118 static struct callout dcons_callout;
119 struct dcons_buf *dcons_buf;            /* for local dconschat */
120 
121 #define DEV         cdev_t
122 #define THREAD      d_thread_t
123 
124 static void         dcons_tty_start(struct tty *);
125 static int          dcons_tty_param(struct tty *, struct termios *);
126 static void         dcons_timeout(void *);
127 static int          dcons_drv_init(int);
128 
129 static cn_probe_t   dcons_cnprobe;
130 static cn_init_t    dcons_cninit;
131 static cn_init_fini_t         dcons_cninit_fini;
132 static cn_getc_t    dcons_cngetc;
133 static cn_checkc_t  dcons_cncheckc;
134 static cn_putc_t    dcons_cnputc;
135 
136 CONS_DRIVER(dcons, dcons_cnprobe, dcons_cninit, dcons_cninit_fini,
137               NULL, dcons_cngetc, dcons_cncheckc, dcons_cnputc, NULL, NULL);
138 
139 #if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER)
140 static int
dcons_check_break(struct dcons_softc * dc,int c)141 dcons_check_break(struct dcons_softc *dc, int c)
142 {
143           if (c < 0)
144                     return (c);
145 
146           switch (dc->brk_state) {
147           case STATE1:
148                     if (c == KEY_TILDE)
149                               dc->brk_state = STATE2;
150                     else
151                               dc->brk_state = STATE0;
152                     break;
153           case STATE2:
154                     dc->brk_state = STATE0;
155                     if (c == KEY_CTRLB) {
156 #if DCONS_FORCE_GDB
157                               if (dc->flags & DC_GDB)
158                                         boothowto |= RB_GDB;
159 #endif
160                               breakpoint();
161                     }
162           }
163           if (c == KEY_CR)
164                     dc->brk_state = STATE1;
165           return (c);
166 }
167 #else
168 #define   dcons_check_break(dc, c)      (c)
169 #endif
170 
171 static int
dcons_os_checkc(struct dcons_softc * dc)172 dcons_os_checkc(struct dcons_softc *dc)
173 {
174           int c;
175 
176           if (dg.dma_tag != NULL)
177                     bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD);
178 
179           c = dcons_check_break(dc, dcons_checkc(dc));
180 
181           if (dg.dma_tag != NULL)
182                     bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD);
183 
184           return (c);
185 }
186 
187 static int
dcons_os_getc(struct dcons_softc * dc)188 dcons_os_getc(struct dcons_softc *dc)
189 {
190           int c;
191 
192           while ((c = dcons_os_checkc(dc)) == -1);
193 
194           return (c & 0xff);
195 }
196 
197 static void
dcons_os_putc(struct dcons_softc * dc,int c)198 dcons_os_putc(struct dcons_softc *dc, int c)
199 {
200           if (dg.dma_tag != NULL)
201                     bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE);
202 
203           dcons_putc(dc, c);
204 
205           if (dg.dma_tag != NULL)
206                     bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE);
207 }
208 static int
dcons_open(struct dev_open_args * ap)209 dcons_open(struct dev_open_args *ap)
210 {
211           cdev_t dev = ap->a_head.a_dev;
212           struct tty *tp;
213           int unit, error;
214 
215           unit = minor(dev);
216           if (unit != 0)
217                     return (ENXIO);
218 
219           tp = ttymalloc(&dev->si_tty);
220           lwkt_gettoken(&tp->t_token);
221           tp->t_oproc = dcons_tty_start;
222           tp->t_param = dcons_tty_param;
223           tp->t_stop = nottystop;
224           tp->t_dev = dev;
225 
226           error = 0;
227 
228           if ((tp->t_state & TS_ISOPEN) == 0) {
229                     tp->t_state |= TS_CARR_ON;
230                     ttychars(tp);
231                     tp->t_iflag = TTYDEF_IFLAG;
232                     tp->t_oflag = TTYDEF_OFLAG;
233                     tp->t_cflag = TTYDEF_CFLAG|CLOCAL;
234                     tp->t_lflag = TTYDEF_LFLAG;
235                     tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
236                     ttsetwater(tp);
237           } else if ((tp->t_state & TS_XCLUDE) &&
238                        caps_priv_check(ap->a_cred, SYSCAP_RESTRICTEDROOT))
239           {
240                     lwkt_reltoken(&tp->t_token);
241 
242                     return (EBUSY);
243           }
244 
245           error = (*linesw[tp->t_line].l_open)(dev, tp);
246           lwkt_reltoken(&tp->t_token);
247 
248           return (error);
249 }
250 
251 static int
dcons_close(struct dev_close_args * ap)252 dcons_close(struct dev_close_args *ap)
253 {
254           cdev_t dev = ap->a_head.a_dev;
255           int       unit;
256           struct    tty *tp;
257 
258           unit = minor(dev);
259           if (unit != 0)
260                     return (ENXIO);
261 
262           tp = dev->si_tty;
263           lwkt_gettoken(&tp->t_token);
264           if (tp->t_state & TS_ISOPEN) {
265                     (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
266                     ttyclose(tp);
267           }
268           lwkt_reltoken(&tp->t_token);
269 
270           return (0);
271 }
272 
273 static int
dcons_ioctl(struct dev_ioctl_args * ap)274 dcons_ioctl(struct dev_ioctl_args *ap)
275 {
276           cdev_t dev = ap->a_head.a_dev;
277           int       unit;
278           struct    tty *tp;
279           int       error;
280 
281           unit = minor(dev);
282           if (unit != 0)
283                     return (ENXIO);
284 
285           tp = dev->si_tty;
286           lwkt_gettoken(&tp->t_token);
287           error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data, ap->a_fflag, ap->a_cred);
288           if (error != ENOIOCTL) {
289                     lwkt_reltoken(&tp->t_token);
290                     return (error);
291           }
292 
293           error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
294           if (error != ENOIOCTL) {
295                     lwkt_reltoken(&tp->t_token);
296                     return (error);
297           }
298 
299           lwkt_reltoken(&tp->t_token);
300           return (ENOTTY);
301 }
302 
303 static int
dcons_tty_param(struct tty * tp,struct termios * t)304 dcons_tty_param(struct tty *tp, struct termios *t)
305 {
306           lwkt_gettoken(&tp->t_token);
307           tp->t_ispeed = t->c_ispeed;
308           tp->t_ospeed = t->c_ospeed;
309           tp->t_cflag = t->c_cflag;
310           lwkt_reltoken(&tp->t_token);
311           return 0;
312 }
313 
314 static void
dcons_tty_start(struct tty * tp)315 dcons_tty_start(struct tty *tp)
316 {
317           struct dcons_softc *dc;
318 
319           lwkt_gettoken(&tp->t_token);
320           dc = (struct dcons_softc *)tp->t_dev->si_drv1;
321           if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
322                     ttwwakeup(tp);
323                     lwkt_reltoken(&tp->t_token);
324                     return;
325           }
326 
327           tp->t_state |= TS_BUSY;
328           while (tp->t_outq.c_cc != 0)
329                     dcons_os_putc(dc, clist_getc(&tp->t_outq));
330           tp->t_state &= ~TS_BUSY;
331 
332           ttwwakeup(tp);
333           lwkt_reltoken(&tp->t_token);
334 }
335 
336 static void
dcons_timeout(void * dummy __unused)337 dcons_timeout(void *dummy __unused)
338 {
339           struct tty *tp;
340           struct dcons_softc *dc;
341           int i, c, polltime;
342 
343           for (i = 0; i < DCONS_NPORT; i ++) {
344                     dc = &sc[i];
345                     tp = ((DEV)dc->dev)->si_tty;
346                     lwkt_gettoken(&tp->t_token);
347                     while ((c = dcons_os_checkc(dc)) != -1) {
348                               if (tp->t_state & TS_ISOPEN)
349                                         (*linesw[tp->t_line].l_rint)(c, tp);
350                     }
351                     lwkt_reltoken(&tp->t_token);
352           }
353           polltime = hz / poll_hz;
354           if (polltime < 1)
355                     polltime = 1;
356           callout_reset(&dcons_callout, polltime, dcons_timeout, NULL);
357 }
358 
359 static void
dcons_cnprobe(struct consdev * cp)360 dcons_cnprobe(struct consdev *cp)
361 {
362           cp->cn_probegood = 1;
363 #if DCONS_FORCE_CONSOLE
364           cp->cn_pri = CN_REMOTE;
365 #else
366           cp->cn_pri = CN_NORMAL;
367 #endif
368 }
369 
370 static void
dcons_cninit(struct consdev * cp)371 dcons_cninit(struct consdev *cp)
372 {
373           dcons_drv_init(0);
374 #ifdef CONS_NODEV
375           cp->cn_arg
376 #else
377           cp->cn_private
378 #endif
379                     = (void *)&sc[DCONS_CON]; /* share port0 with unit0 */
380 }
381 
382 static void
dcons_cninit_fini(struct consdev * cp)383 dcons_cninit_fini(struct consdev *cp)
384 {
385           cp->cn_dev = make_dev(&dcons_ops, DCONS_CON,
386                                     UID_ROOT, GID_WHEEL, 0600, "dcons");
387 }
388 
389 #ifdef CONS_NODEV
390 static int
dcons_cngetc(struct consdev * cp)391 dcons_cngetc(struct consdev *cp)
392 {
393           struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
394           return (dcons_os_getc(dc));
395 }
396 static int
dcons_cncheckc(struct consdev * cp)397 dcons_cncheckc(struct consdev *cp)
398 {
399           struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
400           return (dcons_os_checkc(dc));
401 }
402 static void
dcons_cnputc(struct consdev * cp,int c)403 dcons_cnputc(struct consdev *cp, int c)
404 {
405           struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
406           dcons_os_putc(dc, c);
407 }
408 #else
409 static int
dcons_cngetc(void * private)410 dcons_cngetc(void *private)
411 {
412           struct dcons_softc *dc = (struct dcons_softc *)private;
413           return (dcons_os_getc(dc));
414 }
415 static int
dcons_cncheckc(void * private)416 dcons_cncheckc(void *private)
417 {
418           struct dcons_softc *dc = (struct dcons_softc *)private;
419           return (dcons_os_checkc(dc));
420 }
421 static void
dcons_cnputc(void * private,int c)422 dcons_cnputc(void *private, int c)
423 {
424           struct dcons_softc *dc = (struct dcons_softc *)private;
425           dcons_os_putc(dc, c);
426 }
427 #endif
428 
429 static int
dcons_drv_init(int stage)430 dcons_drv_init(int stage)
431 {
432 #ifdef __i386__
433           quad_t addr, size;
434 #endif
435 
436           if (drv_init)
437                     return(drv_init);
438 
439           drv_init = -1;
440 
441           bzero(&dg, sizeof(dg));
442           dcons_conf = &dg;
443           dg.cdev = &dcons_consdev;
444           dg.buf = NULL;
445           dg.size = DCONS_BUF_SIZE;
446 
447 #ifdef __i386__
448           if (kgetenv_quad("dcons.addr", &addr) > 0 &&
449               kgetenv_quad("dcons.size", &size) > 0) {
450                     vm_paddr_t pa;
451                     /*
452                      * Allow read/write access to dcons buffer.
453                      */
454                     for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE)
455                               pmap_kmodify_rw((vm_offset_t)(KERNBASE + pa));
456                     cpu_invltlb();
457                     /* XXX P to V */
458                     dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr);
459                     dg.size = size;
460                     if (dcons_load_buffer(dg.buf, dg.size, sc) < 0)
461                               dg.buf = NULL;
462           }
463 #endif
464           if (dg.buf != NULL)
465                     goto ok;
466 
467 #ifndef KLD_MODULE
468           if (stage == 0) { /* XXX or cold */
469                     /*
470                      * DCONS_FORCE_CONSOLE == 1 and statically linked.
471                      * called from cninit(). can't use contigmalloc yet .
472                      */
473                     dg.buf = (struct dcons_buf *) bssbuf;
474                     dcons_init(dg.buf, dg.size, sc);
475           } else
476 #endif
477           {
478                     /*
479                      * DCONS_FORCE_CONSOLE == 0 or kernel module case.
480                      * if the module is loaded after boot,
481                      * bssbuf could be non-continuous.
482                      */
483                     dg.buf = (struct dcons_buf *) contigmalloc(dg.size,
484                               M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul);
485                     dcons_init(dg.buf, dg.size, sc);
486           }
487 
488 ok:
489           dcons_buf = dg.buf;
490 
491 #if defined(DDB) && DCONS_FORCE_GDB
492           if (gdb_tab == NULL) {
493                     gdb_tab = &dcons_consdev;
494                     dcons_consdev.cn_gdbprivate = &sc[DCONS_GDB];
495           }
496 #endif
497           drv_init = 1;
498 
499           return 0;
500 }
501 
502 static int
dcons_attach_port(int port,char * name,int flags)503 dcons_attach_port(int port, char *name, int flags)
504 {
505           struct dcons_softc *dc;
506           struct tty *tp;
507           DEV dev;
508 
509           dc = &sc[port];
510           dc->flags = flags;
511           dev = make_dev(&dcons_ops, port, UID_ROOT, GID_WHEEL, 0600,
512                            "%s", name);
513           dc->dev = (void *)dev;
514           dev->si_drv1 = (void *)dc;
515 
516           tp = ttymalloc(&dev->si_tty);
517           tp->t_oproc = dcons_tty_start;
518           tp->t_param = dcons_tty_param;
519           tp->t_stop = nottystop;
520           tp->t_dev = dc->dev;
521 
522           return(0);
523 }
524 
525 static int
dcons_attach(void)526 dcons_attach(void)
527 {
528           int polltime;
529 
530           dcons_attach_port(DCONS_CON, "dcons", 0);
531           dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB);
532           callout_init_mp(&dcons_callout);
533           polltime = hz / poll_hz;
534           if (polltime < 1)
535                     polltime = 1;
536           callout_reset(&dcons_callout, polltime, dcons_timeout, NULL);
537           return(0);
538 }
539 
540 static int
dcons_detach(int port)541 dcons_detach(int port)
542 {
543           struct    tty *tp;
544           struct dcons_softc *dc;
545 
546           dc = &sc[port];
547 
548           tp = ((DEV)dc->dev)->si_tty;
549           lwkt_gettoken(&tp->t_token);
550 
551           if (tp->t_state & TS_ISOPEN) {
552                     kprintf("dcons: still opened\n");
553                     (*linesw[tp->t_line].l_close)(tp, 0);
554                     ttyclose(tp);
555           }
556           /* XXX
557            * must wait until all device are closed.
558            */
559 #ifdef __DragonFly__
560           tsleep((void *)dc, 0, "dcodtc", hz/4);
561 #else
562           tsleep((void *)dc, PWAIT, "dcodtc", hz/4);
563 #endif
564           lwkt_reltoken(&tp->t_token);
565           destroy_dev(dc->dev);
566 
567           return(0);
568 }
569 
570 
571 /* cnXXX works only for FreeBSD-5 */
572 static int
dcons_modevent(module_t mode,int type,void * data)573 dcons_modevent(module_t mode, int type, void *data)
574 {
575           int err = 0, ret;
576 
577           switch (type) {
578           case MOD_LOAD:
579                     ret = dcons_drv_init(1);
580                     dcons_attach();
581 #if 0 /* XXX __FreeBSD_version >= 500000 */
582                     if (ret == 0) {
583                               dcons_cnprobe(&dcons_consdev);
584                               dcons_cninit(&dcons_consdev);
585                               cnadd(&dcons_consdev);
586                     }
587 #endif
588                     break;
589           case MOD_UNLOAD:
590                     kprintf("dcons: unload\n");
591                     callout_stop(&dcons_callout);
592 #if defined(DDB) && DCONS_FORCE_GDB
593 #ifdef CONS_NODEV
594                     gdb_arg = NULL;
595 #else
596                     if (gdb_tab == &dcons_consdev)
597                               gdb_tab = NULL;
598 #endif
599 #endif
600 #if 0 /* XXX __FreeBSD_version >= 500000 */
601                     cnremove(&dcons_consdev);
602 #endif
603                     dcons_detach(DCONS_CON);
604                     dcons_detach(DCONS_GDB);
605                     dg.buf->magic = 0;
606 
607                     contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF);
608 
609                     break;
610           case MOD_SHUTDOWN:
611                     dg.buf->magic = 0;
612                     break;
613           default:
614                     err = EOPNOTSUPP;
615                     break;
616           }
617           return(err);
618 }
619 
620 DEV_MODULE(dcons, dcons_modevent, NULL);
621 MODULE_VERSION(dcons, DCONS_VERSION);
622