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: src/sys/dev/dcons/dcons_os.c,v 1.4 2004/10/24 12:41:04 simokawa Exp $ 35 * $DragonFly: src/sys/dev/misc/dcons/dcons_os.c,v 1.13 2007/08/07 13:14:11 hasso Exp $ 36 */ 37 38 #include <sys/param.h> 39 #if __FreeBSD_version >= 502122 40 #include <sys/kdb.h> 41 #include <gdb/gdb.h> 42 #endif 43 #include <sys/kernel.h> 44 #include <sys/module.h> 45 #include <sys/systm.h> 46 #include <sys/types.h> 47 #include <sys/conf.h> 48 #include <sys/cons.h> 49 #include <sys/consio.h> 50 #include <sys/tty.h> 51 #include <sys/malloc.h> 52 #include <sys/proc.h> 53 #include <sys/priv.h> 54 #include <sys/thread2.h> 55 #include <sys/ucred.h> 56 #include <sys/bus.h> 57 58 #include "dcons.h" 59 #include "dcons_os.h" 60 61 #include <ddb/ddb.h> 62 #include <sys/reboot.h> 63 64 #include <sys/sysctl.h> 65 66 #include <vm/vm.h> 67 #include <vm/vm_param.h> 68 #include <vm/pmap.h> 69 70 #include "opt_ddb.h" 71 #include "opt_comconsole.h" 72 #include "opt_dcons.h" 73 74 #ifndef DCONS_POLL_HZ 75 #define DCONS_POLL_HZ 100 76 #endif 77 78 #ifndef DCONS_BUF_SIZE 79 #define DCONS_BUF_SIZE (16*1024) 80 #endif 81 82 #ifndef DCONS_FORCE_CONSOLE 83 #define DCONS_FORCE_CONSOLE 0 /* Mostly for FreeBSD-4/DragonFly */ 84 #endif 85 86 #ifndef DCONS_FORCE_GDB 87 #define DCONS_FORCE_GDB 1 88 #endif 89 90 #define CDEV_MAJOR 184 91 92 static d_open_t dcons_open; 93 static d_close_t dcons_close; 94 static d_ioctl_t dcons_ioctl; 95 96 static struct dev_ops dcons_ops = { 97 { "dcons", CDEV_MAJOR, D_TTY }, 98 .d_open = dcons_open, 99 .d_close = dcons_close, 100 .d_read = ttyread, 101 .d_write = ttywrite, 102 .d_ioctl = dcons_ioctl, 103 .d_poll = ttypoll, 104 }; 105 106 #ifndef KLD_MODULE 107 static char bssbuf[DCONS_BUF_SIZE]; /* buf in bss */ 108 #endif 109 110 /* global data */ 111 static struct dcons_global dg; 112 struct dcons_global *dcons_conf; 113 static int poll_hz = DCONS_POLL_HZ; 114 115 static struct dcons_softc sc[DCONS_NPORT]; 116 117 SYSCTL_NODE(_kern, OID_AUTO, dcons, CTLFLAG_RD, 0, "Dumb Console"); 118 SYSCTL_INT(_kern_dcons, OID_AUTO, poll_hz, CTLFLAG_RW, &poll_hz, 0, 119 "dcons polling rate"); 120 121 static int drv_init = 0; 122 static struct callout dcons_callout; 123 struct dcons_buf *dcons_buf; /* for local dconschat */ 124 125 #ifdef __DragonFly__ 126 #define DEV cdev_t 127 #define THREAD d_thread_t 128 #elif __FreeBSD_version < 500000 129 #define DEV cdev_t 130 #define THREAD struct proc 131 #else 132 #define DEV struct cdev * 133 #define THREAD struct thread 134 #endif 135 136 137 static void dcons_tty_start(struct tty *); 138 static int dcons_tty_param(struct tty *, struct termios *); 139 static void dcons_timeout(void *); 140 static int dcons_drv_init(int); 141 142 static cn_probe_t dcons_cnprobe; 143 static cn_init_t dcons_cninit; 144 static cn_init_fini_t dcons_cninit_fini; 145 static cn_getc_t dcons_cngetc; 146 static cn_checkc_t dcons_cncheckc; 147 static cn_putc_t dcons_cnputc; 148 149 CONS_DRIVER(dcons, dcons_cnprobe, dcons_cninit, dcons_cninit_fini, 150 NULL, dcons_cngetc, dcons_cncheckc, dcons_cnputc, NULL); 151 152 #if __FreeBSD_version >= 502122 153 static gdb_probe_f dcons_dbg_probe; 154 static gdb_init_f dcons_dbg_init; 155 static gdb_term_f dcons_dbg_term; 156 static gdb_getc_f dcons_dbg_getc; 157 static gdb_checkc_f dcons_dbg_checkc; 158 static gdb_putc_f dcons_dbg_putc; 159 160 GDB_DBGPORT(dcons, dcons_dbg_probe, dcons_dbg_init, dcons_dbg_term, 161 dcons_dbg_checkc, dcons_dbg_getc, dcons_dbg_putc); 162 163 extern struct gdb_dbgport *gdb_cur; 164 #endif 165 166 #if (KDB || DDB) && ALT_BREAK_TO_DEBUGGER 167 static int 168 dcons_check_break(struct dcons_softc *dc, int c) 169 { 170 if (c < 0) 171 return (c); 172 173 #if __FreeBSD_version >= 502122 174 if (kdb_alt_break(c, &dc->brk_state)) { 175 if ((dc->flags & DC_GDB) != 0) { 176 if (gdb_cur == &dcons_gdb_dbgport) { 177 kdb_dbbe_select("gdb"); 178 breakpoint(); 179 } 180 } else 181 breakpoint(); 182 } 183 #else 184 switch (dc->brk_state) { 185 case STATE1: 186 if (c == KEY_TILDE) 187 dc->brk_state = STATE2; 188 else 189 dc->brk_state = STATE0; 190 break; 191 case STATE2: 192 dc->brk_state = STATE0; 193 if (c == KEY_CTRLB) { 194 #if DCONS_FORCE_GDB 195 if (dc->flags & DC_GDB) 196 boothowto |= RB_GDB; 197 #endif 198 breakpoint(); 199 } 200 } 201 if (c == KEY_CR) 202 dc->brk_state = STATE1; 203 #endif 204 return (c); 205 } 206 #else 207 #define dcons_check_break(dc, c) (c) 208 #endif 209 210 static int 211 dcons_os_checkc(struct dcons_softc *dc) 212 { 213 int c; 214 215 if (dg.dma_tag != NULL) 216 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD); 217 218 c = dcons_check_break(dc, dcons_checkc(dc)); 219 220 if (dg.dma_tag != NULL) 221 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD); 222 223 return (c); 224 } 225 226 static int 227 dcons_os_getc(struct dcons_softc *dc) 228 { 229 int c; 230 231 while ((c = dcons_os_checkc(dc)) == -1); 232 233 return (c & 0xff); 234 } 235 236 static void 237 dcons_os_putc(struct dcons_softc *dc, int c) 238 { 239 if (dg.dma_tag != NULL) 240 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE); 241 242 dcons_putc(dc, c); 243 244 if (dg.dma_tag != NULL) 245 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE); 246 } 247 static int 248 dcons_open(struct dev_open_args *ap) 249 { 250 cdev_t dev = ap->a_head.a_dev; 251 struct tty *tp; 252 int unit, error; 253 254 unit = minor(dev); 255 if (unit != 0) 256 return (ENXIO); 257 258 tp = dev->si_tty = ttymalloc(dev->si_tty); 259 tp->t_oproc = dcons_tty_start; 260 tp->t_param = dcons_tty_param; 261 tp->t_stop = nottystop; 262 tp->t_dev = dev; 263 264 error = 0; 265 266 crit_enter(); 267 if ((tp->t_state & TS_ISOPEN) == 0) { 268 tp->t_state |= TS_CARR_ON; 269 ttychars(tp); 270 tp->t_iflag = TTYDEF_IFLAG; 271 tp->t_oflag = TTYDEF_OFLAG; 272 tp->t_cflag = TTYDEF_CFLAG|CLOCAL; 273 tp->t_lflag = TTYDEF_LFLAG; 274 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 275 ttsetwater(tp); 276 } else if ((tp->t_state & TS_XCLUDE) && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) { 277 crit_exit(); 278 return (EBUSY); 279 } 280 crit_exit(); 281 282 #if __FreeBSD_version < 502113 283 error = (*linesw[tp->t_line].l_open)(dev, tp); 284 #else 285 error = ttyld_open(tp, dev); 286 #endif 287 288 return (error); 289 } 290 291 static int 292 dcons_close(struct dev_close_args *ap) 293 { 294 cdev_t dev = ap->a_head.a_dev; 295 int unit; 296 struct tty *tp; 297 298 unit = minor(dev); 299 if (unit != 0) 300 return (ENXIO); 301 302 tp = dev->si_tty; 303 if (tp->t_state & TS_ISOPEN) { 304 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag); 305 ttyclose(tp); 306 } 307 308 return (0); 309 } 310 311 static int 312 dcons_ioctl(struct dev_ioctl_args *ap) 313 { 314 cdev_t dev = ap->a_head.a_dev; 315 int unit; 316 struct tty *tp; 317 int error; 318 319 unit = minor(dev); 320 if (unit != 0) 321 return (ENXIO); 322 323 tp = dev->si_tty; 324 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data, ap->a_fflag, ap->a_cred); 325 if (error != ENOIOCTL) 326 return (error); 327 328 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag); 329 if (error != ENOIOCTL) 330 return (error); 331 332 return (ENOTTY); 333 } 334 335 static int 336 dcons_tty_param(struct tty *tp, struct termios *t) 337 { 338 tp->t_ispeed = t->c_ispeed; 339 tp->t_ospeed = t->c_ospeed; 340 tp->t_cflag = t->c_cflag; 341 return 0; 342 } 343 344 static void 345 dcons_tty_start(struct tty *tp) 346 { 347 struct dcons_softc *dc; 348 349 dc = (struct dcons_softc *)tp->t_dev->si_drv1; 350 crit_enter(); 351 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) { 352 ttwwakeup(tp); 353 return; 354 } 355 356 tp->t_state |= TS_BUSY; 357 while (tp->t_outq.c_cc != 0) 358 dcons_os_putc(dc, clist_getc(&tp->t_outq)); 359 tp->t_state &= ~TS_BUSY; 360 361 ttwwakeup(tp); 362 crit_exit(); 363 } 364 365 static void 366 dcons_timeout(void *v) 367 { 368 struct tty *tp; 369 struct dcons_softc *dc; 370 int i, c, polltime; 371 372 for (i = 0; i < DCONS_NPORT; i ++) { 373 dc = &sc[i]; 374 tp = ((DEV)dc->dev)->si_tty; 375 while ((c = dcons_os_checkc(dc)) != -1) 376 if (tp->t_state & TS_ISOPEN) 377 #if __FreeBSD_version < 502113 378 (*linesw[tp->t_line].l_rint)(c, tp); 379 #else 380 ttyld_rint(tp, c); 381 #endif 382 } 383 polltime = hz / poll_hz; 384 if (polltime < 1) 385 polltime = 1; 386 callout_reset(&dcons_callout, polltime, dcons_timeout, tp); 387 } 388 389 static void 390 dcons_cnprobe(struct consdev *cp) 391 { 392 #ifdef __DragonFly__ 393 cp->cn_probegood = 1; 394 #elif __FreeBSD_version >= 501109 395 ksprintf(cp->cn_name, "dcons"); 396 #else 397 cp->cn_dev = makedev(CDEV_MAJOR, DCONS_CON); 398 #endif 399 #if DCONS_FORCE_CONSOLE 400 cp->cn_pri = CN_REMOTE; 401 #else 402 cp->cn_pri = CN_NORMAL; 403 #endif 404 } 405 406 static void 407 dcons_cninit(struct consdev *cp) 408 { 409 dcons_drv_init(0); 410 #if CONS_NODEV 411 cp->cn_arg 412 #else 413 cp->cn_private 414 #endif 415 = (void *)&sc[DCONS_CON]; /* share port0 with unit0 */ 416 } 417 418 static void 419 dcons_cninit_fini(struct consdev *cp) 420 { 421 cp->cn_dev = make_dev(&dcons_ops, DCONS_CON, 422 UID_ROOT, GID_WHEEL, 0600, "dcons"); 423 } 424 425 #if CONS_NODEV 426 static int 427 dcons_cngetc(struct consdev *cp) 428 { 429 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 430 return (dcons_os_getc(dc)); 431 } 432 static int 433 dcons_cncheckc(struct consdev *cp) 434 { 435 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 436 return (dcons_os_checkc(dc)); 437 } 438 static void 439 dcons_cnputc(struct consdev *cp, int c) 440 { 441 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 442 dcons_os_putc(dc, c); 443 } 444 #else 445 static int 446 dcons_cngetc(void *private) 447 { 448 struct dcons_softc *dc = (struct dcons_softc *)private; 449 return (dcons_os_getc(dc)); 450 } 451 static int 452 dcons_cncheckc(void *private) 453 { 454 struct dcons_softc *dc = (struct dcons_softc *)private; 455 return (dcons_os_checkc(dc)); 456 } 457 static void 458 dcons_cnputc(void *private, int c) 459 { 460 struct dcons_softc *dc = (struct dcons_softc *)private; 461 dcons_os_putc(dc, c); 462 } 463 #endif 464 465 static int 466 dcons_drv_init(int stage) 467 { 468 #ifdef __i386__ 469 quad_t addr, size; 470 #endif 471 472 if (drv_init) 473 return(drv_init); 474 475 drv_init = -1; 476 477 bzero(&dg, sizeof(dg)); 478 dcons_conf = &dg; 479 dg.cdev = &dcons_consdev; 480 dg.buf = NULL; 481 dg.size = DCONS_BUF_SIZE; 482 483 #ifdef __i386__ 484 if (kgetenv_quad("dcons.addr", &addr) > 0 && 485 kgetenv_quad("dcons.size", &size) > 0) { 486 vm_paddr_t pa; 487 /* 488 * Allow read/write access to dcons buffer. 489 */ 490 for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE) 491 pmap_kmodify_rw((vm_offset_t)(KERNBASE + pa)); 492 cpu_invltlb(); 493 /* XXX P to V */ 494 dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr); 495 dg.size = size; 496 if (dcons_load_buffer(dg.buf, dg.size, sc) < 0) 497 dg.buf = NULL; 498 } 499 #endif 500 if (dg.buf != NULL) 501 goto ok; 502 503 #ifndef KLD_MODULE 504 if (stage == 0) { /* XXX or cold */ 505 /* 506 * DCONS_FORCE_CONSOLE == 1 and statically linked. 507 * called from cninit(). can't use contigmalloc yet . 508 */ 509 dg.buf = (struct dcons_buf *) bssbuf; 510 dcons_init(dg.buf, dg.size, sc); 511 } else 512 #endif 513 { 514 /* 515 * DCONS_FORCE_CONSOLE == 0 or kernel module case. 516 * if the module is loaded after boot, 517 * bssbuf could be non-continuous. 518 */ 519 dg.buf = (struct dcons_buf *) contigmalloc(dg.size, 520 M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul); 521 dcons_init(dg.buf, dg.size, sc); 522 } 523 524 ok: 525 dcons_buf = dg.buf; 526 527 #if DDB && DCONS_FORCE_GDB 528 if (gdb_tab == NULL) { 529 gdb_tab = &dcons_consdev; 530 dcons_consdev.cn_gdbprivate = &sc[DCONS_GDB]; 531 } 532 #endif 533 drv_init = 1; 534 535 return 0; 536 } 537 538 539 static int 540 dcons_attach_port(int port, char *name, int flags) 541 { 542 struct dcons_softc *dc; 543 struct tty *tp; 544 DEV dev; 545 546 dc = &sc[port]; 547 dc->flags = flags; 548 dev = make_dev(&dcons_ops, port, 549 UID_ROOT, GID_WHEEL, 0600, name); 550 dc->dev = (void *)dev; 551 tp = ttymalloc(NULL); 552 553 dev->si_drv1 = (void *)dc; 554 dev->si_tty = tp; 555 556 tp->t_oproc = dcons_tty_start; 557 tp->t_param = dcons_tty_param; 558 tp->t_stop = nottystop; 559 tp->t_dev = dc->dev; 560 561 return(0); 562 } 563 564 static int 565 dcons_attach(void) 566 { 567 int polltime; 568 569 #ifdef __DragonFly__ 570 dev_ops_add(&dcons_ops, -1, 0); 571 #endif 572 dcons_attach_port(DCONS_CON, "dcons", 0); 573 dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB); 574 #if __FreeBSD_version < 500000 575 callout_init(&dcons_callout); 576 #else 577 callout_init(&dcons_callout, 0); 578 #endif 579 polltime = hz / poll_hz; 580 if (polltime < 1) 581 polltime = 1; 582 callout_reset(&dcons_callout, polltime, dcons_timeout, NULL); 583 return(0); 584 } 585 586 static int 587 dcons_detach(int port) 588 { 589 struct tty *tp; 590 struct dcons_softc *dc; 591 592 dc = &sc[port]; 593 594 tp = ((DEV)dc->dev)->si_tty; 595 596 if (tp->t_state & TS_ISOPEN) { 597 kprintf("dcons: still opened\n"); 598 #if __FreeBSD_version < 502113 599 (*linesw[tp->t_line].l_close)(tp, 0); 600 ttyclose(tp); 601 #else 602 ttyld_close(tp, 0); 603 tty_close(tp); 604 #endif 605 } 606 /* XXX 607 * must wait until all device are closed. 608 */ 609 #ifdef __DragonFly__ 610 tsleep((void *)dc, 0, "dcodtc", hz/4); 611 #else 612 tsleep((void *)dc, PWAIT, "dcodtc", hz/4); 613 #endif 614 destroy_dev(dc->dev); 615 616 return(0); 617 } 618 619 620 /* cnXXX works only for FreeBSD-5 */ 621 static int 622 dcons_modevent(module_t mode, int type, void *data) 623 { 624 int err = 0, ret; 625 626 switch (type) { 627 case MOD_LOAD: 628 ret = dcons_drv_init(1); 629 dcons_attach(); 630 #if __FreeBSD_version >= 500000 631 if (ret == 0) { 632 dcons_cnprobe(&dcons_consdev); 633 dcons_cninit(&dcons_consdev); 634 cnadd(&dcons_consdev); 635 } 636 #endif 637 break; 638 case MOD_UNLOAD: 639 kprintf("dcons: unload\n"); 640 callout_stop(&dcons_callout); 641 #if __FreeBSD_version < 502122 642 #if DDB && DCONS_FORCE_GDB 643 #if CONS_NODEV 644 gdb_arg = NULL; 645 #else 646 if (gdb_tab == &dcons_consdev) 647 gdb_tab = NULL; 648 #endif 649 #endif 650 #endif 651 #if __FreeBSD_version >= 500000 652 cnremove(&dcons_consdev); 653 #endif 654 dcons_detach(DCONS_CON); 655 dcons_detach(DCONS_GDB); 656 dg.buf->magic = 0; 657 658 contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF); 659 660 break; 661 case MOD_SHUTDOWN: 662 dg.buf->magic = 0; 663 break; 664 default: 665 err = EOPNOTSUPP; 666 break; 667 } 668 return(err); 669 } 670 671 #if __FreeBSD_version >= 502122 672 /* Debugger interface */ 673 674 static int 675 dcons_dbg_probe(void) 676 { 677 return(DCONS_FORCE_GDB); 678 } 679 680 static void 681 dcons_dbg_init(void) 682 { 683 } 684 685 static void 686 dcons_dbg_term(void) 687 { 688 } 689 690 static void 691 dcons_dbg_putc(int c) 692 { 693 struct dcons_softc *dc = &sc[DCONS_GDB]; 694 dcons_os_putc(dc, c); 695 } 696 697 static int 698 dcons_dbg_checkc(void) 699 { 700 struct dcons_softc *dc = &sc[DCONS_GDB]; 701 return (dcons_os_checkc(dc)); 702 } 703 704 static int 705 dcons_dbg_getc(void) 706 { 707 struct dcons_softc *dc = &sc[DCONS_GDB]; 708 return (dcons_os_getc(dc)); 709 } 710 #endif 711 712 DEV_MODULE(dcons, dcons_modevent, NULL); 713 MODULE_VERSION(dcons, DCONS_VERSION); 714