1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1988 University of Utah. 5 * Copyright (c) 1991 The Regents of the University of California. 6 * Copyright (c) 1999 Michael Smith 7 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org> 8 * 9 * All rights reserved. 10 * 11 * This code is derived from software contributed to Berkeley by 12 * the Systems Programming Group of the University of Utah Computer 13 * Science Department. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 3. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * from: @(#)cons.c 7.2 (Berkeley) 5/9/91 40 */ 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include "opt_ddb.h" 46 #include "opt_syscons.h" 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/lock.h> 51 #include <sys/mutex.h> 52 #include <sys/conf.h> 53 #include <sys/cons.h> 54 #include <sys/fcntl.h> 55 #include <sys/kbio.h> 56 #include <sys/kdb.h> 57 #include <sys/kernel.h> 58 #include <sys/malloc.h> 59 #include <sys/msgbuf.h> 60 #include <sys/namei.h> 61 #include <sys/priv.h> 62 #include <sys/proc.h> 63 #include <sys/queue.h> 64 #include <sys/reboot.h> 65 #include <sys/sysctl.h> 66 #include <sys/sbuf.h> 67 #include <sys/tty.h> 68 #include <sys/uio.h> 69 #include <sys/vnode.h> 70 71 #include <ddb/ddb.h> 72 73 #include <dev/kbd/kbdreg.h> 74 75 #include <machine/cpu.h> 76 #include <machine/clock.h> 77 78 static MALLOC_DEFINE(M_TTYCONS, "tty console", "tty console handling"); 79 80 struct cn_device { 81 STAILQ_ENTRY(cn_device) cnd_next; 82 struct consdev *cnd_cn; 83 }; 84 85 #define CNDEVPATHMAX 32 86 #define CNDEVTAB_SIZE 4 87 static struct cn_device cn_devtab[CNDEVTAB_SIZE]; 88 static STAILQ_HEAD(, cn_device) cn_devlist = 89 STAILQ_HEAD_INITIALIZER(cn_devlist); 90 91 int cons_avail_mask = 0; /* Bit mask. Each registered low level console 92 * which is currently unavailable for inpit 93 * (i.e., if it is in graphics mode) will have 94 * this bit cleared. 95 */ 96 static int cn_mute; 97 static char *consbuf; /* buffer used by `consmsgbuf' */ 98 static struct callout conscallout; /* callout for outputting to constty */ 99 struct msgbuf consmsgbuf; /* message buffer for console tty */ 100 static u_char console_pausing; /* pause after each line during probe */ 101 static char *console_pausestr= 102 "<pause; press any key to proceed to next line or '.' to end pause mode>"; 103 struct tty *constty; /* pointer to console "window" tty */ 104 static struct mtx cnputs_mtx; /* Mutex for cnputs(). */ 105 static int use_cnputs_mtx = 0; /* != 0 if cnputs_mtx locking reqd. */ 106 107 static void constty_timeout(void *arg); 108 109 static struct consdev cons_consdev; 110 DATA_SET(cons_set, cons_consdev); 111 SET_DECLARE(cons_set, struct consdev); 112 113 /* 114 * Stub for configurations that don't actually have a keyboard driver. Inclusion 115 * of kbd.c is contingent on any number of keyboard/console drivers being 116 * present in the kernel; rather than trying to catch them all, we'll just 117 * maintain this weak kbdinit that will be overridden by the strong version in 118 * kbd.c if it's present. 119 */ 120 __weak_symbol void 121 kbdinit(void) 122 { 123 124 } 125 126 void 127 cninit(void) 128 { 129 struct consdev *best_cn, *cn, **list; 130 131 /* 132 * Check if we should mute the console (for security reasons perhaps) 133 * It can be changes dynamically using sysctl kern.consmute 134 * once we are up and going. 135 * 136 */ 137 cn_mute = ((boothowto & (RB_MUTE 138 |RB_SINGLE 139 |RB_VERBOSE 140 |RB_ASKNAME)) == RB_MUTE); 141 142 /* 143 * Bring up the kbd layer just in time for cnprobe. Console drivers 144 * have a dependency on kbd being ready, so this fits nicely between the 145 * machdep callers of cninit() and MI probing/initialization of consoles 146 * here. 147 */ 148 kbdinit(); 149 150 /* 151 * Find the first console with the highest priority. 152 */ 153 best_cn = NULL; 154 SET_FOREACH(list, cons_set) { 155 cn = *list; 156 cnremove(cn); 157 /* Skip cons_consdev. */ 158 if (cn->cn_ops == NULL) 159 continue; 160 cn->cn_ops->cn_probe(cn); 161 if (cn->cn_pri == CN_DEAD) 162 continue; 163 if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri) 164 best_cn = cn; 165 if (boothowto & RB_MULTIPLE) { 166 /* 167 * Initialize console, and attach to it. 168 */ 169 cn->cn_ops->cn_init(cn); 170 cnadd(cn); 171 } 172 } 173 if (best_cn == NULL) 174 return; 175 if ((boothowto & RB_MULTIPLE) == 0) { 176 best_cn->cn_ops->cn_init(best_cn); 177 cnadd(best_cn); 178 } 179 if (boothowto & RB_PAUSE) 180 console_pausing = 1; 181 /* 182 * Make the best console the preferred console. 183 */ 184 cnselect(best_cn); 185 186 #ifdef EARLY_PRINTF 187 /* 188 * Release early console. 189 */ 190 early_putc = NULL; 191 #endif 192 } 193 194 void 195 cninit_finish() 196 { 197 console_pausing = 0; 198 } 199 200 /* add a new physical console to back the virtual console */ 201 int 202 cnadd(struct consdev *cn) 203 { 204 struct cn_device *cnd; 205 int i; 206 207 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) 208 if (cnd->cnd_cn == cn) 209 return (0); 210 for (i = 0; i < CNDEVTAB_SIZE; i++) { 211 cnd = &cn_devtab[i]; 212 if (cnd->cnd_cn == NULL) 213 break; 214 } 215 if (cnd->cnd_cn != NULL) 216 return (ENOMEM); 217 cnd->cnd_cn = cn; 218 if (cn->cn_name[0] == '\0') { 219 /* XXX: it is unclear if/where this print might output */ 220 printf("WARNING: console at %p has no name\n", cn); 221 } 222 STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next); 223 if (STAILQ_FIRST(&cn_devlist) == cnd) 224 ttyconsdev_select(cnd->cnd_cn->cn_name); 225 226 /* Add device to the active mask. */ 227 cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0); 228 229 return (0); 230 } 231 232 void 233 cnremove(struct consdev *cn) 234 { 235 struct cn_device *cnd; 236 int i; 237 238 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) { 239 if (cnd->cnd_cn != cn) 240 continue; 241 if (STAILQ_FIRST(&cn_devlist) == cnd) 242 ttyconsdev_select(NULL); 243 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next); 244 cnd->cnd_cn = NULL; 245 246 /* Remove this device from available mask. */ 247 for (i = 0; i < CNDEVTAB_SIZE; i++) 248 if (cnd == &cn_devtab[i]) { 249 cons_avail_mask &= ~(1 << i); 250 break; 251 } 252 #if 0 253 /* 254 * XXX 255 * syscons gets really confused if console resources are 256 * freed after the system has initialized. 257 */ 258 if (cn->cn_term != NULL) 259 cn->cn_ops->cn_term(cn); 260 #endif 261 return; 262 } 263 } 264 265 void 266 cnselect(struct consdev *cn) 267 { 268 struct cn_device *cnd; 269 270 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) { 271 if (cnd->cnd_cn != cn) 272 continue; 273 if (cnd == STAILQ_FIRST(&cn_devlist)) 274 return; 275 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next); 276 STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next); 277 ttyconsdev_select(cnd->cnd_cn->cn_name); 278 return; 279 } 280 } 281 282 void 283 cnavailable(struct consdev *cn, int available) 284 { 285 int i; 286 287 for (i = 0; i < CNDEVTAB_SIZE; i++) { 288 if (cn_devtab[i].cnd_cn == cn) 289 break; 290 } 291 if (available) { 292 if (i < CNDEVTAB_SIZE) 293 cons_avail_mask |= (1 << i); 294 cn->cn_flags &= ~CN_FLAG_NOAVAIL; 295 } else { 296 if (i < CNDEVTAB_SIZE) 297 cons_avail_mask &= ~(1 << i); 298 cn->cn_flags |= CN_FLAG_NOAVAIL; 299 } 300 } 301 302 int 303 cnunavailable(void) 304 { 305 306 return (cons_avail_mask == 0); 307 } 308 309 /* 310 * sysctl_kern_console() provides output parseable in conscontrol(1). 311 */ 312 static int 313 sysctl_kern_console(SYSCTL_HANDLER_ARGS) 314 { 315 struct cn_device *cnd; 316 struct consdev *cp, **list; 317 char *p; 318 int delete, error; 319 struct sbuf *sb; 320 321 sb = sbuf_new(NULL, NULL, CNDEVPATHMAX * 2, SBUF_AUTOEXTEND | 322 SBUF_INCLUDENUL); 323 if (sb == NULL) 324 return (ENOMEM); 325 sbuf_clear(sb); 326 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) 327 sbuf_printf(sb, "%s,", cnd->cnd_cn->cn_name); 328 sbuf_printf(sb, "/"); 329 SET_FOREACH(list, cons_set) { 330 cp = *list; 331 if (cp->cn_name[0] != '\0') 332 sbuf_printf(sb, "%s,", cp->cn_name); 333 } 334 sbuf_finish(sb); 335 error = sysctl_handle_string(oidp, sbuf_data(sb), sbuf_len(sb), req); 336 if (error == 0 && req->newptr != NULL) { 337 p = sbuf_data(sb); 338 error = ENXIO; 339 delete = 0; 340 if (*p == '-') { 341 delete = 1; 342 p++; 343 } 344 SET_FOREACH(list, cons_set) { 345 cp = *list; 346 if (strcmp(p, cp->cn_name) != 0) 347 continue; 348 if (delete) { 349 cnremove(cp); 350 error = 0; 351 } else { 352 error = cnadd(cp); 353 if (error == 0) 354 cnselect(cp); 355 } 356 break; 357 } 358 } 359 sbuf_delete(sb); 360 return (error); 361 } 362 363 SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW, 364 0, 0, sysctl_kern_console, "A", "Console device control"); 365 366 /* 367 * User has changed the state of the console muting. 368 * This may require us to open or close the device in question. 369 */ 370 static int 371 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS) 372 { 373 int error; 374 375 error = sysctl_handle_int(oidp, &cn_mute, 0, req); 376 if (error != 0 || req->newptr == NULL) 377 return (error); 378 return (error); 379 } 380 381 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW, 382 0, sizeof(cn_mute), sysctl_kern_consmute, "I", 383 "State of the console muting"); 384 385 void 386 cngrab() 387 { 388 struct cn_device *cnd; 389 struct consdev *cn; 390 391 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) { 392 cn = cnd->cnd_cn; 393 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) 394 cn->cn_ops->cn_grab(cn); 395 } 396 } 397 398 void 399 cnungrab() 400 { 401 struct cn_device *cnd; 402 struct consdev *cn; 403 404 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) { 405 cn = cnd->cnd_cn; 406 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) 407 cn->cn_ops->cn_ungrab(cn); 408 } 409 } 410 411 void 412 cnresume() 413 { 414 struct cn_device *cnd; 415 struct consdev *cn; 416 417 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) { 418 cn = cnd->cnd_cn; 419 if (cn->cn_ops->cn_resume != NULL) 420 cn->cn_ops->cn_resume(cn); 421 } 422 } 423 424 /* 425 * Low level console routines. 426 */ 427 int 428 cngetc(void) 429 { 430 int c; 431 432 if (cn_mute) 433 return (-1); 434 while ((c = cncheckc()) == -1) 435 cpu_spinwait(); 436 if (c == '\r') 437 c = '\n'; /* console input is always ICRNL */ 438 return (c); 439 } 440 441 int 442 cncheckc(void) 443 { 444 struct cn_device *cnd; 445 struct consdev *cn; 446 int c; 447 448 if (cn_mute) 449 return (-1); 450 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) { 451 cn = cnd->cnd_cn; 452 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) { 453 c = cn->cn_ops->cn_getc(cn); 454 if (c != -1) 455 return (c); 456 } 457 } 458 return (-1); 459 } 460 461 void 462 cngets(char *cp, size_t size, int visible) 463 { 464 char *lp, *end; 465 int c; 466 467 cngrab(); 468 469 lp = cp; 470 end = cp + size - 1; 471 for (;;) { 472 c = cngetc() & 0177; 473 switch (c) { 474 case '\n': 475 case '\r': 476 cnputc(c); 477 *lp = '\0'; 478 cnungrab(); 479 return; 480 case '\b': 481 case '\177': 482 if (lp > cp) { 483 if (visible) 484 cnputs("\b \b"); 485 lp--; 486 } 487 continue; 488 case '\0': 489 continue; 490 default: 491 if (lp < end) { 492 switch (visible) { 493 case GETS_NOECHO: 494 break; 495 case GETS_ECHOPASS: 496 cnputc('*'); 497 break; 498 default: 499 cnputc(c); 500 break; 501 } 502 *lp++ = c; 503 } 504 } 505 } 506 } 507 508 void 509 cnputc(int c) 510 { 511 struct cn_device *cnd; 512 struct consdev *cn; 513 char *cp; 514 515 #ifdef EARLY_PRINTF 516 if (early_putc != NULL) { 517 if (c == '\n') 518 early_putc('\r'); 519 early_putc(c); 520 return; 521 } 522 #endif 523 524 if (cn_mute || c == '\0') 525 return; 526 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) { 527 cn = cnd->cnd_cn; 528 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) { 529 if (c == '\n') 530 cn->cn_ops->cn_putc(cn, '\r'); 531 cn->cn_ops->cn_putc(cn, c); 532 } 533 } 534 if (console_pausing && c == '\n' && !kdb_active) { 535 for (cp = console_pausestr; *cp != '\0'; cp++) 536 cnputc(*cp); 537 cngrab(); 538 if (cngetc() == '.') 539 console_pausing = 0; 540 cnungrab(); 541 cnputc('\r'); 542 for (cp = console_pausestr; *cp != '\0'; cp++) 543 cnputc(' '); 544 cnputc('\r'); 545 } 546 } 547 548 void 549 cnputsn(const char *p, size_t n) 550 { 551 size_t i; 552 int unlock_reqd = 0; 553 554 if (use_cnputs_mtx) { 555 /* 556 * NOTE: Debug prints and/or witness printouts in 557 * console driver clients can cause the "cnputs_mtx" 558 * mutex to recurse. Simply return if that happens. 559 */ 560 if (mtx_owned(&cnputs_mtx)) 561 return; 562 mtx_lock_spin(&cnputs_mtx); 563 unlock_reqd = 1; 564 } 565 566 for (i = 0; i < n; i++) 567 cnputc(p[i]); 568 569 if (unlock_reqd) 570 mtx_unlock_spin(&cnputs_mtx); 571 } 572 573 void 574 cnputs(char *p) 575 { 576 cnputsn(p, strlen(p)); 577 } 578 579 static int consmsgbuf_size = 8192; 580 SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0, 581 "Console tty buffer size"); 582 583 /* 584 * Redirect console output to a tty. 585 */ 586 void 587 constty_set(struct tty *tp) 588 { 589 int size; 590 591 KASSERT(tp != NULL, ("constty_set: NULL tp")); 592 if (consbuf == NULL) { 593 size = consmsgbuf_size; 594 consbuf = malloc(size, M_TTYCONS, M_WAITOK); 595 msgbuf_init(&consmsgbuf, consbuf, size); 596 callout_init(&conscallout, 0); 597 } 598 constty = tp; 599 constty_timeout(NULL); 600 } 601 602 /* 603 * Disable console redirection to a tty. 604 */ 605 void 606 constty_clear(void) 607 { 608 int c; 609 610 constty = NULL; 611 if (consbuf == NULL) 612 return; 613 callout_stop(&conscallout); 614 while ((c = msgbuf_getchar(&consmsgbuf)) != -1) 615 cnputc(c); 616 free(consbuf, M_TTYCONS); 617 consbuf = NULL; 618 } 619 620 /* Times per second to check for pending console tty messages. */ 621 static int constty_wakeups_per_second = 5; 622 SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW, 623 &constty_wakeups_per_second, 0, 624 "Times per second to check for pending console tty messages"); 625 626 static void 627 constty_timeout(void *arg) 628 { 629 int c; 630 631 if (constty != NULL) { 632 tty_lock(constty); 633 while ((c = msgbuf_getchar(&consmsgbuf)) != -1) { 634 if (tty_putchar(constty, c) < 0) { 635 tty_unlock(constty); 636 constty = NULL; 637 break; 638 } 639 } 640 641 if (constty != NULL) 642 tty_unlock(constty); 643 } 644 if (constty != NULL) { 645 callout_reset(&conscallout, hz / constty_wakeups_per_second, 646 constty_timeout, NULL); 647 } else { 648 /* Deallocate the constty buffer memory. */ 649 constty_clear(); 650 } 651 } 652 653 static void 654 cn_drvinit(void *unused) 655 { 656 657 mtx_init(&cnputs_mtx, "cnputs_mtx", NULL, MTX_SPIN | MTX_NOWITNESS); 658 use_cnputs_mtx = 1; 659 } 660 661 SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL); 662 663 /* 664 * Sysbeep(), if we have hardware for it 665 */ 666 667 #ifdef HAS_TIMER_SPKR 668 669 static int beeping; 670 static struct callout beeping_timer; 671 672 static void 673 sysbeepstop(void *chan) 674 { 675 676 timer_spkr_release(); 677 beeping = 0; 678 } 679 680 int 681 sysbeep(int pitch, int period) 682 { 683 684 if (timer_spkr_acquire()) { 685 if (!beeping) { 686 /* Something else owns it. */ 687 return (EBUSY); 688 } 689 } 690 timer_spkr_setfreq(pitch); 691 if (!beeping) { 692 beeping = period; 693 callout_reset(&beeping_timer, period, sysbeepstop, NULL); 694 } 695 return (0); 696 } 697 698 static void 699 sysbeep_init(void *unused) 700 { 701 702 callout_init(&beeping_timer, 1); 703 } 704 SYSINIT(sysbeep, SI_SUB_SOFTINTR, SI_ORDER_ANY, sysbeep_init, NULL); 705 #else 706 707 /* 708 * No hardware, no sound 709 */ 710 711 int 712 sysbeep(int pitch __unused, int period __unused) 713 { 714 715 return (ENODEV); 716 } 717 718 #endif 719 720 /* 721 * Temporary support for sc(4) to vt(4) transition. 722 */ 723 static unsigned vty_prefer; 724 static char vty_name[16]; 725 SYSCTL_STRING(_kern, OID_AUTO, vty, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, vty_name, 726 0, "Console vty driver"); 727 728 int 729 vty_enabled(unsigned vty) 730 { 731 static unsigned vty_selected = 0; 732 733 if (vty_selected == 0) { 734 TUNABLE_STR_FETCH("kern.vty", vty_name, sizeof(vty_name)); 735 do { 736 #if defined(DEV_SC) 737 if (strcmp(vty_name, "sc") == 0) { 738 vty_selected = VTY_SC; 739 break; 740 } 741 #endif 742 #if defined(DEV_VT) 743 if (strcmp(vty_name, "vt") == 0) { 744 vty_selected = VTY_VT; 745 break; 746 } 747 #endif 748 if (vty_prefer != 0) { 749 vty_selected = vty_prefer; 750 break; 751 } 752 #if defined(DEV_VT) 753 vty_selected = VTY_VT; 754 #elif defined(DEV_SC) 755 vty_selected = VTY_SC; 756 #endif 757 } while (0); 758 759 if (vty_selected == VTY_VT) 760 strcpy(vty_name, "vt"); 761 else if (vty_selected == VTY_SC) 762 strcpy(vty_name, "sc"); 763 } 764 return ((vty_selected & vty) != 0); 765 } 766 767 void 768 vty_set_preferred(unsigned vty) 769 { 770 771 vty_prefer = vty; 772 #if !defined(DEV_SC) 773 vty_prefer &= ~VTY_SC; 774 #endif 775 #if !defined(DEV_VT) 776 vty_prefer &= ~VTY_VT; 777 #endif 778 } 779 780