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