1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2009 The FreeBSD Foundation 5 * 6 * This software was developed by Ed Schouten under sponsorship from the 7 * FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/param.h> 32 #include <sys/cons.h> 33 #include <sys/consio.h> 34 #include <sys/kernel.h> 35 #include <sys/lock.h> 36 #include <sys/malloc.h> 37 #include <sys/mutex.h> 38 #include <sys/stdarg.h> 39 #include <sys/systm.h> 40 #include <sys/terminal.h> 41 #include <sys/tty.h> 42 43 static MALLOC_DEFINE(M_TERMINAL, "terminal", "terminal device"); 44 45 /* 46 * Locking. 47 * 48 * Normally we don't need to lock down the terminal emulator, because 49 * the TTY lock is already held when calling teken_input(). 50 * Unfortunately this is not the case when the terminal acts as a 51 * console device, because cnputc() can be called at the same time. 52 * This means terminals may need to be locked down using a spin lock. 53 */ 54 #define TERMINAL_LOCK(tm) do { \ 55 if ((tm)->tm_flags & TF_CONS) \ 56 mtx_lock_spin(&(tm)->tm_mtx); \ 57 else if ((tm)->tm_tty != NULL) \ 58 tty_lock((tm)->tm_tty); \ 59 } while (0) 60 #define TERMINAL_UNLOCK(tm) do { \ 61 if ((tm)->tm_flags & TF_CONS) \ 62 mtx_unlock_spin(&(tm)->tm_mtx); \ 63 else if ((tm)->tm_tty != NULL) \ 64 tty_unlock((tm)->tm_tty); \ 65 } while (0) 66 #define TERMINAL_LOCK_TTY(tm) do { \ 67 if ((tm)->tm_flags & TF_CONS) \ 68 mtx_lock_spin(&(tm)->tm_mtx); \ 69 } while (0) 70 #define TERMINAL_UNLOCK_TTY(tm) do { \ 71 if ((tm)->tm_flags & TF_CONS) \ 72 mtx_unlock_spin(&(tm)->tm_mtx); \ 73 } while (0) 74 #define TERMINAL_LOCK_CONS(tm) mtx_lock_spin(&(tm)->tm_mtx) 75 #define TERMINAL_UNLOCK_CONS(tm) mtx_unlock_spin(&(tm)->tm_mtx) 76 77 /* 78 * TTY routines. 79 */ 80 81 static tsw_open_t termtty_open; 82 static tsw_close_t termtty_close; 83 static tsw_outwakeup_t termtty_outwakeup; 84 static tsw_ioctl_t termtty_ioctl; 85 static tsw_mmap_t termtty_mmap; 86 87 static struct ttydevsw terminal_tty_class = { 88 .tsw_open = termtty_open, 89 .tsw_close = termtty_close, 90 .tsw_outwakeup = termtty_outwakeup, 91 .tsw_ioctl = termtty_ioctl, 92 .tsw_mmap = termtty_mmap, 93 }; 94 95 /* 96 * Terminal emulator routines. 97 */ 98 99 static tf_bell_t termteken_bell; 100 static tf_cursor_t termteken_cursor; 101 static tf_putchar_t termteken_putchar; 102 static tf_fill_t termteken_fill; 103 static tf_copy_t termteken_copy; 104 static tf_pre_input_t termteken_pre_input; 105 static tf_post_input_t termteken_post_input; 106 static tf_param_t termteken_param; 107 static tf_respond_t termteken_respond; 108 109 static teken_funcs_t terminal_drawmethods = { 110 .tf_bell = termteken_bell, 111 .tf_cursor = termteken_cursor, 112 .tf_putchar = termteken_putchar, 113 .tf_fill = termteken_fill, 114 .tf_copy = termteken_copy, 115 .tf_pre_input = termteken_pre_input, 116 .tf_post_input = termteken_post_input, 117 .tf_param = termteken_param, 118 .tf_respond = termteken_respond, 119 }; 120 121 /* Kernel message formatting. */ 122 static teken_attr_t kernel_message = { 123 .ta_fgcolor = TCHAR_FGCOLOR(TERMINAL_KERN_ATTR), 124 .ta_bgcolor = TCHAR_BGCOLOR(TERMINAL_KERN_ATTR), 125 .ta_format = TCHAR_FORMAT(TERMINAL_KERN_ATTR) 126 }; 127 128 static teken_attr_t default_message = { 129 .ta_fgcolor = TCHAR_FGCOLOR(TERMINAL_NORM_ATTR), 130 .ta_bgcolor = TCHAR_BGCOLOR(TERMINAL_NORM_ATTR), 131 .ta_format = TCHAR_FORMAT(TERMINAL_NORM_ATTR) 132 }; 133 134 /* Fudge fg brightness as TF_BOLD (shifted). */ 135 #define TCOLOR_FG_FUDGED(color) __extension__ ({ \ 136 teken_color_t _c; \ 137 \ 138 _c = (color); \ 139 TCOLOR_FG(_c & 7) | ((_c & 8) << 18); \ 140 }) 141 142 /* Fudge bg brightness as TF_BLINK (shifted). */ 143 #define TCOLOR_BG_FUDGED(color) __extension__ ({ \ 144 teken_color_t _c; \ 145 \ 146 _c = (color); \ 147 TCOLOR_BG(_c & 7) | ((_c & 8) << 20); \ 148 }) 149 150 #define TCOLOR_256TO16(color) __extension__ ({ \ 151 teken_color_t _c; \ 152 \ 153 _c = (color); \ 154 if (_c >= 16) \ 155 _c = teken_256to16(_c); \ 156 _c; \ 157 }) 158 159 #define TCHAR_CREATE(c, a) ((c) | TFORMAT((a)->ta_format) | \ 160 TCOLOR_FG_FUDGED(TCOLOR_256TO16((a)->ta_fgcolor)) | \ 161 TCOLOR_BG_FUDGED(TCOLOR_256TO16((a)->ta_bgcolor))) 162 163 static void 164 terminal_init(struct terminal *tm) 165 { 166 int fg, bg; 167 168 if (tm->tm_flags & TF_CONS) 169 mtx_init(&tm->tm_mtx, "trmlck", NULL, MTX_SPIN); 170 171 teken_init(&tm->tm_emulator, &terminal_drawmethods, tm); 172 173 fg = bg = -1; 174 TUNABLE_INT_FETCH("teken.fg_color", &fg); 175 TUNABLE_INT_FETCH("teken.bg_color", &bg); 176 177 if (fg != -1) { 178 default_message.ta_fgcolor = fg; 179 kernel_message.ta_fgcolor = fg; 180 } 181 if (bg != -1) { 182 default_message.ta_bgcolor = bg; 183 kernel_message.ta_bgcolor = bg; 184 } 185 186 if (default_message.ta_bgcolor == TC_WHITE) { 187 default_message.ta_bgcolor |= TC_LIGHT; 188 kernel_message.ta_bgcolor |= TC_LIGHT; 189 } 190 191 if (default_message.ta_bgcolor == TC_BLACK && 192 default_message.ta_fgcolor < TC_NCOLORS) 193 kernel_message.ta_fgcolor |= TC_LIGHT; 194 teken_set_defattr(&tm->tm_emulator, &default_message); 195 } 196 197 struct terminal * 198 terminal_alloc(const struct terminal_class *tc, void *softc) 199 { 200 struct terminal *tm; 201 202 tm = malloc(sizeof(struct terminal), M_TERMINAL, M_WAITOK|M_ZERO); 203 terminal_init(tm); 204 205 tm->tm_class = tc; 206 tm->tm_softc = softc; 207 208 return (tm); 209 } 210 211 static void 212 terminal_sync_ttysize(struct terminal *tm) 213 { 214 struct tty *tp; 215 216 tp = tm->tm_tty; 217 if (tp == NULL) 218 return; 219 220 tty_lock(tp); 221 tty_set_winsize(tp, &tm->tm_winsize); 222 tty_unlock(tp); 223 } 224 225 void 226 terminal_maketty(struct terminal *tm, const char *fmt, ...) 227 { 228 struct tty *tp; 229 char name[8]; 230 va_list ap; 231 232 va_start(ap, fmt); 233 vsnrprintf(name, sizeof name, 32, fmt, ap); 234 va_end(ap); 235 236 tp = tty_alloc(&terminal_tty_class, tm); 237 tty_makedev(tp, NULL, "%s", name); 238 tm->tm_tty = tp; 239 terminal_sync_ttysize(tm); 240 } 241 242 void 243 terminal_set_cursor(struct terminal *tm, const term_pos_t *pos) 244 { 245 246 teken_set_cursor(&tm->tm_emulator, pos); 247 } 248 249 void 250 terminal_set_winsize_blank(struct terminal *tm, const struct winsize *size, 251 int blank, const term_attr_t *attr) 252 { 253 term_rect_t r; 254 255 tm->tm_winsize = *size; 256 257 r.tr_begin.tp_row = r.tr_begin.tp_col = 0; 258 r.tr_end.tp_row = size->ws_row; 259 r.tr_end.tp_col = size->ws_col; 260 261 TERMINAL_LOCK(tm); 262 if (blank == 0) 263 teken_set_winsize_noreset(&tm->tm_emulator, &r.tr_end); 264 else 265 teken_set_winsize(&tm->tm_emulator, &r.tr_end); 266 TERMINAL_UNLOCK(tm); 267 268 if ((blank != 0) && !(tm->tm_flags & TF_MUTE)) 269 tm->tm_class->tc_fill(tm, &r, 270 TCHAR_CREATE((teken_char_t)' ', attr)); 271 272 terminal_sync_ttysize(tm); 273 } 274 275 void 276 terminal_set_winsize(struct terminal *tm, const struct winsize *size) 277 { 278 279 terminal_set_winsize_blank(tm, size, 1, 280 (const term_attr_t *)&default_message); 281 } 282 283 /* 284 * XXX: This function is a kludge. Drivers like vt(4) need to 285 * temporarily stop input when resizing, etc. This should ideally be 286 * handled within the driver. 287 */ 288 289 void 290 terminal_mute(struct terminal *tm, int yes) 291 { 292 293 TERMINAL_LOCK(tm); 294 if (yes) 295 tm->tm_flags |= TF_MUTE; 296 else 297 tm->tm_flags &= ~TF_MUTE; 298 TERMINAL_UNLOCK(tm); 299 } 300 301 void 302 terminal_input_char(struct terminal *tm, term_char_t c) 303 { 304 struct tty *tp; 305 306 tp = tm->tm_tty; 307 if (tp == NULL) 308 return; 309 310 /* 311 * Strip off any attributes. Also ignore input of second part of 312 * CJK fullwidth characters, as we don't want to return these 313 * characters twice. 314 */ 315 if (TCHAR_FORMAT(c) & TF_CJK_RIGHT) 316 return; 317 c = TCHAR_CHARACTER(c); 318 319 tty_lock(tp); 320 /* 321 * Conversion to UTF-8. 322 */ 323 if (c < 0x80) { 324 ttydisc_rint(tp, c, 0); 325 } else if (c < 0x800) { 326 char str[2] = { 327 0xc0 | (c >> 6), 328 0x80 | (c & 0x3f) 329 }; 330 331 ttydisc_rint_simple(tp, str, sizeof str); 332 } else if (c < 0x10000) { 333 char str[3] = { 334 0xe0 | (c >> 12), 335 0x80 | ((c >> 6) & 0x3f), 336 0x80 | (c & 0x3f) 337 }; 338 339 ttydisc_rint_simple(tp, str, sizeof str); 340 } else { 341 char str[4] = { 342 0xf0 | (c >> 18), 343 0x80 | ((c >> 12) & 0x3f), 344 0x80 | ((c >> 6) & 0x3f), 345 0x80 | (c & 0x3f) 346 }; 347 348 ttydisc_rint_simple(tp, str, sizeof str); 349 } 350 ttydisc_rint_done(tp); 351 tty_unlock(tp); 352 } 353 354 void 355 terminal_input_raw(struct terminal *tm, char c) 356 { 357 struct tty *tp; 358 359 tp = tm->tm_tty; 360 if (tp == NULL) 361 return; 362 363 tty_lock(tp); 364 ttydisc_rint(tp, c, 0); 365 ttydisc_rint_done(tp); 366 tty_unlock(tp); 367 } 368 369 void 370 terminal_input_special(struct terminal *tm, unsigned int k) 371 { 372 struct tty *tp; 373 const char *str; 374 375 tp = tm->tm_tty; 376 if (tp == NULL) 377 return; 378 379 str = teken_get_sequence(&tm->tm_emulator, k); 380 if (str == NULL) 381 return; 382 383 tty_lock(tp); 384 ttydisc_rint_simple(tp, str, strlen(str)); 385 ttydisc_rint_done(tp); 386 tty_unlock(tp); 387 } 388 389 /* 390 * Binding with the TTY layer. 391 */ 392 393 static int 394 termtty_open(struct tty *tp) 395 { 396 struct terminal *tm = tty_softc(tp); 397 398 tm->tm_class->tc_opened(tm, 1); 399 return (0); 400 } 401 402 static void 403 termtty_close(struct tty *tp) 404 { 405 struct terminal *tm = tty_softc(tp); 406 407 tm->tm_class->tc_opened(tm, 0); 408 } 409 410 static void 411 termtty_outwakeup(struct tty *tp) 412 { 413 struct terminal *tm = tty_softc(tp); 414 char obuf[128]; 415 size_t olen; 416 unsigned int flags = 0; 417 418 while ((olen = ttydisc_getc(tp, obuf, sizeof obuf)) > 0) { 419 TERMINAL_LOCK_TTY(tm); 420 if (!(tm->tm_flags & TF_MUTE)) { 421 tm->tm_flags &= ~TF_BELL; 422 teken_input(&tm->tm_emulator, obuf, olen); 423 flags |= tm->tm_flags; 424 } 425 TERMINAL_UNLOCK_TTY(tm); 426 } 427 428 TERMINAL_LOCK_TTY(tm); 429 if (!(tm->tm_flags & TF_MUTE)) 430 tm->tm_class->tc_done(tm); 431 TERMINAL_UNLOCK_TTY(tm); 432 if (flags & TF_BELL) 433 tm->tm_class->tc_bell(tm); 434 } 435 436 static int 437 termtty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td) 438 { 439 struct terminal *tm = tty_softc(tp); 440 int error; 441 442 switch (cmd) { 443 case CONS_GETINFO: { 444 vid_info_t *vi = (vid_info_t *)data; 445 const teken_pos_t *p; 446 int fg, bg; 447 448 if (vi->size != sizeof(vid_info_t)) 449 return (EINVAL); 450 451 /* Already help the console driver by filling in some data. */ 452 p = teken_get_cursor(&tm->tm_emulator); 453 vi->mv_row = p->tp_row; 454 vi->mv_col = p->tp_col; 455 456 p = teken_get_winsize(&tm->tm_emulator); 457 vi->mv_rsz = p->tp_row; 458 vi->mv_csz = p->tp_col; 459 460 teken_get_defattr_cons25(&tm->tm_emulator, &fg, &bg); 461 vi->mv_norm.fore = fg; 462 vi->mv_norm.back = bg; 463 /* XXX: keep vidcontrol happy; bold backgrounds. */ 464 vi->mv_rev.fore = bg; 465 vi->mv_rev.back = fg & 0x7; 466 break; 467 } 468 } 469 470 /* 471 * Unlike various other drivers, this driver will never 472 * deallocate TTYs. This means it's safe to temporarily unlock 473 * the TTY when handling ioctls. 474 */ 475 tty_unlock(tp); 476 error = tm->tm_class->tc_ioctl(tm, cmd, data, td); 477 tty_lock(tp); 478 if ((error == 0) && (cmd == CONS_CLRHIST)) { 479 /* 480 * Scrollback history has been successfully cleared, 481 * so reset the cursor position to the top left of the screen. 482 */ 483 teken_pos_t p; 484 p.tp_row = 0; 485 p.tp_col = 0; 486 teken_set_cursor(&tm->tm_emulator, &p); 487 } 488 return (error); 489 } 490 491 static int 492 termtty_mmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t * paddr, 493 int nprot, vm_memattr_t *memattr) 494 { 495 struct terminal *tm = tty_softc(tp); 496 497 return (tm->tm_class->tc_mmap(tm, offset, paddr, nprot, memattr)); 498 } 499 500 /* 501 * Binding with the kernel and debug console. 502 */ 503 504 static cn_probe_t termcn_cnprobe; 505 static cn_init_t termcn_cninit; 506 static cn_term_t termcn_cnterm; 507 static cn_getc_t termcn_cngetc; 508 static cn_putc_t termcn_cnputc; 509 static cn_grab_t termcn_cngrab; 510 static cn_ungrab_t termcn_cnungrab; 511 512 const struct consdev_ops termcn_cnops = { 513 .cn_probe = termcn_cnprobe, 514 .cn_init = termcn_cninit, 515 .cn_term = termcn_cnterm, 516 .cn_getc = termcn_cngetc, 517 .cn_putc = termcn_cnputc, 518 .cn_grab = termcn_cngrab, 519 .cn_ungrab = termcn_cnungrab, 520 }; 521 522 void 523 termcn_cnregister(struct terminal *tm) 524 { 525 struct consdev *cp; 526 527 cp = tm->consdev; 528 if (cp == NULL) { 529 cp = malloc(sizeof(struct consdev), M_TERMINAL, 530 M_WAITOK|M_ZERO); 531 cp->cn_ops = &termcn_cnops; 532 cp->cn_arg = tm; 533 cp->cn_pri = CN_INTERNAL; 534 sprintf(cp->cn_name, "ttyv0"); 535 536 tm->tm_flags = TF_CONS; 537 tm->consdev = cp; 538 539 terminal_init(tm); 540 } 541 542 /* Attach terminal as console. */ 543 cnadd(cp); 544 } 545 546 static void 547 termcn_cngrab(struct consdev *cp) 548 { 549 struct terminal *tm = cp->cn_arg; 550 551 tm->tm_class->tc_cngrab(tm); 552 } 553 554 static void 555 termcn_cnungrab(struct consdev *cp) 556 { 557 struct terminal *tm = cp->cn_arg; 558 559 tm->tm_class->tc_cnungrab(tm); 560 } 561 562 static void 563 termcn_cnprobe(struct consdev *cp) 564 { 565 struct terminal *tm = cp->cn_arg; 566 567 if (tm == NULL) { 568 cp->cn_pri = CN_DEAD; 569 return; 570 } 571 572 tm->consdev = cp; 573 terminal_init(tm); 574 575 tm->tm_class->tc_cnprobe(tm, cp); 576 } 577 578 static void 579 termcn_cninit(struct consdev *cp) 580 { 581 582 } 583 584 static void 585 termcn_cnterm(struct consdev *cp) 586 { 587 588 } 589 590 static int 591 termcn_cngetc(struct consdev *cp) 592 { 593 struct terminal *tm = cp->cn_arg; 594 595 return (tm->tm_class->tc_cngetc(tm)); 596 } 597 598 static void 599 termcn_cnputc(struct consdev *cp, int c) 600 { 601 struct terminal *tm = cp->cn_arg; 602 teken_attr_t backup; 603 char cv = c; 604 605 TERMINAL_LOCK_CONS(tm); 606 if (!(tm->tm_flags & TF_MUTE)) { 607 backup = *teken_get_curattr(&tm->tm_emulator); 608 teken_set_curattr(&tm->tm_emulator, &kernel_message); 609 teken_input(&tm->tm_emulator, &cv, 1); 610 teken_set_curattr(&tm->tm_emulator, &backup); 611 tm->tm_class->tc_done(tm); 612 } 613 TERMINAL_UNLOCK_CONS(tm); 614 } 615 616 /* 617 * Binding with the terminal emulator. 618 */ 619 620 static void 621 termteken_bell(void *softc) 622 { 623 struct terminal *tm = softc; 624 625 tm->tm_flags |= TF_BELL; 626 } 627 628 static void 629 termteken_cursor(void *softc, const teken_pos_t *p) 630 { 631 struct terminal *tm = softc; 632 633 tm->tm_class->tc_cursor(tm, p); 634 } 635 636 static void 637 termteken_putchar(void *softc, const teken_pos_t *p, teken_char_t c, 638 const teken_attr_t *a) 639 { 640 struct terminal *tm = softc; 641 642 tm->tm_class->tc_putchar(tm, p, TCHAR_CREATE(c, a)); 643 } 644 645 static void 646 termteken_fill(void *softc, const teken_rect_t *r, teken_char_t c, 647 const teken_attr_t *a) 648 { 649 struct terminal *tm = softc; 650 651 tm->tm_class->tc_fill(tm, r, TCHAR_CREATE(c, a)); 652 } 653 654 static void 655 termteken_copy(void *softc, const teken_rect_t *r, const teken_pos_t *p) 656 { 657 struct terminal *tm = softc; 658 659 tm->tm_class->tc_copy(tm, r, p); 660 } 661 662 static void 663 termteken_pre_input(void *softc) 664 { 665 struct terminal *tm = softc; 666 667 tm->tm_class->tc_pre_input(tm); 668 } 669 670 static void 671 termteken_post_input(void *softc) 672 { 673 struct terminal *tm = softc; 674 675 tm->tm_class->tc_post_input(tm); 676 } 677 678 static void 679 termteken_param(void *softc, int cmd, unsigned int arg) 680 { 681 struct terminal *tm = softc; 682 683 tm->tm_class->tc_param(tm, cmd, arg); 684 } 685 686 static void 687 termteken_respond(void *softc, const void *buf, size_t len) 688 { 689 #if 0 690 struct terminal *tm = softc; 691 struct tty *tp; 692 693 /* 694 * Only inject a response into the TTY if the data actually 695 * originated from the TTY. 696 * 697 * XXX: This cannot be done right now. The TTY could pick up 698 * other locks. It could also in theory cause loops, when the 699 * TTY performs echoing of a command that generates even more 700 * input. 701 */ 702 tp = tm->tm_tty; 703 if (tp == NULL) 704 return; 705 706 ttydisc_rint_simple(tp, buf, len); 707 ttydisc_rint_done(tp); 708 #endif 709 } 710