1 /*- 2 * Copyright (C) 2003,2004 3 * Hidetoshi Shimokawa. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * 16 * This product includes software developed by Hidetoshi Shimokawa. 17 * 18 * 4. Neither the name of the author nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * $FreeBSD$ 35 */ 36 37 #include <sys/param.h> 38 #if __FreeBSD_version >= 502122 39 #include <sys/kdb.h> 40 #include <gdb/gdb.h> 41 #endif 42 #include <sys/kernel.h> 43 #include <sys/module.h> 44 #include <sys/systm.h> 45 #include <sys/types.h> 46 #include <sys/conf.h> 47 #include <sys/cons.h> 48 #include <sys/consio.h> 49 #include <sys/tty.h> 50 #include <sys/malloc.h> 51 #include <sys/priv.h> 52 #include <sys/proc.h> 53 #include <sys/ucred.h> 54 55 #include <machine/bus.h> 56 57 #ifdef __DragonFly__ 58 #include "dcons.h" 59 #include "dcons_os.h" 60 #else 61 #include <dev/dcons/dcons.h> 62 #include <dev/dcons/dcons_os.h> 63 #endif 64 65 #include <ddb/ddb.h> 66 #include <sys/reboot.h> 67 68 #include <sys/sysctl.h> 69 70 #include <vm/vm.h> 71 #include <vm/vm_param.h> 72 #include <vm/pmap.h> 73 74 #include "opt_ddb.h" 75 #include "opt_comconsole.h" 76 #include "opt_dcons.h" 77 78 #ifndef DCONS_POLL_HZ 79 #define DCONS_POLL_HZ 100 80 #endif 81 82 #ifndef DCONS_BUF_SIZE 83 #define DCONS_BUF_SIZE (16*1024) 84 #endif 85 86 #ifndef DCONS_FORCE_CONSOLE 87 #define DCONS_FORCE_CONSOLE 0 /* Mostly for FreeBSD-4/DragonFly */ 88 #endif 89 90 #ifndef DCONS_FORCE_GDB 91 #define DCONS_FORCE_GDB 1 92 #endif 93 94 #if __FreeBSD_version >= 500101 95 #define CONS_NODEV 1 96 #if __FreeBSD_version < 502122 97 static struct consdev gdbconsdev; 98 #endif 99 #endif 100 101 static d_open_t dcons_open; 102 static d_close_t dcons_close; 103 #if defined(__DragonFly__) || __FreeBSD_version < 500104 104 static d_ioctl_t dcons_ioctl; 105 #endif 106 107 static struct cdevsw dcons_cdevsw = { 108 #ifdef __DragonFly__ 109 #define CDEV_MAJOR 184 110 "dcons", CDEV_MAJOR, D_TTY, NULL, 0, 111 dcons_open, dcons_close, ttyread, ttywrite, dcons_ioctl, 112 ttypoll, nommap, nostrategy, nodump, nopsize, 113 #elif __FreeBSD_version >= 500104 114 .d_version = D_VERSION, 115 .d_open = dcons_open, 116 .d_close = dcons_close, 117 .d_name = "dcons", 118 .d_flags = D_TTY | D_NEEDGIANT, 119 #else 120 #define CDEV_MAJOR 184 121 /* open */ dcons_open, 122 /* close */ dcons_close, 123 /* read */ ttyread, 124 /* write */ ttywrite, 125 /* ioctl */ dcons_ioctl, 126 /* poll */ ttypoll, 127 /* mmap */ nommap, 128 /* strategy */ nostrategy, 129 /* name */ "dcons", 130 /* major */ CDEV_MAJOR, 131 /* dump */ nodump, 132 /* psize */ nopsize, 133 /* flags */ D_TTY, 134 #endif 135 }; 136 137 #ifndef KLD_MODULE 138 static char bssbuf[DCONS_BUF_SIZE]; /* buf in bss */ 139 #endif 140 141 /* global data */ 142 static struct dcons_global dg; 143 struct dcons_global *dcons_conf; 144 static int poll_hz = DCONS_POLL_HZ; 145 146 static struct dcons_softc sc[DCONS_NPORT]; 147 148 SYSCTL_NODE(_kern, OID_AUTO, dcons, CTLFLAG_RD, 0, "Dumb Console"); 149 SYSCTL_INT(_kern_dcons, OID_AUTO, poll_hz, CTLFLAG_RW, &poll_hz, 0, 150 "dcons polling rate"); 151 152 static int drv_init = 0; 153 static struct callout dcons_callout; 154 struct dcons_buf *dcons_buf; /* for local dconschat */ 155 156 #ifdef __DragonFly__ 157 #define DEV dev_t 158 #define THREAD d_thread_t 159 #elif __FreeBSD_version < 500000 160 #define DEV dev_t 161 #define THREAD struct proc 162 #else 163 #define DEV struct cdev * 164 #define THREAD struct thread 165 #endif 166 167 168 static void dcons_tty_start(struct tty *); 169 static int dcons_tty_param(struct tty *, struct termios *); 170 static void dcons_timeout(void *); 171 static int dcons_drv_init(int); 172 173 static cn_probe_t dcons_cnprobe; 174 static cn_init_t dcons_cninit; 175 static cn_term_t dcons_cnterm; 176 static cn_getc_t dcons_cngetc; 177 static cn_putc_t dcons_cnputc; 178 179 CONSOLE_DRIVER(dcons); 180 181 #if __FreeBSD_version >= 502122 182 static gdb_probe_f dcons_dbg_probe; 183 static gdb_init_f dcons_dbg_init; 184 static gdb_term_f dcons_dbg_term; 185 static gdb_getc_f dcons_dbg_getc; 186 static gdb_putc_f dcons_dbg_putc; 187 188 GDB_DBGPORT(dcons, dcons_dbg_probe, dcons_dbg_init, dcons_dbg_term, 189 dcons_dbg_getc, dcons_dbg_putc); 190 191 extern struct gdb_dbgport *gdb_cur; 192 #endif 193 194 #if (defined(KDB) || defined(DDB)) && defined(ALT_BREAK_TO_DEBUGGER) 195 static int 196 dcons_check_break(struct dcons_softc *dc, int c) 197 { 198 if (c < 0) 199 return (c); 200 201 #if __FreeBSD_version >= 502122 202 if (kdb_alt_break(c, &dc->brk_state)) { 203 if ((dc->flags & DC_GDB) != 0) { 204 if (gdb_cur == &dcons_gdb_dbgport) { 205 kdb_dbbe_select("gdb"); 206 breakpoint(); 207 } 208 } else 209 breakpoint(); 210 } 211 #else 212 switch (dc->brk_state) { 213 case STATE1: 214 if (c == KEY_TILDE) 215 dc->brk_state = STATE2; 216 else 217 dc->brk_state = STATE0; 218 break; 219 case STATE2: 220 dc->brk_state = STATE0; 221 if (c == KEY_CTRLB) { 222 #if DCONS_FORCE_GDB 223 if (dc->flags & DC_GDB) 224 boothowto |= RB_GDB; 225 #endif 226 breakpoint(); 227 } 228 } 229 if (c == KEY_CR) 230 dc->brk_state = STATE1; 231 #endif 232 return (c); 233 } 234 #else 235 #define dcons_check_break(dc, c) (c) 236 #endif 237 238 static int 239 dcons_os_checkc(struct dcons_softc *dc) 240 { 241 int c; 242 243 if (dg.dma_tag != NULL) 244 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD); 245 246 c = dcons_check_break(dc, dcons_checkc(dc)); 247 248 if (dg.dma_tag != NULL) 249 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD); 250 251 return (c); 252 } 253 254 static int 255 dcons_os_getc(struct dcons_softc *dc) 256 { 257 int c; 258 259 while ((c = dcons_os_checkc(dc)) == -1); 260 261 return (c & 0xff); 262 } 263 264 static void 265 dcons_os_putc(struct dcons_softc *dc, int c) 266 { 267 if (dg.dma_tag != NULL) 268 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE); 269 270 dcons_putc(dc, c); 271 272 if (dg.dma_tag != NULL) 273 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE); 274 } 275 static int 276 dcons_open(DEV dev, int flag, int mode, THREAD *td) 277 { 278 struct tty *tp; 279 int unit, error, s; 280 281 unit = minor(dev); 282 if (unit != 0) 283 return (ENXIO); 284 285 tp = dev->si_tty; 286 tp->t_oproc = dcons_tty_start; 287 tp->t_param = dcons_tty_param; 288 tp->t_stop = nottystop; 289 tp->t_dev = dev; 290 291 error = 0; 292 293 s = spltty(); 294 if ((tp->t_state & TS_ISOPEN) == 0) { 295 tp->t_state |= TS_CARR_ON; 296 ttyconsolemode(tp, 0); 297 } else if ((tp->t_state & TS_XCLUDE) && 298 priv_check(td, PRIV_TTY_EXCLUSIVE)) { 299 splx(s); 300 return (EBUSY); 301 } 302 splx(s); 303 304 #if __FreeBSD_version < 502113 305 error = (*linesw[tp->t_line].l_open)(dev, tp); 306 #else 307 error = ttyld_open(tp, dev); 308 #endif 309 310 return (error); 311 } 312 313 static int 314 dcons_close(DEV dev, int flag, int mode, THREAD *td) 315 { 316 int unit; 317 struct tty *tp; 318 319 unit = minor(dev); 320 if (unit != 0) 321 return (ENXIO); 322 323 tp = dev->si_tty; 324 if (tp->t_state & TS_ISOPEN) { 325 #if __FreeBSD_version < 502113 326 (*linesw[tp->t_line].l_close)(tp, flag); 327 ttyclose(tp); 328 #else 329 ttyld_close(tp, flag); 330 tty_close(tp); 331 #endif 332 } 333 334 return (0); 335 } 336 337 #if defined(__DragonFly__) || __FreeBSD_version < 500104 338 static int 339 dcons_ioctl(DEV dev, u_long cmd, caddr_t data, int flag, THREAD *td) 340 { 341 int unit; 342 struct tty *tp; 343 int error; 344 345 unit = minor(dev); 346 if (unit != 0) 347 return (ENXIO); 348 349 tp = dev->si_tty; 350 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td); 351 if (error != ENOIOCTL) 352 return (error); 353 354 error = ttioctl(tp, cmd, data, flag); 355 if (error != ENOIOCTL) 356 return (error); 357 358 return (ENOTTY); 359 } 360 #endif 361 362 static int 363 dcons_tty_param(struct tty *tp, struct termios *t) 364 { 365 tp->t_ispeed = t->c_ispeed; 366 tp->t_ospeed = t->c_ospeed; 367 tp->t_cflag = t->c_cflag; 368 return 0; 369 } 370 371 static void 372 dcons_tty_start(struct tty *tp) 373 { 374 struct dcons_softc *dc; 375 int s; 376 377 dc = (struct dcons_softc *)tp->t_dev->si_drv1; 378 s = spltty(); 379 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) { 380 ttwwakeup(tp); 381 return; 382 } 383 384 tp->t_state |= TS_BUSY; 385 while (tp->t_outq.c_cc != 0) 386 dcons_os_putc(dc, getc(&tp->t_outq)); 387 tp->t_state &= ~TS_BUSY; 388 389 ttwwakeup(tp); 390 splx(s); 391 } 392 393 static void 394 dcons_timeout(void *v) 395 { 396 struct tty *tp; 397 struct dcons_softc *dc; 398 int i, c, polltime; 399 400 for (i = 0; i < DCONS_NPORT; i ++) { 401 dc = &sc[i]; 402 tp = ((DEV)dc->dev)->si_tty; 403 while ((c = dcons_os_checkc(dc)) != -1) 404 if (tp->t_state & TS_ISOPEN) 405 #if __FreeBSD_version < 502113 406 (*linesw[tp->t_line].l_rint)(c, tp); 407 #else 408 ttyld_rint(tp, c); 409 #endif 410 } 411 polltime = hz / poll_hz; 412 if (polltime < 1) 413 polltime = 1; 414 callout_reset(&dcons_callout, polltime, dcons_timeout, tp); 415 } 416 417 static void 418 dcons_cnprobe(struct consdev *cp) 419 { 420 #ifdef __DragonFly__ 421 cp->cn_dev = make_dev(&dcons_cdevsw, DCONS_CON, 422 UID_ROOT, GID_WHEEL, 0600, "dcons"); 423 #elif __FreeBSD_version >= 501109 424 sprintf(cp->cn_name, "dcons"); 425 #else 426 cp->cn_dev = makedev(CDEV_MAJOR, DCONS_CON); 427 #endif 428 #if DCONS_FORCE_CONSOLE 429 cp->cn_pri = CN_REMOTE; 430 #else 431 cp->cn_pri = CN_NORMAL; 432 #endif 433 } 434 435 static void 436 dcons_cninit(struct consdev *cp) 437 { 438 dcons_drv_init(0); 439 #if CONS_NODEV 440 cp->cn_arg 441 #else 442 cp->cn_dev->si_drv1 443 #endif 444 = (void *)&sc[DCONS_CON]; /* share port0 with unit0 */ 445 } 446 447 static void 448 dcons_cnterm(struct consdev *cp) 449 { 450 } 451 452 #if CONS_NODEV 453 static int 454 dcons_cngetc(struct consdev *cp) 455 { 456 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 457 return (dcons_os_checkc(dc)); 458 } 459 static void 460 dcons_cnputc(struct consdev *cp, int c) 461 { 462 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 463 dcons_os_putc(dc, c); 464 } 465 #else 466 static int 467 dcons_cngetc(DEV dev) 468 { 469 struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1; 470 return (dcons_os_getc(dc)); 471 } 472 static int 473 dcons_cncheckc(DEV dev) 474 { 475 struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1; 476 return (dcons_os_checkc(dc)); 477 } 478 static void 479 dcons_cnputc(DEV dev, int c) 480 { 481 struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1; 482 dcons_os_putc(dc, c); 483 } 484 #endif 485 486 static int 487 dcons_drv_init(int stage) 488 { 489 #if defined(__i386__) || defined(__amd64__) 490 quad_t addr, size; 491 #endif 492 493 if (drv_init) 494 return(drv_init); 495 496 drv_init = -1; 497 498 bzero(&dg, sizeof(dg)); 499 dcons_conf = &dg; 500 dg.cdev = &dcons_consdev; 501 dg.buf = NULL; 502 dg.size = DCONS_BUF_SIZE; 503 504 #if defined(__i386__) || defined(__amd64__) 505 if (getenv_quad("dcons.addr", &addr) > 0 && 506 getenv_quad("dcons.size", &size) > 0) { 507 #ifdef __i386__ 508 vm_paddr_t pa; 509 /* 510 * Allow read/write access to dcons buffer. 511 */ 512 for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE) 513 *vtopte(KERNBASE + pa) |= PG_RW; 514 invltlb(); 515 #endif 516 /* XXX P to V */ 517 dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr); 518 dg.size = size; 519 if (dcons_load_buffer(dg.buf, dg.size, sc) < 0) 520 dg.buf = NULL; 521 } 522 #endif 523 if (dg.buf != NULL) 524 goto ok; 525 526 #ifndef KLD_MODULE 527 if (stage == 0) { /* XXX or cold */ 528 /* 529 * DCONS_FORCE_CONSOLE == 1 and statically linked. 530 * called from cninit(). can't use contigmalloc yet . 531 */ 532 dg.buf = (struct dcons_buf *) bssbuf; 533 dcons_init(dg.buf, dg.size, sc); 534 } else 535 #endif 536 { 537 /* 538 * DCONS_FORCE_CONSOLE == 0 or kernel module case. 539 * if the module is loaded after boot, 540 * bssbuf could be non-continuous. 541 */ 542 dg.buf = (struct dcons_buf *) contigmalloc(dg.size, 543 M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul); 544 dcons_init(dg.buf, dg.size, sc); 545 } 546 547 ok: 548 dcons_buf = dg.buf; 549 550 #if __FreeBSD_version < 502122 551 #if defined(DDB) && DCONS_FORCE_GDB 552 #if CONS_NODEV 553 gdbconsdev.cn_arg = (void *)&sc[DCONS_GDB]; 554 #if __FreeBSD_version >= 501109 555 sprintf(gdbconsdev.cn_name, "dgdb"); 556 #endif 557 gdb_arg = &gdbconsdev; 558 #elif defined(__DragonFly__) 559 gdbdev = make_dev(&dcons_cdevsw, DCONS_GDB, 560 UID_ROOT, GID_WHEEL, 0600, "dgdb"); 561 #else 562 gdbdev = makedev(CDEV_MAJOR, DCONS_GDB); 563 #endif 564 gdb_getc = dcons_cngetc; 565 gdb_putc = dcons_cnputc; 566 #endif 567 #endif 568 drv_init = 1; 569 570 return 0; 571 } 572 573 574 static int 575 dcons_attach_port(int port, char *name, int flags) 576 { 577 struct dcons_softc *dc; 578 struct tty *tp; 579 DEV dev; 580 581 dc = &sc[port]; 582 dc->flags = flags; 583 dev = make_dev(&dcons_cdevsw, port, 584 UID_ROOT, GID_WHEEL, 0600, name); 585 dc->dev = (void *)dev; 586 tp = ttyalloc(); 587 588 dev->si_drv1 = (void *)dc; 589 dev->si_tty = tp; 590 591 tp->t_oproc = dcons_tty_start; 592 tp->t_param = dcons_tty_param; 593 tp->t_stop = nottystop; 594 tp->t_dev = dc->dev; 595 596 return(0); 597 } 598 599 static int 600 dcons_attach(void) 601 { 602 int polltime; 603 604 #ifdef __DragonFly__ 605 cdevsw_add(&dcons_cdevsw, -1, 0); 606 #endif 607 dcons_attach_port(DCONS_CON, "dcons", 0); 608 dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB); 609 #if __FreeBSD_version < 500000 610 callout_init(&dcons_callout); 611 #else 612 callout_init(&dcons_callout, 0); 613 #endif 614 polltime = hz / poll_hz; 615 if (polltime < 1) 616 polltime = 1; 617 callout_reset(&dcons_callout, polltime, dcons_timeout, NULL); 618 return(0); 619 } 620 621 static int 622 dcons_detach(int port) 623 { 624 struct tty *tp; 625 struct dcons_softc *dc; 626 627 dc = &sc[port]; 628 629 tp = ((DEV)dc->dev)->si_tty; 630 631 if (tp->t_state & TS_ISOPEN) { 632 printf("dcons: still opened\n"); 633 #if __FreeBSD_version < 502113 634 (*linesw[tp->t_line].l_close)(tp, 0); 635 tp->t_gen++; 636 ttyclose(tp); 637 ttwakeup(tp); 638 ttwwakeup(tp); 639 #else 640 ttyld_close(tp, 0); 641 tty_close(tp); 642 #endif 643 } 644 /* XXX 645 * must wait until all device are closed. 646 */ 647 #ifdef __DragonFly__ 648 tsleep((void *)dc, 0, "dcodtc", hz/4); 649 #else 650 tsleep((void *)dc, PWAIT, "dcodtc", hz/4); 651 #endif 652 destroy_dev(dc->dev); 653 654 return(0); 655 } 656 657 658 /* cnXXX works only for FreeBSD-5 */ 659 static int 660 dcons_modevent(module_t mode, int type, void *data) 661 { 662 int err = 0, ret; 663 664 switch (type) { 665 case MOD_LOAD: 666 ret = dcons_drv_init(1); 667 dcons_attach(); 668 #if __FreeBSD_version >= 500000 669 if (ret == 0) { 670 dcons_cnprobe(&dcons_consdev); 671 dcons_cninit(&dcons_consdev); 672 cnadd(&dcons_consdev); 673 } 674 #endif 675 break; 676 case MOD_UNLOAD: 677 printf("dcons: unload\n"); 678 callout_stop(&dcons_callout); 679 #if __FreeBSD_version < 502122 680 #if defined(DDB) && DCONS_FORCE_GDB 681 #if CONS_NODEV 682 gdb_arg = NULL; 683 #else 684 gdbdev = NULL; 685 #endif 686 #endif 687 #endif 688 #if __FreeBSD_version >= 500000 689 cnremove(&dcons_consdev); 690 #endif 691 dcons_detach(DCONS_CON); 692 dcons_detach(DCONS_GDB); 693 dg.buf->magic = 0; 694 695 contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF); 696 697 break; 698 case MOD_SHUTDOWN: 699 dg.buf->magic = 0; 700 break; 701 default: 702 err = EOPNOTSUPP; 703 break; 704 } 705 return(err); 706 } 707 708 #if __FreeBSD_version >= 502122 709 /* Debugger interface */ 710 711 static int 712 dcons_dbg_probe(void) 713 { 714 return(DCONS_FORCE_GDB); 715 } 716 717 static void 718 dcons_dbg_init(void) 719 { 720 } 721 722 static void 723 dcons_dbg_term(void) 724 { 725 } 726 727 static void 728 dcons_dbg_putc(int c) 729 { 730 struct dcons_softc *dc = &sc[DCONS_GDB]; 731 dcons_os_putc(dc, c); 732 } 733 734 static int 735 dcons_dbg_getc(void) 736 { 737 struct dcons_softc *dc = &sc[DCONS_GDB]; 738 return (dcons_os_getc(dc)); 739 } 740 #endif 741 742 DEV_MODULE(dcons, dcons_modevent, NULL); 743 MODULE_VERSION(dcons, DCONS_VERSION); 744