dcons_os.c revision 153110
1159248Srwatson/*- 2159248Srwatson * 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: head/sys/dev/dcons/dcons_os.c 153110 2005-12-05 11:58:35Z ru $ 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/proc.h> 52#include <sys/ucred.h> 53 54#include <machine/bus.h> 55 56#ifdef __DragonFly__ 57#include "dcons.h" 58#include "dcons_os.h" 59#else 60#include <dev/dcons/dcons.h> 61#include <dev/dcons/dcons_os.h> 62#endif 63 64#include <ddb/ddb.h> 65#include <sys/reboot.h> 66 67#include <sys/sysctl.h> 68 69#include <vm/vm.h> 70#include <vm/vm_param.h> 71#include <vm/pmap.h> 72 73#include "opt_ddb.h" 74#include "opt_comconsole.h" 75#include "opt_dcons.h" 76 77#ifndef DCONS_POLL_HZ 78#define DCONS_POLL_HZ 100 79#endif 80 81#ifndef DCONS_BUF_SIZE 82#define DCONS_BUF_SIZE (16*1024) 83#endif 84 85#ifndef DCONS_FORCE_CONSOLE 86#define DCONS_FORCE_CONSOLE 0 /* Mostly for FreeBSD-4/DragonFly */ 87#endif 88 89#ifndef DCONS_FORCE_GDB 90#define DCONS_FORCE_GDB 1 91#endif 92 93#if __FreeBSD_version >= 500101 94#define CONS_NODEV 1 95#if __FreeBSD_version < 502122 96static struct consdev gdbconsdev; 97#endif 98#endif 99 100static d_open_t dcons_open; 101static d_close_t dcons_close; 102#if defined(__DragonFly__) || __FreeBSD_version < 500104 103static d_ioctl_t dcons_ioctl; 104#endif 105 106static struct cdevsw dcons_cdevsw = { 107#ifdef __DragonFly__ 108#define CDEV_MAJOR 184 109 "dcons", CDEV_MAJOR, D_TTY, NULL, 0, 110 dcons_open, dcons_close, ttyread, ttywrite, dcons_ioctl, 111 ttypoll, nommap, nostrategy, nodump, nopsize, 112#elif __FreeBSD_version >= 500104 113 .d_version = D_VERSION, 114 .d_open = dcons_open, 115 .d_close = dcons_close, 116 .d_name = "dcons", 117 .d_flags = D_TTY | D_NEEDGIANT, 118#else 119#define CDEV_MAJOR 184 120 /* open */ dcons_open, 121 /* close */ dcons_close, 122 /* read */ ttyread, 123 /* write */ ttywrite, 124 /* ioctl */ dcons_ioctl, 125 /* poll */ ttypoll, 126 /* mmap */ nommap, 127 /* strategy */ nostrategy, 128 /* name */ "dcons", 129 /* major */ CDEV_MAJOR, 130 /* dump */ nodump, 131 /* psize */ nopsize, 132 /* flags */ D_TTY, 133#endif 134}; 135 136#ifndef KLD_MODULE 137static char bssbuf[DCONS_BUF_SIZE]; /* buf in bss */ 138#endif 139 140/* global data */ 141static struct dcons_global dg; 142struct dcons_global *dcons_conf; 143static int poll_hz = DCONS_POLL_HZ; 144 145static struct dcons_softc sc[DCONS_NPORT]; 146 147SYSCTL_NODE(_kern, OID_AUTO, dcons, CTLFLAG_RD, 0, "Dumb Console"); 148SYSCTL_INT(_kern_dcons, OID_AUTO, poll_hz, CTLFLAG_RW, &poll_hz, 0, 149 "dcons polling rate"); 150 151static int drv_init = 0; 152static struct callout dcons_callout; 153struct dcons_buf *dcons_buf; /* for local dconschat */ 154 155#ifdef __DragonFly__ 156#define DEV dev_t 157#define THREAD d_thread_t 158#elif __FreeBSD_version < 500000 159#define DEV dev_t 160#define THREAD struct proc 161#else 162#define DEV struct cdev * 163#define THREAD struct thread 164#endif 165 166 167static void dcons_tty_start(struct tty *); 168static int dcons_tty_param(struct tty *, struct termios *); 169static void dcons_timeout(void *); 170static int dcons_drv_init(int); 171 172static cn_probe_t dcons_cnprobe; 173static cn_init_t dcons_cninit; 174static cn_getc_t dcons_cngetc; 175static cn_checkc_t dcons_cncheckc; 176static cn_putc_t dcons_cnputc; 177 178CONS_DRIVER(dcons, dcons_cnprobe, dcons_cninit, NULL, dcons_cngetc, 179 dcons_cncheckc, dcons_cnputc, NULL); 180 181#if __FreeBSD_version >= 502122 182static gdb_probe_f dcons_dbg_probe; 183static gdb_init_f dcons_dbg_init; 184static gdb_term_f dcons_dbg_term; 185static gdb_getc_f dcons_dbg_getc; 186static gdb_checkc_f dcons_dbg_checkc; 187static gdb_putc_f dcons_dbg_putc; 188 189GDB_DBGPORT(dcons, dcons_dbg_probe, dcons_dbg_init, dcons_dbg_term, 190 dcons_dbg_checkc, dcons_dbg_getc, dcons_dbg_putc); 191 192extern struct gdb_dbgport *gdb_cur; 193#endif 194 195#if (defined(KDB) || defined(DDB)) && defined(ALT_BREAK_TO_DEBUGGER) 196static int 197dcons_check_break(struct dcons_softc *dc, int c) 198{ 199 if (c < 0) 200 return (c); 201 202#if __FreeBSD_version >= 502122 203 if (kdb_alt_break(c, &dc->brk_state)) { 204 if ((dc->flags & DC_GDB) != 0) { 205 if (gdb_cur == &dcons_gdb_dbgport) { 206 kdb_dbbe_select("gdb"); 207 breakpoint(); 208 } 209 } else 210 breakpoint(); 211 } 212#else 213 switch (dc->brk_state) { 214 case STATE1: 215 if (c == KEY_TILDE) 216 dc->brk_state = STATE2; 217 else 218 dc->brk_state = STATE0; 219 break; 220 case STATE2: 221 dc->brk_state = STATE0; 222 if (c == KEY_CTRLB) { 223#if DCONS_FORCE_GDB 224 if (dc->flags & DC_GDB) 225 boothowto |= RB_GDB; 226#endif 227 breakpoint(); 228 } 229 } 230 if (c == KEY_CR) 231 dc->brk_state = STATE1; 232#endif 233 return (c); 234} 235#else 236#define dcons_check_break(dc, c) (c) 237#endif 238 239static int 240dcons_os_checkc(struct dcons_softc *dc) 241{ 242 int c; 243 244 if (dg.dma_tag != NULL) 245 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD); 246 247 c = dcons_check_break(dc, dcons_checkc(dc)); 248 249 if (dg.dma_tag != NULL) 250 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD); 251 252 return (c); 253} 254 255static int 256dcons_os_getc(struct dcons_softc *dc) 257{ 258 int c; 259 260 while ((c = dcons_os_checkc(dc)) == -1); 261 262 return (c & 0xff); 263} 264 265static void 266dcons_os_putc(struct dcons_softc *dc, int c) 267{ 268 if (dg.dma_tag != NULL) 269 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE); 270 271 dcons_putc(dc, c); 272 273 if (dg.dma_tag != NULL) 274 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE); 275} 276static int 277dcons_open(DEV dev, int flag, int mode, THREAD *td) 278{ 279 struct tty *tp; 280 int unit, error, s; 281 282 unit = minor(dev); 283 if (unit != 0) 284 return (ENXIO); 285 286 tp = dev->si_tty = ttymalloc(dev->si_tty); 287 tp->t_oproc = dcons_tty_start; 288 tp->t_param = dcons_tty_param; 289 tp->t_stop = nottystop; 290 tp->t_dev = dev; 291 292 error = 0; 293 294 s = spltty(); 295 if ((tp->t_state & TS_ISOPEN) == 0) { 296 tp->t_state |= TS_CARR_ON; 297 ttyconsolemode(tp, 0); 298 } else if ((tp->t_state & TS_XCLUDE) && suser(td)) { 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 313static int 314dcons_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 338static int 339dcons_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 362static int 363dcons_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 371static void 372dcons_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 393static void 394dcons_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 417static void 418dcons_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 435static void 436dcons_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#if CONS_NODEV 448static int 449dcons_cngetc(struct consdev *cp) 450{ 451 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 452 return (dcons_os_getc(dc)); 453} 454static int 455dcons_cncheckc(struct consdev *cp) 456{ 457 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 458 return (dcons_os_checkc(dc)); 459} 460static void 461dcons_cnputc(struct consdev *cp, int c) 462{ 463 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 464 dcons_os_putc(dc, c); 465} 466#else 467static int 468dcons_cngetc(DEV dev) 469{ 470 struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1; 471 return (dcons_os_getc(dc)); 472} 473static int 474dcons_cncheckc(DEV dev) 475{ 476 struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1; 477 return (dcons_os_checkc(dc)); 478} 479static void 480dcons_cnputc(DEV dev, int c) 481{ 482 struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1; 483 dcons_os_putc(dc, c); 484} 485#endif 486 487static int 488dcons_drv_init(int stage) 489{ 490#if defined(__i386__) || defined(__amd64__) 491 quad_t addr, size; 492#endif 493 494 if (drv_init) 495 return(drv_init); 496 497 drv_init = -1; 498 499 bzero(&dg, sizeof(dg)); 500 dcons_conf = &dg; 501 dg.cdev = &dcons_consdev; 502 dg.buf = NULL; 503 dg.size = DCONS_BUF_SIZE; 504 505#if defined(__i386__) || defined(__amd64__) 506 if (getenv_quad("dcons.addr", &addr) > 0 && 507 getenv_quad("dcons.size", &size) > 0) { 508#ifdef __i386__ 509 vm_paddr_t pa; 510 /* 511 * Allow read/write access to dcons buffer. 512 */ 513 for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE) 514 *vtopte(KERNBASE + pa) |= PG_RW; 515 invltlb(); 516#endif 517 /* XXX P to V */ 518 dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr); 519 dg.size = size; 520 if (dcons_load_buffer(dg.buf, dg.size, sc) < 0) 521 dg.buf = NULL; 522 } 523#endif 524 if (dg.buf != NULL) 525 goto ok; 526 527#ifndef KLD_MODULE 528 if (stage == 0) { /* XXX or cold */ 529 /* 530 * DCONS_FORCE_CONSOLE == 1 and statically linked. 531 * called from cninit(). can't use contigmalloc yet . 532 */ 533 dg.buf = (struct dcons_buf *) bssbuf; 534 dcons_init(dg.buf, dg.size, sc); 535 } else 536#endif 537 { 538 /* 539 * DCONS_FORCE_CONSOLE == 0 or kernel module case. 540 * if the module is loaded after boot, 541 * bssbuf could be non-continuous. 542 */ 543 dg.buf = (struct dcons_buf *) contigmalloc(dg.size, 544 M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul); 545 dcons_init(dg.buf, dg.size, sc); 546 } 547 548ok: 549 dcons_buf = dg.buf; 550 551#if __FreeBSD_version < 502122 552#if defined(DDB) && DCONS_FORCE_GDB 553#if CONS_NODEV 554 gdbconsdev.cn_arg = (void *)&sc[DCONS_GDB]; 555#if __FreeBSD_version >= 501109 556 sprintf(gdbconsdev.cn_name, "dgdb"); 557#endif 558 gdb_arg = &gdbconsdev; 559#elif defined(__DragonFly__) 560 gdbdev = make_dev(&dcons_cdevsw, DCONS_GDB, 561 UID_ROOT, GID_WHEEL, 0600, "dgdb"); 562#else 563 gdbdev = makedev(CDEV_MAJOR, DCONS_GDB); 564#endif 565 gdb_getc = dcons_cngetc; 566 gdb_putc = dcons_cnputc; 567#endif 568#endif 569 drv_init = 1; 570 571 return 0; 572} 573 574 575static int 576dcons_attach_port(int port, char *name, int flags) 577{ 578 struct dcons_softc *dc; 579 struct tty *tp; 580 DEV dev; 581 582 dc = &sc[port]; 583 dc->flags = flags; 584 dev = make_dev(&dcons_cdevsw, port, 585 UID_ROOT, GID_WHEEL, 0600, name); 586 dc->dev = (void *)dev; 587 tp = ttymalloc(NULL); 588 589 dev->si_drv1 = (void *)dc; 590 dev->si_tty = tp; 591 592 tp->t_oproc = dcons_tty_start; 593 tp->t_param = dcons_tty_param; 594 tp->t_stop = nottystop; 595 tp->t_dev = dc->dev; 596 597 return(0); 598} 599 600static int 601dcons_attach(void) 602{ 603 int polltime; 604 605#ifdef __DragonFly__ 606 cdevsw_add(&dcons_cdevsw, -1, 0); 607#endif 608 dcons_attach_port(DCONS_CON, "dcons", 0); 609 dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB); 610#if __FreeBSD_version < 500000 611 callout_init(&dcons_callout); 612#else 613 callout_init(&dcons_callout, 0); 614#endif 615 polltime = hz / poll_hz; 616 if (polltime < 1) 617 polltime = 1; 618 callout_reset(&dcons_callout, polltime, dcons_timeout, NULL); 619 return(0); 620} 621 622static int 623dcons_detach(int port) 624{ 625 struct tty *tp; 626 struct dcons_softc *dc; 627 628 dc = &sc[port]; 629 630 tp = ((DEV)dc->dev)->si_tty; 631 632 if (tp->t_state & TS_ISOPEN) { 633 printf("dcons: still opened\n"); 634#if __FreeBSD_version < 502113 635 (*linesw[tp->t_line].l_close)(tp, 0); 636 tp->t_gen++; 637 ttyclose(tp); 638 ttwakeup(tp); 639 ttwwakeup(tp); 640#else 641 ttyld_close(tp, 0); 642 tty_close(tp); 643#endif 644 } 645 /* XXX 646 * must wait until all device are closed. 647 */ 648#ifdef __DragonFly__ 649 tsleep((void *)dc, 0, "dcodtc", hz/4); 650#else 651 tsleep((void *)dc, PWAIT, "dcodtc", hz/4); 652#endif 653 destroy_dev(dc->dev); 654 655 return(0); 656} 657 658 659/* cnXXX works only for FreeBSD-5 */ 660static int 661dcons_modevent(module_t mode, int type, void *data) 662{ 663 int err = 0, ret; 664 665 switch (type) { 666 case MOD_LOAD: 667 ret = dcons_drv_init(1); 668 dcons_attach(); 669#if __FreeBSD_version >= 500000 670 if (ret == 0) { 671 dcons_cnprobe(&dcons_consdev); 672 dcons_cninit(&dcons_consdev); 673 cnadd(&dcons_consdev); 674 } 675#endif 676 break; 677 case MOD_UNLOAD: 678 printf("dcons: unload\n"); 679 callout_stop(&dcons_callout); 680#if __FreeBSD_version < 502122 681#if defined(DDB) && DCONS_FORCE_GDB 682#if CONS_NODEV 683 gdb_arg = NULL; 684#else 685 gdbdev = NULL; 686#endif 687#endif 688#endif 689#if __FreeBSD_version >= 500000 690 cnremove(&dcons_consdev); 691#endif 692 dcons_detach(DCONS_CON); 693 dcons_detach(DCONS_GDB); 694 dg.buf->magic = 0; 695 696 contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF); 697 698 break; 699 case MOD_SHUTDOWN: 700 dg.buf->magic = 0; 701 break; 702 default: 703 err = EOPNOTSUPP; 704 break; 705 } 706 return(err); 707} 708 709#if __FreeBSD_version >= 502122 710/* Debugger interface */ 711 712static int 713dcons_dbg_probe(void) 714{ 715 return(DCONS_FORCE_GDB); 716} 717 718static void 719dcons_dbg_init(void) 720{ 721} 722 723static void 724dcons_dbg_term(void) 725{ 726} 727 728static void 729dcons_dbg_putc(int c) 730{ 731 struct dcons_softc *dc = &sc[DCONS_GDB]; 732 dcons_os_putc(dc, c); 733} 734 735static int 736dcons_dbg_checkc(void) 737{ 738 struct dcons_softc *dc = &sc[DCONS_GDB]; 739 return (dcons_os_checkc(dc)); 740} 741 742static int 743dcons_dbg_getc(void) 744{ 745 struct dcons_softc *dc = &sc[DCONS_GDB]; 746 return (dcons_os_getc(dc)); 747} 748#endif 749 750DEV_MODULE(dcons, dcons_modevent, NULL); 751MODULE_VERSION(dcons, DCONS_VERSION); 752