1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 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 37 #include <sys/param.h> 38 #include <sys/eventhandler.h> 39 #include <sys/kdb.h> 40 #include <gdb/gdb.h> 41 #include <sys/kernel.h> 42 #include <sys/module.h> 43 #include <sys/systm.h> 44 #include <sys/types.h> 45 #include <sys/conf.h> 46 #include <sys/cons.h> 47 #include <sys/consio.h> 48 #include <sys/tty.h> 49 #include <sys/malloc.h> 50 #include <sys/priv.h> 51 #include <sys/proc.h> 52 #include <sys/ucred.h> 53 54 #include <machine/atomic.h> 55 #include <machine/bus.h> 56 57 #include <dev/dcons/dcons.h> 58 #include <dev/dcons/dcons_os.h> 59 60 #include <ddb/ddb.h> 61 #include <sys/reboot.h> 62 63 #include <sys/sysctl.h> 64 65 #include <vm/vm.h> 66 #include <vm/vm_param.h> 67 #include <vm/pmap.h> 68 69 #include "opt_dcons.h" 70 #include "opt_kdb.h" 71 #include "opt_gdb.h" 72 #include "opt_ddb.h" 73 74 75 #ifndef DCONS_POLL_HZ 76 #define DCONS_POLL_HZ 25 77 #endif 78 79 #ifndef DCONS_POLL_IDLE 80 #define DCONS_POLL_IDLE 256 81 #endif 82 83 #ifndef DCONS_BUF_SIZE 84 #define DCONS_BUF_SIZE (16*1024) 85 #endif 86 87 #ifndef DCONS_FORCE_CONSOLE 88 #define DCONS_FORCE_CONSOLE 0 /* Mostly for FreeBSD-4/DragonFly */ 89 #endif 90 91 #ifndef KLD_MODULE 92 static char bssbuf[DCONS_BUF_SIZE]; /* buf in bss */ 93 #endif 94 95 /* global data */ 96 static struct dcons_global dg; 97 struct dcons_global *dcons_conf; 98 static int poll_hz = DCONS_POLL_HZ; 99 static u_int poll_idle = DCONS_POLL_HZ * DCONS_POLL_IDLE; 100 101 static struct dcons_softc sc[DCONS_NPORT]; 102 103 static SYSCTL_NODE(_kern, OID_AUTO, dcons, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 104 "Dumb Console"); 105 SYSCTL_INT(_kern_dcons, OID_AUTO, poll_hz, CTLFLAG_RW, &poll_hz, 0, 106 "dcons polling rate"); 107 108 static int drv_init = 0; 109 static struct callout dcons_callout; 110 struct dcons_buf *dcons_buf; /* for local dconschat */ 111 112 static void dcons_timeout(void *); 113 static int dcons_drv_init(int); 114 115 static cn_probe_t dcons_cnprobe; 116 static cn_init_t dcons_cninit; 117 static cn_term_t dcons_cnterm; 118 static cn_getc_t dcons_cngetc; 119 static cn_putc_t dcons_cnputc; 120 static cn_grab_t dcons_cngrab; 121 static cn_ungrab_t dcons_cnungrab; 122 123 CONSOLE_DRIVER(dcons); 124 125 #if defined(GDB) 126 static gdb_probe_f dcons_dbg_probe; 127 static gdb_init_f dcons_dbg_init; 128 static gdb_term_f dcons_dbg_term; 129 static gdb_getc_f dcons_dbg_getc; 130 static gdb_putc_f dcons_dbg_putc; 131 132 GDB_DBGPORT(dcons, dcons_dbg_probe, dcons_dbg_init, dcons_dbg_term, 133 dcons_dbg_getc, dcons_dbg_putc); 134 135 extern struct gdb_dbgport *gdb_cur; 136 #endif 137 138 static tsw_outwakeup_t dcons_outwakeup; 139 static tsw_free_t dcons_free; 140 141 static struct ttydevsw dcons_ttydevsw = { 142 .tsw_flags = TF_NOPREFIX, 143 .tsw_outwakeup = dcons_outwakeup, 144 .tsw_free = dcons_free, 145 }; 146 147 static int dcons_close_refs; 148 149 #if (defined(GDB) || defined(DDB)) 150 static int 151 dcons_check_break(struct dcons_softc *dc, int c) 152 { 153 154 if (c < 0) 155 return (c); 156 157 #ifdef GDB 158 if ((dc->flags & DC_GDB) != 0 && gdb_cur == &dcons_gdb_dbgport) 159 kdb_alt_break_gdb(c, &dc->brk_state); 160 else 161 #endif 162 kdb_alt_break(c, &dc->brk_state); 163 164 return (c); 165 } 166 #else 167 #define dcons_check_break(dc, c) (c) 168 #endif 169 170 static int 171 dcons_os_checkc_nopoll(struct dcons_softc *dc) 172 { 173 int c; 174 175 if (dg.dma_tag != NULL) 176 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD); 177 178 c = dcons_check_break(dc, dcons_checkc(dc)); 179 180 if (dg.dma_tag != NULL) 181 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD); 182 183 return (c); 184 } 185 186 static int 187 dcons_os_checkc(struct dcons_softc *dc) 188 { 189 EVENTHANDLER_INVOKE(dcons_poll, 0); 190 return (dcons_os_checkc_nopoll(dc)); 191 } 192 193 static void 194 dcons_os_putc(struct dcons_softc *dc, int c) 195 { 196 if (dg.dma_tag != NULL) 197 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE); 198 199 dcons_putc(dc, c); 200 201 if (dg.dma_tag != NULL) 202 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE); 203 } 204 205 static void 206 dcons_free(void *xsc __unused) 207 { 208 209 /* Our deferred free has arrived, now we're waiting for one fewer. */ 210 atomic_subtract_rel_int(&dcons_close_refs, 1); 211 } 212 213 static void 214 dcons_outwakeup(struct tty *tp) 215 { 216 struct dcons_softc *dc; 217 char ch; 218 219 dc = tty_softc(tp); 220 221 while (ttydisc_getc(tp, &ch, sizeof ch) != 0) 222 dcons_os_putc(dc, ch); 223 } 224 225 static void 226 dcons_timeout(void *v) 227 { 228 struct tty *tp; 229 struct dcons_softc *dc; 230 int i, c, polltime; 231 232 for (i = 0; i < DCONS_NPORT; i ++) { 233 dc = &sc[i]; 234 tp = dc->tty; 235 236 tty_lock(tp); 237 while ((c = dcons_os_checkc_nopoll(dc)) != -1) { 238 ttydisc_rint(tp, c, 0); 239 poll_idle = 0; 240 } 241 ttydisc_rint_done(tp); 242 tty_unlock(tp); 243 } 244 poll_idle++; 245 polltime = hz; 246 if (poll_idle <= (poll_hz * DCONS_POLL_IDLE)) 247 polltime /= poll_hz; 248 callout_reset(&dcons_callout, polltime, dcons_timeout, tp); 249 } 250 251 static void 252 dcons_cnprobe(struct consdev *cp) 253 { 254 sprintf(cp->cn_name, "dcons"); 255 #if DCONS_FORCE_CONSOLE 256 cp->cn_pri = CN_REMOTE; 257 #else 258 cp->cn_pri = CN_NORMAL; 259 #endif 260 } 261 262 static void 263 dcons_cninit(struct consdev *cp) 264 { 265 dcons_drv_init(0); 266 cp->cn_arg = (void *)&sc[DCONS_CON]; /* share port0 with unit0 */ 267 } 268 269 static void 270 dcons_cnterm(struct consdev *cp) 271 { 272 } 273 274 static void 275 dcons_cngrab(struct consdev *cp) 276 { 277 } 278 279 static void 280 dcons_cnungrab(struct consdev *cp) 281 { 282 } 283 284 static int 285 dcons_cngetc(struct consdev *cp) 286 { 287 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 288 return (dcons_os_checkc(dc)); 289 } 290 291 static void 292 dcons_cnputc(struct consdev *cp, int c) 293 { 294 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 295 dcons_os_putc(dc, c); 296 } 297 298 static int 299 dcons_drv_init(int stage) 300 { 301 #if defined(__i386__) || defined(__amd64__) 302 quad_t addr, size; 303 #endif 304 305 if (drv_init) 306 return(drv_init); 307 308 drv_init = -1; 309 310 bzero(&dg, sizeof(dg)); 311 dcons_conf = &dg; 312 dg.cdev = &dcons_consdev; 313 dg.buf = NULL; 314 dg.size = DCONS_BUF_SIZE; 315 316 #if defined(__i386__) || defined(__amd64__) 317 if (getenv_quad("dcons.addr", &addr) > 0 && 318 getenv_quad("dcons.size", &size) > 0) { 319 #ifdef __i386__ 320 vm_paddr_t pa; 321 /* 322 * Allow read/write access to dcons buffer. 323 */ 324 for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE) 325 pmap_ksetrw(PMAP_MAP_LOW + pa); 326 invltlb(); 327 #endif 328 /* XXX P to V */ 329 #ifdef __amd64__ 330 dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr); 331 #else /* __i386__ */ 332 dg.buf = (struct dcons_buf *)(vm_offset_t)(PMAP_MAP_LOW + 333 addr); 334 #endif 335 dg.size = size; 336 if (dcons_load_buffer(dg.buf, dg.size, sc) < 0) 337 dg.buf = NULL; 338 } 339 #endif 340 if (dg.buf != NULL) 341 goto ok; 342 343 #ifndef KLD_MODULE 344 if (stage == 0) { /* XXX or cold */ 345 /* 346 * DCONS_FORCE_CONSOLE == 1 and statically linked. 347 * called from cninit(). can't use contigmalloc yet . 348 */ 349 dg.buf = (struct dcons_buf *) bssbuf; 350 dcons_init(dg.buf, dg.size, sc); 351 } else 352 #endif 353 { 354 /* 355 * DCONS_FORCE_CONSOLE == 0 or kernel module case. 356 * if the module is loaded after boot, 357 * bssbuf could be non-continuous. 358 */ 359 dg.buf = (struct dcons_buf *) contigmalloc(dg.size, 360 M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul); 361 if (dg.buf == NULL) 362 return (-1); 363 dcons_init(dg.buf, dg.size, sc); 364 } 365 366 ok: 367 dcons_buf = dg.buf; 368 369 drv_init = 1; 370 371 return 0; 372 } 373 374 375 static int 376 dcons_attach_port(int port, char *name, int flags) 377 { 378 struct dcons_softc *dc; 379 struct tty *tp; 380 381 dc = &sc[port]; 382 tp = tty_alloc(&dcons_ttydevsw, dc); 383 dc->flags = flags; 384 dc->tty = tp; 385 tty_init_console(tp, 0); 386 tty_makedev(tp, NULL, "%s", name); 387 return(0); 388 } 389 390 static int 391 dcons_attach(void) 392 { 393 int polltime; 394 395 dcons_attach_port(DCONS_CON, "dcons", 0); 396 dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB); 397 callout_init(&dcons_callout, 1); 398 polltime = hz / poll_hz; 399 callout_reset(&dcons_callout, polltime, dcons_timeout, NULL); 400 return(0); 401 } 402 403 static int 404 dcons_detach(int port) 405 { 406 struct tty *tp; 407 struct dcons_softc *dc; 408 409 dc = &sc[port]; 410 tp = dc->tty; 411 412 /* tty_rel_gone() schedules a deferred free callback, count it. */ 413 atomic_add_int(&dcons_close_refs, 1); 414 tty_lock(tp); 415 tty_rel_gone(tp); 416 417 return(0); 418 } 419 420 static int 421 dcons_modevent(module_t mode, int type, void *data) 422 { 423 int err = 0, ret; 424 425 switch (type) { 426 case MOD_LOAD: 427 ret = dcons_drv_init(1); 428 if (ret != -1) 429 dcons_attach(); 430 if (ret == 0) { 431 dcons_cnprobe(&dcons_consdev); 432 dcons_cninit(&dcons_consdev); 433 cnadd(&dcons_consdev); 434 } 435 break; 436 case MOD_UNLOAD: 437 printf("dcons: unload\n"); 438 if (drv_init == 1) { 439 callout_stop(&dcons_callout); 440 cnremove(&dcons_consdev); 441 dcons_detach(DCONS_CON); 442 dcons_detach(DCONS_GDB); 443 dg.buf->magic = 0; 444 445 free(dg.buf, M_DEVBUF); 446 } 447 448 /* Wait for tty deferred free callbacks to complete. */ 449 while (atomic_load_acq_int(&dcons_close_refs) > 0) 450 pause_sbt("dcunld", mstosbt(50), mstosbt(10), 0); 451 break; 452 case MOD_SHUTDOWN: 453 #if 0 /* Keep connection after halt */ 454 dg.buf->magic = 0; 455 #endif 456 break; 457 default: 458 err = EOPNOTSUPP; 459 break; 460 } 461 return(err); 462 } 463 464 #if defined(GDB) 465 /* Debugger interface */ 466 467 static int 468 dcons_os_getc(struct dcons_softc *dc) 469 { 470 int c; 471 472 while ((c = dcons_os_checkc(dc)) == -1); 473 474 return (c & 0xff); 475 } 476 477 static int 478 dcons_dbg_probe(void) 479 { 480 int dcons_gdb; 481 482 if (getenv_int("dcons_gdb", &dcons_gdb) == 0) 483 return (-1); 484 return (dcons_gdb); 485 } 486 487 static void 488 dcons_dbg_init(void) 489 { 490 } 491 492 static void 493 dcons_dbg_term(void) 494 { 495 } 496 497 static void 498 dcons_dbg_putc(int c) 499 { 500 struct dcons_softc *dc = &sc[DCONS_GDB]; 501 dcons_os_putc(dc, c); 502 } 503 504 static int 505 dcons_dbg_getc(void) 506 { 507 struct dcons_softc *dc = &sc[DCONS_GDB]; 508 return (dcons_os_getc(dc)); 509 } 510 #endif 511 512 DEV_MODULE(dcons, dcons_modevent, NULL); 513 MODULE_VERSION(dcons, DCONS_VERSION); 514