1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2009, 2013 The FreeBSD Foundation 5 * 6 * This software was developed by Ed Schouten under sponsorship from the 7 * FreeBSD Foundation. 8 * 9 * Portions of this software were developed by Oleksandr Rybalko 10 * under sponsorship from the FreeBSD Foundation. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/consio.h> 36 #include <sys/devctl.h> 37 #include <sys/eventhandler.h> 38 #include <sys/fbio.h> 39 #include <sys/font.h> 40 #include <sys/kbio.h> 41 #include <sys/kdb.h> 42 #include <sys/kernel.h> 43 #include <sys/linker.h> 44 #include <sys/lock.h> 45 #include <sys/malloc.h> 46 #include <sys/mutex.h> 47 #include <sys/splash.h> 48 #include <sys/power.h> 49 #include <sys/priv.h> 50 #include <sys/proc.h> 51 #include <sys/random.h> 52 #include <sys/reboot.h> 53 #include <sys/sbuf.h> 54 #include <sys/systm.h> 55 #include <sys/terminal.h> 56 57 #include <dev/kbd/kbdreg.h> 58 #include <dev/vt/vt.h> 59 60 #if defined(__i386__) || defined(__amd64__) 61 #include <machine/psl.h> 62 #include <machine/frame.h> 63 #endif 64 65 static int vtterm_cngrab_noswitch(struct vt_device *, struct vt_window *); 66 static int vtterm_cnungrab_noswitch(struct vt_device *, struct vt_window *); 67 68 static tc_bell_t vtterm_bell; 69 static tc_cursor_t vtterm_cursor; 70 static tc_putchar_t vtterm_putchar; 71 static tc_fill_t vtterm_fill; 72 static tc_copy_t vtterm_copy; 73 static tc_pre_input_t vtterm_pre_input; 74 static tc_post_input_t vtterm_post_input; 75 static tc_param_t vtterm_param; 76 static tc_done_t vtterm_done; 77 78 static tc_cnprobe_t vtterm_cnprobe; 79 static tc_cngetc_t vtterm_cngetc; 80 81 static tc_cngrab_t vtterm_cngrab; 82 static tc_cnungrab_t vtterm_cnungrab; 83 84 static tc_opened_t vtterm_opened; 85 static tc_ioctl_t vtterm_ioctl; 86 static tc_mmap_t vtterm_mmap; 87 88 static const struct terminal_class vt_termclass = { 89 .tc_bell = vtterm_bell, 90 .tc_cursor = vtterm_cursor, 91 .tc_putchar = vtterm_putchar, 92 .tc_fill = vtterm_fill, 93 .tc_copy = vtterm_copy, 94 .tc_pre_input = vtterm_pre_input, 95 .tc_post_input = vtterm_post_input, 96 .tc_param = vtterm_param, 97 .tc_done = vtterm_done, 98 99 .tc_cnprobe = vtterm_cnprobe, 100 .tc_cngetc = vtterm_cngetc, 101 102 .tc_cngrab = vtterm_cngrab, 103 .tc_cnungrab = vtterm_cnungrab, 104 105 .tc_opened = vtterm_opened, 106 .tc_ioctl = vtterm_ioctl, 107 .tc_mmap = vtterm_mmap, 108 }; 109 110 /* 111 * Use a constant timer of 25 Hz to redraw the screen. 112 * 113 * XXX: In theory we should only fire up the timer when there is really 114 * activity. Unfortunately we cannot always start timers. We really 115 * don't want to process kernel messages synchronously, because it 116 * really slows down the system. 117 */ 118 #define VT_TIMERFREQ 25 119 120 /* Bell pitch/duration. */ 121 #define VT_BELLDURATION (SBT_1S / 20) 122 #define VT_BELLPITCH 800 123 124 #define VT_UNIT(vw) ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \ 125 (vw)->vw_number) 126 127 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 128 "vt(9) parameters"); 129 static VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)"); 130 static VT_SYSCTL_INT(enable_bell, 0, "Enable bell"); 131 static VT_SYSCTL_INT(debug, 0, "vt(9) debug level"); 132 static VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode"); 133 static VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend"); 134 135 /* Slow down and dont rely on timers and interrupts */ 136 static VT_SYSCTL_INT(slow_down, 0, "Non-zero make console slower and synchronous."); 137 138 /* Allow to disable some keyboard combinations. */ 139 static VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination. " 140 "See kbdmap(5) to configure."); 141 static VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination. " 142 "See kbdmap(5) to configure."); 143 static VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination. " 144 "See kbdmap(5) to configure (typically Ctrl-Alt-Delete)."); 145 static VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger. " 146 "See kbdmap(5) to configure (typically Ctrl-Alt-Esc)."); 147 static VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic. " 148 "See kbdmap(5) to configure."); 149 150 /* Used internally, not a tunable. */ 151 int vt_draw_logo_cpus; 152 VT_SYSCTL_INT(splash_cpu, 0, "Show logo CPUs during boot"); 153 VT_SYSCTL_INT(splash_ncpu, 0, "Override number of logos displayed " 154 "(0 = do not override)"); 155 VT_SYSCTL_INT(splash_cpu_style, 2, "Draw logo style " 156 "(0 = Alternate beastie, 1 = Beastie, 2 = Orb)"); 157 VT_SYSCTL_INT(splash_cpu_duration, 10, "Hide logos after (seconds)"); 158 159 static unsigned int vt_unit = 0; 160 static MALLOC_DEFINE(M_VT, "vt", "vt device"); 161 struct vt_device *main_vd = &vt_consdev; 162 163 /* Boot logo. */ 164 extern unsigned int vt_logo_width; 165 extern unsigned int vt_logo_height; 166 extern unsigned int vt_logo_depth; 167 extern unsigned char vt_logo_image[]; 168 #ifndef DEV_SPLASH 169 #define vtterm_draw_cpu_logos(...) do {} while (0) 170 const unsigned int vt_logo_sprite_height; 171 #endif 172 173 /* 174 * Console font. vt_font_loader will be filled with font data passed 175 * by loader. If there is no font passed by boot loader, we use built in 176 * default. 177 */ 178 extern struct vt_font vt_font_default; 179 static struct vt_font vt_font_loader; 180 static struct vt_font *vt_font_assigned = &vt_font_default; 181 182 #ifndef SC_NO_CUTPASTE 183 extern struct vt_mouse_cursor vt_default_mouse_pointer; 184 #endif 185 186 static int signal_vt_rel(struct vt_window *); 187 static int signal_vt_acq(struct vt_window *); 188 static int finish_vt_rel(struct vt_window *, int, int *); 189 static int finish_vt_acq(struct vt_window *); 190 static int vt_window_switch(struct vt_window *); 191 static int vt_late_window_switch(struct vt_window *); 192 static int vt_proc_alive(struct vt_window *); 193 static void vt_resize(struct vt_device *); 194 static void vt_update_static(void *); 195 #ifndef SC_NO_CUTPASTE 196 static void vt_mouse_paste(void); 197 #endif 198 static void vt_suspend_handler(void *priv); 199 static void vt_resume_handler(void *priv); 200 201 SET_DECLARE(vt_drv_set, struct vt_driver); 202 203 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_MAX_HEIGHT)) 204 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_MAX_WIDTH)) 205 206 static struct terminal vt_consterm; 207 static struct vt_window vt_conswindow; 208 static term_char_t vt_consdrawn[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)]; 209 static term_color_t vt_consdrawnfg[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)]; 210 static term_color_t vt_consdrawnbg[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)]; 211 static bool vt_cons_pos_to_flush[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)]; 212 struct vt_device vt_consdev = { 213 .vd_driver = NULL, 214 .vd_softc = NULL, 215 .vd_prev_driver = NULL, 216 .vd_prev_softc = NULL, 217 .vd_flags = VDF_INVALID, 218 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, }, 219 .vd_curwindow = &vt_conswindow, 220 .vd_kbstate = 0, 221 222 #ifndef SC_NO_CUTPASTE 223 .vd_pastebuf = { 224 .vpb_buf = NULL, 225 .vpb_bufsz = 0, 226 .vpb_len = 0 227 }, 228 .vd_mcursor = &vt_default_mouse_pointer, 229 .vd_mcursor_fg = TC_WHITE, 230 .vd_mcursor_bg = TC_BLACK, 231 #endif 232 233 .vd_drawn = vt_consdrawn, 234 .vd_drawnfg = vt_consdrawnfg, 235 .vd_drawnbg = vt_consdrawnbg, 236 237 .vd_pos_to_flush = vt_cons_pos_to_flush, 238 }; 239 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)]; 240 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE]; 241 static struct vt_window vt_conswindow = { 242 .vw_number = VT_CONSWINDOW, 243 .vw_flags = VWF_CONSOLE, 244 .vw_buf = { 245 .vb_buffer = &vt_constextbuf[0], 246 .vb_rows = &vt_constextbufrows[0], 247 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE, 248 .vb_curroffset = 0, 249 .vb_roffset = 0, 250 .vb_flags = VBF_STATIC, 251 .vb_mark_start = {.tp_row = 0, .tp_col = 0,}, 252 .vb_mark_end = {.tp_row = 0, .tp_col = 0,}, 253 .vb_scr_size = { 254 .tp_row = _VTDEFH, 255 .tp_col = _VTDEFW, 256 }, 257 }, 258 .vw_device = &vt_consdev, 259 .vw_terminal = &vt_consterm, 260 .vw_kbdmode = K_XLATE, 261 .vw_grabbed = 0, 262 .vw_bell_pitch = VT_BELLPITCH, 263 .vw_bell_duration = VT_BELLDURATION, 264 }; 265 266 /* Add to set of consoles. */ 267 TERMINAL_DECLARE_EARLY(vt_consterm, vt_termclass, &vt_conswindow); 268 269 /* 270 * Right after kmem is done to allow early drivers to use locking and allocate 271 * memory. 272 */ 273 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static, 274 &vt_consdev); 275 /* Delay until all devices attached, to not waste time. */ 276 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade, 277 &vt_consdev); 278 279 static bool inside_vt_flush = false; 280 static bool inside_vt_window_switch = false; 281 282 /* Initialize locks/mem depended members. */ 283 static void 284 vt_update_static(void *dummy) 285 { 286 287 if (!vty_enabled(VTY_VT)) 288 return; 289 if (main_vd->vd_driver != NULL) 290 printf("VT(%s): %s %ux%u\n", main_vd->vd_driver->vd_name, 291 (main_vd->vd_flags & VDF_TEXTMODE) ? "text" : "resolution", 292 main_vd->vd_width, main_vd->vd_height); 293 else 294 printf("VT: init without driver.\n"); 295 296 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF); 297 mtx_init(&main_vd->vd_flush_lock, "vtdev flush", NULL, MTX_DEF); 298 cv_init(&main_vd->vd_winswitch, "vtwswt"); 299 } 300 301 static void 302 vt_schedule_flush(struct vt_device *vd, int ms) 303 { 304 305 if (ms <= 0) 306 /* Default to initial value. */ 307 ms = 1000 / VT_TIMERFREQ; 308 309 callout_schedule(&vd->vd_timer, hz / (1000 / ms)); 310 } 311 312 void 313 vt_resume_flush_timer(struct vt_window *vw, int ms) 314 { 315 struct vt_device *vd = vw->vw_device; 316 317 if (vd->vd_curwindow != vw) 318 return; 319 320 if (!(vd->vd_flags & VDF_ASYNC) || 321 !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1)) 322 return; 323 324 vt_schedule_flush(vd, ms); 325 } 326 327 static void 328 vt_suspend_flush_timer(struct vt_device *vd) 329 { 330 /* 331 * As long as this function is called locked, callout_stop() 332 * has the same effect like callout_drain() with regard to 333 * preventing the callback function from executing. 334 */ 335 VT_LOCK_ASSERT(vd, MA_OWNED); 336 337 if (!(vd->vd_flags & VDF_ASYNC) || 338 !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0)) 339 return; 340 341 callout_stop(&vd->vd_timer); 342 } 343 344 static void 345 vt_switch_timer(void *arg, int pending) 346 { 347 348 (void)vt_late_window_switch((struct vt_window *)arg); 349 } 350 351 static int 352 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd) 353 { 354 int mode, ret; 355 356 mode = 0; 357 ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode); 358 if (ret == ENOIOCTL) 359 ret = ENODEV; 360 if (ret != 0) 361 return (ret); 362 363 vw->vw_kbdmode = mode; 364 365 return (0); 366 } 367 368 static int 369 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd) 370 { 371 int ret; 372 373 ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode); 374 if (ret == ENOIOCTL) 375 ret = ENODEV; 376 377 return (ret); 378 } 379 380 static int 381 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd) 382 { 383 int state, ret; 384 385 state = 0; 386 ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state); 387 if (ret == ENOIOCTL) 388 ret = ENODEV; 389 if (ret != 0) 390 return (ret); 391 392 vw->vw_kbdstate &= ~LOCK_MASK; 393 vw->vw_kbdstate |= state & LOCK_MASK; 394 395 return (0); 396 } 397 398 static int 399 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd) 400 { 401 int state, ret; 402 403 state = vw->vw_kbdstate & LOCK_MASK; 404 ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state); 405 if (ret == ENOIOCTL) 406 ret = ENODEV; 407 408 return (ret); 409 } 410 411 static int 412 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd) 413 { 414 int leds, ret; 415 416 leds = 0; 417 ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds); 418 if (ret == ENOIOCTL) 419 ret = ENODEV; 420 if (ret != 0) 421 return (ret); 422 423 vw->vw_kbdstate &= ~LED_MASK; 424 vw->vw_kbdstate |= leds & LED_MASK; 425 426 return (0); 427 } 428 429 static int 430 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd) 431 { 432 int leds, ret; 433 434 leds = vw->vw_kbdstate & LED_MASK; 435 ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds); 436 if (ret == ENOIOCTL) 437 ret = ENODEV; 438 439 return (ret); 440 } 441 442 static int 443 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw) 444 { 445 446 DPRINTF(40, "%s\n", __func__); 447 curvw->vw_switch_to = vw; 448 /* Set timer to allow switch in case when process hang. */ 449 taskqueue_enqueue_timeout(taskqueue_thread, &vw->vw_timeout_task_dead, hz * vt_deadtimer); 450 /* Notify process about vt switch attempt. */ 451 DPRINTF(30, "%s: Notify process.\n", __func__); 452 signal_vt_rel(curvw); 453 454 return (0); 455 } 456 457 static int 458 vt_window_postswitch(struct vt_window *vw) 459 { 460 461 signal_vt_acq(vw); 462 return (0); 463 } 464 465 /* vt_late_window_switch will do VT switching for regular case. */ 466 static int 467 vt_late_window_switch(struct vt_window *vw) 468 { 469 struct vt_window *curvw; 470 int ret; 471 472 taskqueue_cancel_timeout(taskqueue_thread, &vw->vw_timeout_task_dead, NULL); 473 474 ret = vt_window_switch(vw); 475 if (ret != 0) { 476 /* 477 * If the switch hasn't happened, then return the VT 478 * to the current owner, if any. 479 */ 480 curvw = vw->vw_device->vd_curwindow; 481 if (curvw->vw_smode.mode == VT_PROCESS) 482 (void)vt_window_postswitch(curvw); 483 return (ret); 484 } 485 486 /* Notify owner process about terminal availability. */ 487 if (vw->vw_smode.mode == VT_PROCESS) { 488 ret = vt_window_postswitch(vw); 489 } 490 return (ret); 491 } 492 493 /* Switch window. */ 494 static int 495 vt_proc_window_switch(struct vt_window *vw) 496 { 497 struct vt_window *curvw; 498 struct vt_device *vd; 499 int ret; 500 501 /* Prevent switching to NULL */ 502 if (vw == NULL) { 503 DPRINTF(30, "%s: Cannot switch: vw is NULL.", __func__); 504 return (EINVAL); 505 } 506 vd = vw->vw_device; 507 curvw = vd->vd_curwindow; 508 509 /* Check if virtual terminal is locked */ 510 if (curvw->vw_flags & VWF_VTYLOCK) 511 return (EBUSY); 512 513 /* Check if switch already in progress */ 514 if (curvw->vw_flags & VWF_SWWAIT_REL) { 515 /* Check if switching to same window */ 516 if (curvw->vw_switch_to == vw) { 517 DPRINTF(30, "%s: Switch in progress to same vw.", __func__); 518 return (0); /* success */ 519 } 520 DPRINTF(30, "%s: Switch in progress to different vw.", __func__); 521 return (EBUSY); 522 } 523 524 /* Avoid switching to already selected window */ 525 if (vw == curvw) { 526 DPRINTF(30, "%s: Cannot switch: vw == curvw.", __func__); 527 return (0); /* success */ 528 } 529 530 /* 531 * Early check for an attempt to switch to a non-functional VT. 532 * The same check is done in vt_window_switch(), but it's better 533 * to fail as early as possible to avoid needless pre-switch 534 * actions. 535 */ 536 VT_LOCK(vd); 537 if ((vw->vw_flags & (VWF_OPENED|VWF_CONSOLE)) == 0) { 538 VT_UNLOCK(vd); 539 return (EINVAL); 540 } 541 VT_UNLOCK(vd); 542 543 /* Ask current process permission to switch away. */ 544 if (curvw->vw_smode.mode == VT_PROCESS) { 545 DPRINTF(30, "%s: VT_PROCESS ", __func__); 546 if (vt_proc_alive(curvw) == FALSE) { 547 DPRINTF(30, "Dead. Cleaning."); 548 /* Dead */ 549 } else { 550 DPRINTF(30, "%s: Signaling process.\n", __func__); 551 /* Alive, try to ask him. */ 552 ret = vt_window_preswitch(vw, curvw); 553 /* Wait for process answer or timeout. */ 554 return (ret); 555 } 556 DPRINTF(30, "\n"); 557 } 558 559 ret = vt_late_window_switch(vw); 560 return (ret); 561 } 562 563 /* Switch window ignoring process locking. */ 564 static int 565 vt_window_switch(struct vt_window *vw) 566 { 567 struct vt_device *vd = vw->vw_device; 568 struct vt_window *curvw = vd->vd_curwindow; 569 keyboard_t *kbd; 570 571 if (inside_vt_window_switch && KERNEL_PANICKED()) 572 return (0); 573 574 inside_vt_window_switch = true; 575 576 if (kdb_active) { 577 /* 578 * When grabbing the console for the debugger, avoid 579 * locks as that can result in deadlock. While this 580 * could use try locks, that wouldn't really make a 581 * difference as there are sufficient barriers in 582 * debugger entry/exit to be equivalent to 583 * successfully try-locking here. 584 */ 585 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) { 586 inside_vt_window_switch = false; 587 return (EINVAL); 588 } 589 590 vd->vd_curwindow = vw; 591 vd->vd_flags |= VDF_INVALID; 592 if (vd->vd_driver->vd_postswitch) 593 vd->vd_driver->vd_postswitch(vd); 594 inside_vt_window_switch = false; 595 return (0); 596 } 597 598 VT_LOCK(vd); 599 if (curvw == vw) { 600 /* 601 * Nothing to do, except ensure the driver has the opportunity to 602 * switch to console mode when panicking, making sure the panic 603 * is readable (even when a GUI was using ttyv0). 604 */ 605 if ((kdb_active || KERNEL_PANICKED()) && 606 vd->vd_driver->vd_postswitch) 607 vd->vd_driver->vd_postswitch(vd); 608 inside_vt_window_switch = false; 609 VT_UNLOCK(vd); 610 return (0); 611 } 612 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) { 613 inside_vt_window_switch = false; 614 VT_UNLOCK(vd); 615 return (EINVAL); 616 } 617 618 vt_suspend_flush_timer(vd); 619 620 vd->vd_curwindow = vw; 621 vd->vd_flags |= VDF_INVALID; 622 cv_broadcast(&vd->vd_winswitch); 623 VT_UNLOCK(vd); 624 625 if (vd->vd_driver->vd_postswitch) 626 vd->vd_driver->vd_postswitch(vd); 627 628 vt_resume_flush_timer(vw, 0); 629 630 /* Restore per-window keyboard mode. */ 631 mtx_lock(&Giant); 632 if ((kbd = vd->vd_keyboard) != NULL) { 633 if (curvw->vw_kbdmode == K_XLATE) 634 vt_save_kbd_state(curvw, kbd); 635 636 vt_update_kbd_mode(vw, kbd); 637 vt_update_kbd_state(vw, kbd); 638 } 639 mtx_unlock(&Giant); 640 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number); 641 642 inside_vt_window_switch = false; 643 return (0); 644 } 645 646 void 647 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size) 648 { 649 650 size->tp_row = vd->vd_height; 651 if (vt_draw_logo_cpus) 652 size->tp_row -= vt_logo_sprite_height; 653 size->tp_col = vd->vd_width; 654 if (vf != NULL) { 655 size->tp_row = MIN(size->tp_row / vf->vf_height, 656 PIXEL_HEIGHT(VT_FB_MAX_HEIGHT)); 657 size->tp_col = MIN(size->tp_col / vf->vf_width, 658 PIXEL_WIDTH(VT_FB_MAX_WIDTH)); 659 } 660 } 661 662 static inline void 663 vt_termrect(struct vt_device *vd, struct vt_font *vf, term_rect_t *rect) 664 { 665 666 rect->tr_begin.tp_row = rect->tr_begin.tp_col = 0; 667 if (vt_draw_logo_cpus) 668 rect->tr_begin.tp_row = vt_logo_sprite_height; 669 670 rect->tr_end.tp_row = vd->vd_height; 671 rect->tr_end.tp_col = vd->vd_width; 672 673 if (vf != NULL) { 674 rect->tr_begin.tp_row = 675 howmany(rect->tr_begin.tp_row, vf->vf_height); 676 677 rect->tr_end.tp_row = MIN(rect->tr_end.tp_row / vf->vf_height, 678 PIXEL_HEIGHT(VT_FB_MAX_HEIGHT)); 679 rect->tr_end.tp_col = MIN(rect->tr_end.tp_col / vf->vf_width, 680 PIXEL_WIDTH(VT_FB_MAX_WIDTH)); 681 } 682 } 683 684 void 685 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size) 686 { 687 688 size->ws_ypixel = vd->vd_height; 689 if (vt_draw_logo_cpus) 690 size->ws_ypixel -= vt_logo_sprite_height; 691 size->ws_row = size->ws_ypixel; 692 size->ws_col = size->ws_xpixel = vd->vd_width; 693 if (vf != NULL) { 694 size->ws_row = MIN(size->ws_row / vf->vf_height, 695 PIXEL_HEIGHT(VT_FB_MAX_HEIGHT)); 696 size->ws_col = MIN(size->ws_col / vf->vf_width, 697 PIXEL_WIDTH(VT_FB_MAX_WIDTH)); 698 } 699 } 700 701 void 702 vt_compute_drawable_area(struct vt_window *vw) 703 { 704 struct vt_device *vd; 705 struct vt_font *vf; 706 vt_axis_t height; 707 708 vd = vw->vw_device; 709 710 if (vw->vw_font == NULL) { 711 vw->vw_draw_area.tr_begin.tp_col = 0; 712 vw->vw_draw_area.tr_begin.tp_row = 0; 713 if (vt_draw_logo_cpus) 714 vw->vw_draw_area.tr_begin.tp_row = vt_logo_sprite_height; 715 vw->vw_draw_area.tr_end.tp_col = vd->vd_width; 716 vw->vw_draw_area.tr_end.tp_row = vd->vd_height; 717 return; 718 } 719 720 vf = vw->vw_font; 721 722 /* 723 * Compute the drawable area, so that the text is centered on 724 * the screen. 725 */ 726 727 height = vd->vd_height; 728 if (vt_draw_logo_cpus) 729 height -= vt_logo_sprite_height; 730 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2; 731 vw->vw_draw_area.tr_begin.tp_row = (height % vf->vf_height) / 2; 732 if (vt_draw_logo_cpus) 733 vw->vw_draw_area.tr_begin.tp_row += vt_logo_sprite_height; 734 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col + 735 rounddown(vd->vd_width, vf->vf_width); 736 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row + 737 rounddown(height, vf->vf_height); 738 } 739 740 static void 741 vt_scroll(struct vt_window *vw, int offset, int whence) 742 { 743 int diff; 744 term_pos_t size; 745 746 if ((vw->vw_flags & VWF_SCROLL) == 0) 747 return; 748 749 vt_termsize(vw->vw_device, vw->vw_font, &size); 750 751 diff = vthistory_seek(&vw->vw_buf, offset, whence); 752 if (diff) 753 vw->vw_device->vd_flags |= VDF_INVALID; 754 vt_resume_flush_timer(vw, 0); 755 } 756 757 static int 758 vt_machine_kbdevent(struct vt_device *vd, int c) 759 { 760 761 switch (c) { 762 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */ 763 if (vt_kbd_debug) { 764 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger"); 765 #if VT_ALT_TO_ESC_HACK 766 /* 767 * There's an unfortunate conflict between SPCLKEY|DBG 768 * and VT_ALT_TO_ESC_HACK. Just assume they didn't mean 769 * it if we got to here. 770 */ 771 vd->vd_kbstate &= ~ALKED; 772 #endif 773 } 774 return (1); 775 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */ 776 if (vt_kbd_halt) 777 shutdown_nice(RB_HALT); 778 return (1); 779 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */ 780 #ifndef SC_NO_CUTPASTE 781 /* Insert text from cut-paste buffer. */ 782 vt_mouse_paste(); 783 #endif 784 break; 785 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */ 786 if (vt_kbd_poweroff) 787 shutdown_nice(RB_HALT|RB_POWEROFF); 788 return (1); 789 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */ 790 /* 791 * Request to immediate panic if sysctl 792 * kern.vt.enable_panic_key allow it. 793 */ 794 if (vt_kbd_panic) 795 panic("Forced by the panic key"); 796 return (1); 797 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */ 798 if (vt_kbd_reboot) 799 shutdown_nice(RB_AUTOBOOT); 800 return (1); 801 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */ 802 /* Force activatation/deactivation of the screen saver. */ 803 /* TODO */ 804 return (1); 805 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */ 806 /* Put machine into Stand-By mode. */ 807 power_pm_suspend(POWER_SLEEP_STATE_STANDBY); 808 return (1); 809 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */ 810 /* Suspend machine. */ 811 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND); 812 return (1); 813 } 814 815 return (0); 816 } 817 818 static void 819 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console) 820 { 821 struct vt_device *vd; 822 term_pos_t size; 823 824 vd = vw->vw_device; 825 /* Only special keys handled in ScrollLock mode */ 826 if ((c & SPCLKEY) == 0) 827 return; 828 829 c &= ~SPCLKEY; 830 831 if (console == 0) { 832 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) { 833 vw = vd->vd_windows[c - F_SCR]; 834 vt_proc_window_switch(vw); 835 return; 836 } 837 VT_LOCK(vd); 838 } 839 840 switch (c) { 841 case SLK: { 842 /* Turn scrolling off. */ 843 vt_scroll(vw, 0, VHS_END); 844 VTBUF_SLCK_DISABLE(&vw->vw_buf); 845 vw->vw_flags &= ~VWF_SCROLL; 846 break; 847 } 848 case FKEY | F(49): /* Home key. */ 849 vt_scroll(vw, 0, VHS_SET); 850 break; 851 case FKEY | F(50): /* Arrow up. */ 852 vt_scroll(vw, -1, VHS_CUR); 853 break; 854 case FKEY | F(51): /* Page up. */ 855 vt_termsize(vd, vw->vw_font, &size); 856 vt_scroll(vw, -size.tp_row, VHS_CUR); 857 break; 858 case FKEY | F(57): /* End key. */ 859 vt_scroll(vw, 0, VHS_END); 860 break; 861 case FKEY | F(58): /* Arrow down. */ 862 vt_scroll(vw, 1, VHS_CUR); 863 break; 864 case FKEY | F(59): /* Page down. */ 865 vt_termsize(vd, vw->vw_font, &size); 866 vt_scroll(vw, size.tp_row, VHS_CUR); 867 break; 868 } 869 870 if (console == 0) 871 VT_UNLOCK(vd); 872 } 873 874 static int 875 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c) 876 { 877 struct vt_window *vw = vd->vd_curwindow; 878 879 random_harvest_queue(&c, sizeof(c), RANDOM_KEYBOARD); 880 #if VT_ALT_TO_ESC_HACK 881 if (c & RELKEY) { 882 switch (c & ~RELKEY) { 883 case (SPCLKEY | RALT): 884 if (vt_enable_altgr != 0) 885 break; 886 case (SPCLKEY | LALT): 887 vd->vd_kbstate &= ~ALKED; 888 } 889 /* Other keys ignored for RELKEY event. */ 890 return (0); 891 } else { 892 switch (c & ~RELKEY) { 893 case (SPCLKEY | RALT): 894 if (vt_enable_altgr != 0) 895 break; 896 case (SPCLKEY | LALT): 897 vd->vd_kbstate |= ALKED; 898 } 899 } 900 #else 901 if (c & RELKEY) 902 /* Other keys ignored for RELKEY event. */ 903 return (0); 904 #endif 905 906 if (vt_machine_kbdevent(vd, c)) 907 return (0); 908 909 if (vw->vw_flags & VWF_SCROLL) { 910 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */); 911 /* Scroll mode keys handled, nothing to do more. */ 912 return (0); 913 } 914 915 if (c & SPCLKEY) { 916 c &= ~SPCLKEY; 917 918 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) { 919 vw = vd->vd_windows[c - F_SCR]; 920 vt_proc_window_switch(vw); 921 return (0); 922 } 923 924 switch (c) { 925 case NEXT: 926 /* Switch to next VT. */ 927 c = (vw->vw_number + 1) % VT_MAXWINDOWS; 928 vw = vd->vd_windows[c]; 929 vt_proc_window_switch(vw); 930 return (0); 931 case PREV: 932 /* Switch to previous VT. */ 933 c = (vw->vw_number + VT_MAXWINDOWS - 1) % VT_MAXWINDOWS; 934 vw = vd->vd_windows[c]; 935 vt_proc_window_switch(vw); 936 return (0); 937 case SLK: { 938 vt_save_kbd_state(vw, kbd); 939 VT_LOCK(vd); 940 if (vw->vw_kbdstate & SLKED) { 941 /* Turn scrolling on. */ 942 vw->vw_flags |= VWF_SCROLL; 943 VTBUF_SLCK_ENABLE(&vw->vw_buf); 944 } else { 945 /* Turn scrolling off. */ 946 vw->vw_flags &= ~VWF_SCROLL; 947 VTBUF_SLCK_DISABLE(&vw->vw_buf); 948 vt_scroll(vw, 0, VHS_END); 949 } 950 VT_UNLOCK(vd); 951 break; 952 } 953 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3): 954 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6): 955 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9): 956 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12): 957 /* F1 through F12 keys. */ 958 terminal_input_special(vw->vw_terminal, 959 TKEY_F1 + c - (FKEY | F(1))); 960 break; 961 case FKEY | F(49): /* Home key. */ 962 terminal_input_special(vw->vw_terminal, TKEY_HOME); 963 break; 964 case FKEY | F(50): /* Arrow up. */ 965 terminal_input_special(vw->vw_terminal, TKEY_UP); 966 break; 967 case FKEY | F(51): /* Page up. */ 968 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP); 969 break; 970 case FKEY | F(53): /* Arrow left. */ 971 terminal_input_special(vw->vw_terminal, TKEY_LEFT); 972 break; 973 case FKEY | F(55): /* Arrow right. */ 974 terminal_input_special(vw->vw_terminal, TKEY_RIGHT); 975 break; 976 case FKEY | F(57): /* End key. */ 977 terminal_input_special(vw->vw_terminal, TKEY_END); 978 break; 979 case FKEY | F(58): /* Arrow down. */ 980 terminal_input_special(vw->vw_terminal, TKEY_DOWN); 981 break; 982 case FKEY | F(59): /* Page down. */ 983 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN); 984 break; 985 case FKEY | F(60): /* Insert key. */ 986 terminal_input_special(vw->vw_terminal, TKEY_INSERT); 987 break; 988 case FKEY | F(61): /* Delete key. */ 989 terminal_input_special(vw->vw_terminal, TKEY_DELETE); 990 break; 991 } 992 } else if (KEYFLAGS(c) == 0) { 993 /* Don't do UTF-8 conversion when doing raw mode. */ 994 if (vw->vw_kbdmode == K_XLATE) { 995 #if VT_ALT_TO_ESC_HACK 996 if (vd->vd_kbstate & ALKED) { 997 /* 998 * Prepend ESC sequence if one of ALT keys down. 999 */ 1000 terminal_input_char(vw->vw_terminal, 0x1b); 1001 } 1002 #endif 1003 #if defined(KDB) 1004 kdb_alt_break(c, &vd->vd_altbrk); 1005 #endif 1006 terminal_input_char(vw->vw_terminal, KEYCHAR(c)); 1007 } else 1008 terminal_input_raw(vw->vw_terminal, c); 1009 } 1010 return (0); 1011 } 1012 1013 static int 1014 vt_kbdevent(keyboard_t *kbd, int event, void *arg) 1015 { 1016 struct vt_device *vd = arg; 1017 int c; 1018 1019 switch (event) { 1020 case KBDIO_KEYINPUT: 1021 break; 1022 case KBDIO_UNLOADING: 1023 mtx_lock(&Giant); 1024 vd->vd_keyboard = NULL; 1025 kbd_release(kbd, (void *)vd); 1026 mtx_unlock(&Giant); 1027 return (0); 1028 default: 1029 return (EINVAL); 1030 } 1031 1032 while ((c = kbdd_read_char(kbd, 0)) != NOKEY) 1033 vt_processkey(kbd, vd, c); 1034 1035 return (0); 1036 } 1037 1038 static int 1039 vt_allocate_keyboard(struct vt_device *vd) 1040 { 1041 int grabbed, i, idx0, idx; 1042 keyboard_t *k0, *k; 1043 keyboard_info_t ki; 1044 1045 /* 1046 * If vt_upgrade() happens while the console is grabbed, we are 1047 * potentially going to switch keyboard devices while the keyboard is in 1048 * use. Unwind the grabbing of the current keyboard first, then we will 1049 * re-grab the new keyboard below, before we return. 1050 */ 1051 if (vd->vd_curwindow == &vt_conswindow) { 1052 grabbed = vd->vd_curwindow->vw_grabbed; 1053 for (i = 0; i < grabbed; ++i) 1054 vtterm_cnungrab_noswitch(vd, vd->vd_curwindow); 1055 } 1056 1057 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd); 1058 if (idx0 >= 0) { 1059 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0); 1060 k0 = kbd_get_keyboard(idx0); 1061 1062 for (idx = kbd_find_keyboard2("*", -1, 0); 1063 idx != -1; 1064 idx = kbd_find_keyboard2("*", -1, idx + 1)) { 1065 k = kbd_get_keyboard(idx); 1066 1067 if (idx == idx0 || KBD_IS_BUSY(k)) 1068 continue; 1069 1070 bzero(&ki, sizeof(ki)); 1071 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name)); 1072 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0'; 1073 ki.kb_unit = k->kb_unit; 1074 1075 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki); 1076 } 1077 } else { 1078 DPRINTF(20, "%s: no kbdmux allocated\n", __func__); 1079 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd); 1080 if (idx0 < 0) { 1081 DPRINTF(10, "%s: No keyboard found.\n", __func__); 1082 return (-1); 1083 } 1084 k0 = kbd_get_keyboard(idx0); 1085 } 1086 vd->vd_keyboard = k0; 1087 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, 1088 vd->vd_keyboard->kb_index); 1089 1090 if (vd->vd_curwindow == &vt_conswindow) { 1091 for (i = 0; i < grabbed; ++i) 1092 vtterm_cngrab_noswitch(vd, vd->vd_curwindow); 1093 } 1094 1095 return (idx0); 1096 } 1097 1098 #define DEVCTL_LEN 64 1099 static void 1100 vtterm_devctl(bool enabled, bool hushed, int hz, sbintime_t duration) 1101 { 1102 struct sbuf sb; 1103 char *buf; 1104 1105 buf = malloc(DEVCTL_LEN, M_VT, M_NOWAIT); 1106 if (buf == NULL) 1107 return; 1108 sbuf_new(&sb, buf, DEVCTL_LEN, SBUF_FIXEDLEN); 1109 sbuf_printf(&sb, "enabled=%s hushed=%s hz=%d duration_ms=%d", 1110 enabled ? "true" : "false", hushed ? "true" : "false", 1111 hz, (int)(duration / SBT_1MS)); 1112 sbuf_finish(&sb); 1113 if (sbuf_error(&sb) == 0) 1114 devctl_notify("VT", "BELL", "RING", sbuf_data(&sb)); 1115 sbuf_delete(&sb); 1116 free(buf, M_VT); 1117 } 1118 1119 static void 1120 vtterm_bell(struct terminal *tm) 1121 { 1122 struct vt_window *vw = tm->tm_softc; 1123 struct vt_device *vd = vw->vw_device; 1124 1125 vtterm_devctl(vt_enable_bell, vd->vd_flags & VDF_QUIET_BELL, 1126 vw->vw_bell_pitch, vw->vw_bell_duration); 1127 1128 if (!vt_enable_bell) 1129 return; 1130 1131 if (vd->vd_flags & VDF_QUIET_BELL) 1132 return; 1133 1134 if (vw->vw_bell_pitch == 0 || 1135 vw->vw_bell_duration == 0) 1136 return; 1137 1138 sysbeep(vw->vw_bell_pitch, vw->vw_bell_duration); 1139 } 1140 1141 /* 1142 * Beep with user-provided frequency and duration as specified by a KDMKTONE 1143 * ioctl (compatible with Linux). The frequency is specified as a 8254 PIT 1144 * divisor for a 1.19MHz clock. 1145 * 1146 * See https://tldp.org/LDP/lpg/node83.html. 1147 */ 1148 static void 1149 vtterm_beep(struct terminal *tm, u_int param) 1150 { 1151 u_int freq; 1152 sbintime_t period; 1153 struct vt_window *vw = tm->tm_softc; 1154 struct vt_device *vd = vw->vw_device; 1155 1156 if ((param == 0) || ((param & 0xffff) == 0)) { 1157 vtterm_bell(tm); 1158 return; 1159 } 1160 1161 /* XXX period unit is supposed to be "timer ticks." */ 1162 period = ((param >> 16) & 0xffff) * SBT_1MS; 1163 freq = 1193182 / (param & 0xffff); 1164 1165 vtterm_devctl(vt_enable_bell, vd->vd_flags & VDF_QUIET_BELL, 1166 freq, period); 1167 1168 if (!vt_enable_bell) 1169 return; 1170 1171 sysbeep(freq, period); 1172 } 1173 1174 static void 1175 vtterm_cursor(struct terminal *tm, const term_pos_t *p) 1176 { 1177 struct vt_window *vw = tm->tm_softc; 1178 1179 vtbuf_cursor_position(&vw->vw_buf, p); 1180 } 1181 1182 static void 1183 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c) 1184 { 1185 struct vt_window *vw = tm->tm_softc; 1186 1187 vtbuf_putchar(&vw->vw_buf, p, c); 1188 } 1189 1190 static void 1191 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c) 1192 { 1193 struct vt_window *vw = tm->tm_softc; 1194 1195 vtbuf_fill(&vw->vw_buf, r, c); 1196 } 1197 1198 static void 1199 vtterm_copy(struct terminal *tm, const term_rect_t *r, 1200 const term_pos_t *p) 1201 { 1202 struct vt_window *vw = tm->tm_softc; 1203 1204 vtbuf_copy(&vw->vw_buf, r, p); 1205 } 1206 1207 static void 1208 vtterm_param(struct terminal *tm, int cmd, unsigned int arg) 1209 { 1210 struct vt_window *vw = tm->tm_softc; 1211 1212 switch (cmd) { 1213 case TP_SETLOCALCURSOR: 1214 /* 1215 * 0 means normal (usually block), 1 means hidden, and 1216 * 2 means blinking (always block) for compatibility with 1217 * syscons. We don't support any changes except hiding, 1218 * so must map 2 to 0. 1219 */ 1220 arg = (arg == 1) ? 0 : 1; 1221 /* FALLTHROUGH */ 1222 case TP_SHOWCURSOR: 1223 vtbuf_cursor_visibility(&vw->vw_buf, arg); 1224 vt_resume_flush_timer(vw, 0); 1225 break; 1226 case TP_MOUSE: 1227 vw->vw_mouse_level = arg; 1228 break; 1229 case TP_SETBELLPD: 1230 vw->vw_bell_pitch = TP_SETBELLPD_PITCH(arg); 1231 vw->vw_bell_duration = 1232 TICKS_2_MSEC(TP_SETBELLPD_DURATION(arg)) * SBT_1MS; 1233 break; 1234 } 1235 } 1236 1237 void 1238 vt_determine_colors(term_char_t c, int cursor, 1239 term_color_t *fg, term_color_t *bg) 1240 { 1241 term_color_t tmp; 1242 int invert; 1243 1244 invert = 0; 1245 1246 *fg = TCHAR_FGCOLOR(c); 1247 if (TCHAR_FORMAT(c) & TF_BOLD) 1248 *fg = TCOLOR_LIGHT(*fg); 1249 *bg = TCHAR_BGCOLOR(c); 1250 if (TCHAR_FORMAT(c) & TF_BLINK) 1251 *bg = TCOLOR_LIGHT(*bg); 1252 1253 if (TCHAR_FORMAT(c) & TF_REVERSE) 1254 invert ^= 1; 1255 if (cursor) 1256 invert ^= 1; 1257 1258 if (invert) { 1259 tmp = *fg; 1260 *fg = *bg; 1261 *bg = tmp; 1262 } 1263 } 1264 1265 #ifndef SC_NO_CUTPASTE 1266 int 1267 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area) 1268 { 1269 unsigned int mx, my; 1270 1271 /* 1272 * We use the cursor position saved during the current refresh, 1273 * in case the cursor moved since. 1274 */ 1275 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col; 1276 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row; 1277 1278 if (mx >= area->tr_end.tp_col || 1279 mx + vd->vd_mcursor->width <= area->tr_begin.tp_col || 1280 my >= area->tr_end.tp_row || 1281 my + vd->vd_mcursor->height <= area->tr_begin.tp_row) 1282 return (0); 1283 return (1); 1284 } 1285 1286 static void 1287 vt_mark_mouse_position_as_dirty(struct vt_device *vd, int locked) 1288 { 1289 term_rect_t area; 1290 struct vt_window *vw; 1291 struct vt_font *vf; 1292 int x, y; 1293 1294 vw = vd->vd_curwindow; 1295 vf = vw->vw_font; 1296 1297 x = vd->vd_mx_drawn; 1298 y = vd->vd_my_drawn; 1299 1300 if (vf != NULL) { 1301 area.tr_begin.tp_col = x / vf->vf_width; 1302 area.tr_begin.tp_row = y / vf->vf_height; 1303 area.tr_end.tp_col = 1304 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1; 1305 area.tr_end.tp_row = 1306 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1; 1307 } else { 1308 /* 1309 * No font loaded (ie. vt_vga operating in textmode). 1310 * 1311 * FIXME: This fake area needs to be revisited once the 1312 * mouse cursor is supported in vt_vga's textmode. 1313 */ 1314 area.tr_begin.tp_col = x; 1315 area.tr_begin.tp_row = y; 1316 area.tr_end.tp_col = x + 2; 1317 area.tr_end.tp_row = y + 2; 1318 } 1319 1320 if (!locked) 1321 vtbuf_lock(&vw->vw_buf); 1322 if (vd->vd_driver->vd_invalidate_text) 1323 vd->vd_driver->vd_invalidate_text(vd, &area); 1324 vtbuf_dirty(&vw->vw_buf, &area); 1325 if (!locked) 1326 vtbuf_unlock(&vw->vw_buf); 1327 } 1328 #endif 1329 1330 static void 1331 vt_set_border(struct vt_device *vd, const term_rect_t *area, 1332 term_color_t c) 1333 { 1334 vd_drawrect_t *drawrect = vd->vd_driver->vd_drawrect; 1335 1336 if (drawrect == NULL) 1337 return; 1338 1339 /* Top bar */ 1340 if (area->tr_begin.tp_row > 0) 1341 drawrect(vd, 0, 0, vd->vd_width - 1, 1342 area->tr_begin.tp_row - 1, 1, c); 1343 1344 /* Left bar */ 1345 if (area->tr_begin.tp_col > 0) 1346 drawrect(vd, 0, area->tr_begin.tp_row, 1347 area->tr_begin.tp_col - 1, area->tr_end.tp_row - 1, 1, c); 1348 1349 /* Right bar */ 1350 if (area->tr_end.tp_col < vd->vd_width) 1351 drawrect(vd, area->tr_end.tp_col, area->tr_begin.tp_row, 1352 vd->vd_width - 1, area->tr_end.tp_row - 1, 1, c); 1353 1354 /* Bottom bar */ 1355 if (area->tr_end.tp_row < vd->vd_height) 1356 drawrect(vd, 0, area->tr_end.tp_row, vd->vd_width - 1, 1357 vd->vd_height - 1, 1, c); 1358 } 1359 1360 static void 1361 vt_flush_to_buffer(struct vt_device *vd, 1362 const struct vt_window *vw, const term_rect_t *area) 1363 { 1364 unsigned int col, row; 1365 term_char_t c; 1366 term_color_t fg, bg; 1367 size_t z; 1368 1369 for (row = area->tr_begin.tp_row; row < area->tr_end.tp_row; ++row) { 1370 for (col = area->tr_begin.tp_col; col < area->tr_end.tp_col; 1371 ++col) { 1372 z = row * PIXEL_WIDTH(VT_FB_MAX_WIDTH) + col; 1373 if (z >= PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * 1374 PIXEL_WIDTH(VT_FB_MAX_WIDTH)) 1375 continue; 1376 1377 c = VTBUF_GET_FIELD(&vw->vw_buf, row, col); 1378 vt_determine_colors(c, 1379 VTBUF_ISCURSOR(&vw->vw_buf, row, col), &fg, &bg); 1380 1381 if (vd->vd_drawn && (vd->vd_drawn[z] == c) && 1382 vd->vd_drawnfg && (vd->vd_drawnfg[z] == fg) && 1383 vd->vd_drawnbg && (vd->vd_drawnbg[z] == bg)) { 1384 vd->vd_pos_to_flush[z] = false; 1385 continue; 1386 } 1387 1388 vd->vd_pos_to_flush[z] = true; 1389 1390 if (vd->vd_drawn) 1391 vd->vd_drawn[z] = c; 1392 if (vd->vd_drawnfg) 1393 vd->vd_drawnfg[z] = fg; 1394 if (vd->vd_drawnbg) 1395 vd->vd_drawnbg[z] = bg; 1396 } 1397 } 1398 } 1399 1400 static void 1401 vt_bitblt_buffer(struct vt_device *vd, const struct vt_window *vw, 1402 const term_rect_t *area) 1403 { 1404 unsigned int col, row, x, y; 1405 struct vt_font *vf; 1406 term_char_t c; 1407 term_color_t fg, bg; 1408 const uint8_t *pattern; 1409 size_t z; 1410 1411 vf = vw->vw_font; 1412 1413 for (row = area->tr_begin.tp_row; row < area->tr_end.tp_row; ++row) { 1414 for (col = area->tr_begin.tp_col; col < area->tr_end.tp_col; 1415 ++col) { 1416 x = col * vf->vf_width + 1417 vw->vw_draw_area.tr_begin.tp_col; 1418 y = row * vf->vf_height + 1419 vw->vw_draw_area.tr_begin.tp_row; 1420 1421 z = row * PIXEL_WIDTH(VT_FB_MAX_WIDTH) + col; 1422 if (z >= PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * 1423 PIXEL_WIDTH(VT_FB_MAX_WIDTH)) 1424 continue; 1425 if (!vd->vd_pos_to_flush[z]) 1426 continue; 1427 1428 c = vd->vd_drawn[z]; 1429 fg = vd->vd_drawnfg[z]; 1430 bg = vd->vd_drawnbg[z]; 1431 1432 pattern = vtfont_lookup(vf, c); 1433 vd->vd_driver->vd_bitblt_bmp(vd, vw, 1434 pattern, NULL, vf->vf_width, vf->vf_height, 1435 x, y, fg, bg); 1436 } 1437 } 1438 1439 #ifndef SC_NO_CUTPASTE 1440 if (!vd->vd_mshown) 1441 return; 1442 1443 term_rect_t drawn_area; 1444 1445 drawn_area.tr_begin.tp_col = area->tr_begin.tp_col * vf->vf_width; 1446 drawn_area.tr_begin.tp_row = area->tr_begin.tp_row * vf->vf_height; 1447 drawn_area.tr_end.tp_col = area->tr_end.tp_col * vf->vf_width; 1448 drawn_area.tr_end.tp_row = area->tr_end.tp_row * vf->vf_height; 1449 1450 if (vt_is_cursor_in_area(vd, &drawn_area)) { 1451 vd->vd_driver->vd_bitblt_bmp(vd, vw, 1452 vd->vd_mcursor->map, vd->vd_mcursor->mask, 1453 vd->vd_mcursor->width, vd->vd_mcursor->height, 1454 vd->vd_mx_drawn + vw->vw_draw_area.tr_begin.tp_col, 1455 vd->vd_my_drawn + vw->vw_draw_area.tr_begin.tp_row, 1456 vd->vd_mcursor_fg, vd->vd_mcursor_bg); 1457 } 1458 #endif 1459 } 1460 1461 static void 1462 vt_draw_decorations(struct vt_device *vd) 1463 { 1464 struct vt_window *vw; 1465 const teken_attr_t *a; 1466 1467 vw = vd->vd_curwindow; 1468 1469 a = teken_get_curattr(&vw->vw_terminal->tm_emulator); 1470 vt_set_border(vd, &vw->vw_draw_area, a->ta_bgcolor); 1471 1472 if (vt_draw_logo_cpus) 1473 vtterm_draw_cpu_logos(vd); 1474 } 1475 1476 static int 1477 vt_flush(struct vt_device *vd) 1478 { 1479 struct vt_window *vw; 1480 struct vt_font *vf; 1481 term_rect_t tarea; 1482 #ifndef SC_NO_CUTPASTE 1483 int cursor_was_shown, cursor_moved; 1484 #endif 1485 bool needs_refresh; 1486 1487 if (inside_vt_flush && KERNEL_PANICKED()) 1488 return (0); 1489 1490 vw = vd->vd_curwindow; 1491 if (vw == NULL) 1492 return (0); 1493 1494 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY) 1495 return (0); 1496 1497 vf = vw->vw_font; 1498 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL)) 1499 return (0); 1500 1501 VT_FLUSH_LOCK(vd); 1502 1503 vtbuf_lock(&vw->vw_buf); 1504 inside_vt_flush = true; 1505 1506 #ifndef SC_NO_CUTPASTE 1507 cursor_was_shown = vd->vd_mshown; 1508 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn || 1509 vd->vd_my != vd->vd_my_drawn); 1510 1511 /* Check if the cursor should be displayed or not. */ 1512 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */ 1513 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */ 1514 !kdb_active && !KERNEL_PANICKED()) { /* DDB inactive. */ 1515 vd->vd_mshown = 1; 1516 } else { 1517 vd->vd_mshown = 0; 1518 } 1519 1520 /* 1521 * If the cursor changed display state or moved, we must mark 1522 * the old position as dirty, so that it's erased. 1523 */ 1524 if (cursor_was_shown != vd->vd_mshown || 1525 (vd->vd_mshown && cursor_moved)) 1526 vt_mark_mouse_position_as_dirty(vd, true); 1527 1528 /* 1529 * Save position of the mouse cursor. It's used by backends to 1530 * know where to draw the cursor and during the next refresh to 1531 * erase the previous position. 1532 */ 1533 vd->vd_mx_drawn = vd->vd_mx; 1534 vd->vd_my_drawn = vd->vd_my; 1535 1536 /* 1537 * If the cursor is displayed and has moved since last refresh, 1538 * mark the new position as dirty. 1539 */ 1540 if (vd->vd_mshown && cursor_moved) 1541 vt_mark_mouse_position_as_dirty(vd, true); 1542 #endif 1543 1544 vtbuf_undirty(&vw->vw_buf, &tarea); 1545 1546 /* Force a full redraw when the screen contents might be invalid. */ 1547 needs_refresh = false; 1548 if (vd->vd_flags & (VDF_INVALID | VDF_SUSPENDED)) { 1549 needs_refresh = true; 1550 vd->vd_flags &= ~VDF_INVALID; 1551 1552 vt_termrect(vd, vf, &tarea); 1553 if (vd->vd_driver->vd_invalidate_text) 1554 vd->vd_driver->vd_invalidate_text(vd, &tarea); 1555 } 1556 1557 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) { 1558 if (vd->vd_driver->vd_bitblt_after_vtbuf_unlock) { 1559 /* 1560 * When `vd_bitblt_after_vtbuf_unlock` is set to true, 1561 * we first remember the characters to redraw. They are 1562 * already copied to the `vd_drawn` arrays. 1563 * 1564 * We then unlock vt_buf and proceed with the actual 1565 * drawing using the backend driver. 1566 */ 1567 vt_flush_to_buffer(vd, vw, &tarea); 1568 vtbuf_unlock(&vw->vw_buf); 1569 vt_bitblt_buffer(vd, vw, &tarea); 1570 1571 if (needs_refresh) 1572 vt_draw_decorations(vd); 1573 1574 /* 1575 * We can reset `inside_vt_flush` after unlocking vtbuf 1576 * here because we also hold vt_flush_lock in this code 1577 * path. 1578 */ 1579 inside_vt_flush = false; 1580 } else { 1581 /* 1582 * When `vd_bitblt_after_vtbuf_unlock` is false, we use 1583 * the backend's `vd_bitblt_text` callback directly. 1584 */ 1585 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea); 1586 1587 if (needs_refresh) 1588 vt_draw_decorations(vd); 1589 1590 inside_vt_flush = false; 1591 vtbuf_unlock(&vw->vw_buf); 1592 } 1593 1594 VT_FLUSH_UNLOCK(vd); 1595 1596 return (1); 1597 } 1598 1599 inside_vt_flush = false; 1600 vtbuf_unlock(&vw->vw_buf); 1601 1602 VT_FLUSH_UNLOCK(vd); 1603 1604 return (0); 1605 } 1606 1607 static void 1608 vt_timer(void *arg) 1609 { 1610 struct vt_device *vd; 1611 int changed; 1612 1613 vd = arg; 1614 /* Update screen if required. */ 1615 changed = vt_flush(vd); 1616 1617 /* Schedule for next update. */ 1618 if (changed) 1619 vt_schedule_flush(vd, 0); 1620 else 1621 vd->vd_timer_armed = 0; 1622 } 1623 1624 static void 1625 vtterm_pre_input(struct terminal *tm) 1626 { 1627 struct vt_window *vw = tm->tm_softc; 1628 1629 if (inside_vt_flush && KERNEL_PANICKED()) 1630 return; 1631 1632 vtbuf_lock(&vw->vw_buf); 1633 } 1634 1635 static void 1636 vtterm_post_input(struct terminal *tm) 1637 { 1638 struct vt_window *vw = tm->tm_softc; 1639 1640 if (inside_vt_flush && KERNEL_PANICKED()) 1641 return; 1642 1643 vtbuf_unlock(&vw->vw_buf); 1644 vt_resume_flush_timer(vw, 0); 1645 } 1646 1647 static void 1648 vtterm_done(struct terminal *tm) 1649 { 1650 struct vt_window *vw = tm->tm_softc; 1651 struct vt_device *vd = vw->vw_device; 1652 1653 if (kdb_active || KERNEL_PANICKED()) { 1654 /* Switch to the debugger. */ 1655 if (vd->vd_curwindow != vw) { 1656 vd->vd_curwindow = vw; 1657 vd->vd_flags |= VDF_INVALID; 1658 if (vd->vd_driver->vd_postswitch) 1659 vd->vd_driver->vd_postswitch(vd); 1660 } 1661 vd->vd_flags &= ~VDF_SPLASH; 1662 vt_flush(vd); 1663 } else if (vt_slow_down > 0) { 1664 int i, j; 1665 for (i = 0; i < vt_slow_down; i++) { 1666 for (j = 0; j < 1000; j++) 1667 vt_flush(vd); 1668 } 1669 } else if (!(vd->vd_flags & VDF_ASYNC)) { 1670 vt_flush(vd); 1671 } 1672 } 1673 1674 #ifdef DEV_SPLASH 1675 static void 1676 vtterm_splash(struct vt_device *vd) 1677 { 1678 struct splash_info *si; 1679 uintptr_t image; 1680 vt_axis_t top, left; 1681 1682 si = MD_FETCH(preload_kmdp, MODINFOMD_SPLASH, struct splash_info *); 1683 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) { 1684 if (si == NULL) { 1685 top = (vd->vd_height - vt_logo_height) / 2; 1686 left = (vd->vd_width - vt_logo_width) / 2; 1687 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow, 1688 vt_logo_image, NULL, vt_logo_width, vt_logo_height, 1689 left, top, TC_WHITE, TC_BLACK); 1690 } else { 1691 if (si->si_depth != 4) 1692 return; 1693 image = (uintptr_t)si + sizeof(struct splash_info); 1694 image = roundup2(image, 8); 1695 top = (vd->vd_height - si->si_height) / 2; 1696 left = (vd->vd_width - si->si_width) / 2; 1697 vd->vd_driver->vd_bitblt_argb(vd, vd->vd_curwindow, 1698 (unsigned char *)image, si->si_width, si->si_height, 1699 left, top); 1700 } 1701 vd->vd_flags |= VDF_SPLASH; 1702 } 1703 } 1704 #endif 1705 1706 static struct vt_font * 1707 parse_font_info_static(struct font_info *fi) 1708 { 1709 struct vt_font *vfp; 1710 uintptr_t ptr; 1711 uint32_t checksum; 1712 1713 if (fi == NULL) 1714 return (NULL); 1715 1716 ptr = (uintptr_t)fi; 1717 /* 1718 * Compute and verify checksum. The total sum of all the fields 1719 * must be 0. 1720 */ 1721 checksum = fi->fi_width; 1722 checksum += fi->fi_height; 1723 checksum += fi->fi_bitmap_size; 1724 for (unsigned i = 0; i < VFNT_MAPS; i++) 1725 checksum += fi->fi_map_count[i]; 1726 1727 if (checksum + fi->fi_checksum != 0) 1728 return (NULL); 1729 1730 ptr += sizeof(struct font_info); 1731 ptr = roundup2(ptr, 8); 1732 1733 vfp = &vt_font_loader; 1734 vfp->vf_height = fi->fi_height; 1735 vfp->vf_width = fi->fi_width; 1736 /* This is default font, set refcount 1 to disable removal. */ 1737 vfp->vf_refcount = 1; 1738 for (unsigned i = 0; i < VFNT_MAPS; i++) { 1739 if (fi->fi_map_count[i] == 0) 1740 continue; 1741 vfp->vf_map_count[i] = fi->fi_map_count[i]; 1742 vfp->vf_map[i] = (vfnt_map_t *)ptr; 1743 ptr += (fi->fi_map_count[i] * sizeof(vfnt_map_t)); 1744 ptr = roundup2(ptr, 8); 1745 } 1746 vfp->vf_bytes = (uint8_t *)ptr; 1747 return (vfp); 1748 } 1749 1750 /* 1751 * Set up default font with allocated data structures. 1752 * However, we can not set refcount here, because it is already set and 1753 * incremented in vtterm_cnprobe() to avoid being released by font load from 1754 * userland. 1755 */ 1756 static struct vt_font * 1757 parse_font_info(struct font_info *fi) 1758 { 1759 struct vt_font *vfp; 1760 uintptr_t ptr; 1761 uint32_t checksum; 1762 size_t size; 1763 1764 if (fi == NULL) 1765 return (NULL); 1766 1767 ptr = (uintptr_t)fi; 1768 /* 1769 * Compute and verify checksum. The total sum of all the fields 1770 * must be 0. 1771 */ 1772 checksum = fi->fi_width; 1773 checksum += fi->fi_height; 1774 checksum += fi->fi_bitmap_size; 1775 for (unsigned i = 0; i < VFNT_MAPS; i++) 1776 checksum += fi->fi_map_count[i]; 1777 1778 if (checksum + fi->fi_checksum != 0) 1779 return (NULL); 1780 1781 ptr += sizeof(struct font_info); 1782 ptr = roundup2(ptr, 8); 1783 1784 vfp = &vt_font_loader; 1785 vfp->vf_height = fi->fi_height; 1786 vfp->vf_width = fi->fi_width; 1787 for (unsigned i = 0; i < VFNT_MAPS; i++) { 1788 if (fi->fi_map_count[i] == 0) 1789 continue; 1790 vfp->vf_map_count[i] = fi->fi_map_count[i]; 1791 size = fi->fi_map_count[i] * sizeof(vfnt_map_t); 1792 vfp->vf_map[i] = malloc(size, M_VT, M_WAITOK | M_ZERO); 1793 bcopy((vfnt_map_t *)ptr, vfp->vf_map[i], size); 1794 ptr += size; 1795 ptr = roundup2(ptr, 8); 1796 } 1797 vfp->vf_bytes = malloc(fi->fi_bitmap_size, M_VT, M_WAITOK | M_ZERO); 1798 bcopy((uint8_t *)ptr, vfp->vf_bytes, fi->fi_bitmap_size); 1799 return (vfp); 1800 } 1801 1802 static void 1803 vt_init_font(void *arg) 1804 { 1805 struct font_info *fi; 1806 struct vt_font *font; 1807 1808 fi = MD_FETCH(preload_kmdp, MODINFOMD_FONT, struct font_info *); 1809 1810 font = parse_font_info(fi); 1811 if (font != NULL) 1812 vt_font_assigned = font; 1813 } 1814 1815 SYSINIT(vt_init_font, SI_SUB_KMEM, SI_ORDER_ANY, vt_init_font, &vt_consdev); 1816 1817 static void 1818 vt_init_font_static(void) 1819 { 1820 struct font_info *fi; 1821 struct vt_font *font; 1822 1823 fi = MD_FETCH(preload_kmdp, MODINFOMD_FONT, struct font_info *); 1824 1825 font = parse_font_info_static(fi); 1826 if (font != NULL) 1827 vt_font_assigned = font; 1828 } 1829 1830 static void 1831 vtterm_cnprobe(struct terminal *tm, struct consdev *cp) 1832 { 1833 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL; 1834 struct vt_window *vw = tm->tm_softc; 1835 struct vt_device *vd = vw->vw_device; 1836 struct winsize wsz; 1837 const term_attr_t *a; 1838 1839 if (!vty_enabled(VTY_VT)) 1840 return; 1841 1842 if (vd->vd_flags & VDF_INITIALIZED) 1843 /* Initialization already done. */ 1844 return; 1845 1846 SET_FOREACH(vtdlist, vt_drv_set) { 1847 vtd = *vtdlist; 1848 if (vtd->vd_probe == NULL) 1849 continue; 1850 if (vtd->vd_probe(vd) == CN_DEAD) 1851 continue; 1852 if ((vtdbest == NULL) || 1853 (vtd->vd_priority > vtdbest->vd_priority)) 1854 vtdbest = vtd; 1855 } 1856 if (vtdbest == NULL) { 1857 cp->cn_pri = CN_DEAD; 1858 vd->vd_flags |= VDF_DEAD; 1859 } else { 1860 vd->vd_driver = vtdbest; 1861 cp->cn_pri = vd->vd_driver->vd_init(vd); 1862 } 1863 1864 /* Check if driver's vt_init return CN_DEAD. */ 1865 if (cp->cn_pri == CN_DEAD) { 1866 vd->vd_flags |= VDF_DEAD; 1867 } 1868 1869 /* Initialize any early-boot keyboard drivers */ 1870 kbd_configure(KB_CONF_PROBE_ONLY); 1871 1872 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1); 1873 vd->vd_windows[VT_CONSWINDOW] = vw; 1874 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw)); 1875 1876 vt_init_font_static(); 1877 1878 /* Attach default font if not in TEXTMODE. */ 1879 if ((vd->vd_flags & VDF_TEXTMODE) == 0) { 1880 vw->vw_font = vtfont_ref(vt_font_assigned); 1881 vt_compute_drawable_area(vw); 1882 } 1883 1884 /* 1885 * The original screen size was faked (_VTDEFW x _VTDEFH). Now 1886 * that we have the real viewable size, fix it in the static 1887 * buffer. 1888 */ 1889 if (vd->vd_width != 0 && vd->vd_height != 0) 1890 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size); 1891 1892 /* We need to access terminal attributes from vtbuf */ 1893 vw->vw_buf.vb_terminal = tm; 1894 vtbuf_init_early(&vw->vw_buf); 1895 vt_winsize(vd, vw->vw_font, &wsz); 1896 a = teken_get_curattr(&tm->tm_emulator); 1897 terminal_set_winsize_blank(tm, &wsz, 1, a); 1898 1899 if (vtdbest != NULL) { 1900 #ifdef DEV_SPLASH 1901 if (!vt_splash_cpu) 1902 vtterm_splash(vd); 1903 #endif 1904 vd->vd_flags |= VDF_INITIALIZED; 1905 } 1906 } 1907 1908 static int 1909 vtterm_cngetc(struct terminal *tm) 1910 { 1911 struct vt_window *vw = tm->tm_softc; 1912 struct vt_device *vd = vw->vw_device; 1913 keyboard_t *kbd; 1914 u_int c; 1915 1916 if (vw->vw_kbdsq && *vw->vw_kbdsq) 1917 return (*vw->vw_kbdsq++); 1918 1919 /* Make sure the splash screen is not there. */ 1920 if (vd->vd_flags & VDF_SPLASH) { 1921 /* Remove splash */ 1922 vd->vd_flags &= ~VDF_SPLASH; 1923 /* Mark screen as invalid to force update */ 1924 vd->vd_flags |= VDF_INVALID; 1925 vt_flush(vd); 1926 } 1927 1928 /* Stripped down keyboard handler. */ 1929 if ((kbd = vd->vd_keyboard) == NULL) 1930 return (-1); 1931 1932 /* Force keyboard input mode to K_XLATE */ 1933 vw->vw_kbdmode = K_XLATE; 1934 vt_update_kbd_mode(vw, kbd); 1935 1936 /* Switch the keyboard to polling to make it work here. */ 1937 kbdd_poll(kbd, TRUE); 1938 c = kbdd_read_char(kbd, 0); 1939 kbdd_poll(kbd, FALSE); 1940 if (c & RELKEY) 1941 return (-1); 1942 1943 if (vw->vw_flags & VWF_SCROLL) { 1944 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */); 1945 vt_flush(vd); 1946 return (-1); 1947 } 1948 1949 /* Stripped down handling of vt_kbdevent(), without locking, etc. */ 1950 if (c & SPCLKEY) { 1951 switch (c) { 1952 case SPCLKEY | SLK: 1953 vt_save_kbd_state(vw, kbd); 1954 if (vw->vw_kbdstate & SLKED) { 1955 /* Turn scrolling on. */ 1956 vw->vw_flags |= VWF_SCROLL; 1957 VTBUF_SLCK_ENABLE(&vw->vw_buf); 1958 } else { 1959 /* Turn scrolling off. */ 1960 vt_scroll(vw, 0, VHS_END); 1961 vw->vw_flags &= ~VWF_SCROLL; 1962 VTBUF_SLCK_DISABLE(&vw->vw_buf); 1963 } 1964 break; 1965 /* XXX: KDB can handle history. */ 1966 case SPCLKEY | FKEY | F(50): /* Arrow up. */ 1967 vw->vw_kbdsq = "\x1b[A"; 1968 break; 1969 case SPCLKEY | FKEY | F(58): /* Arrow down. */ 1970 vw->vw_kbdsq = "\x1b[B"; 1971 break; 1972 case SPCLKEY | FKEY | F(55): /* Arrow right. */ 1973 vw->vw_kbdsq = "\x1b[C"; 1974 break; 1975 case SPCLKEY | FKEY | F(53): /* Arrow left. */ 1976 vw->vw_kbdsq = "\x1b[D"; 1977 break; 1978 } 1979 1980 /* Force refresh to make scrollback work. */ 1981 vt_flush(vd); 1982 } else if (KEYFLAGS(c) == 0) { 1983 return (KEYCHAR(c)); 1984 } 1985 1986 if (vw->vw_kbdsq && *vw->vw_kbdsq) 1987 return (*vw->vw_kbdsq++); 1988 1989 return (-1); 1990 } 1991 1992 /* 1993 * These two do most of what we want to do in vtterm_cnungrab, but without 1994 * actually switching windows. This is necessary for, e.g., 1995 * vt_allocate_keyboard() to get the current keyboard into the state it needs to 1996 * be in without damaging the device's window state. 1997 * 1998 * Both return the current grab count, though it's only used in vtterm_cnungrab. 1999 */ 2000 static int 2001 vtterm_cngrab_noswitch(struct vt_device *vd, struct vt_window *vw) 2002 { 2003 keyboard_t *kbd; 2004 2005 if (vw->vw_grabbed++ > 0) 2006 return (vw->vw_grabbed); 2007 2008 if ((kbd = vd->vd_keyboard) == NULL) 2009 return (1); 2010 2011 /* 2012 * Make sure the keyboard is accessible even when the kbd device 2013 * driver is disabled. 2014 */ 2015 kbdd_enable(kbd); 2016 2017 /* We shall always use the keyboard in the XLATE mode here. */ 2018 vw->vw_prev_kbdmode = vw->vw_kbdmode; 2019 vw->vw_kbdmode = K_XLATE; 2020 vt_update_kbd_mode(vw, kbd); 2021 2022 kbdd_poll(kbd, TRUE); 2023 return (1); 2024 } 2025 2026 static int 2027 vtterm_cnungrab_noswitch(struct vt_device *vd, struct vt_window *vw) 2028 { 2029 keyboard_t *kbd; 2030 2031 if (--vw->vw_grabbed > 0) 2032 return (vw->vw_grabbed); 2033 2034 if ((kbd = vd->vd_keyboard) == NULL) 2035 return (0); 2036 2037 kbdd_poll(kbd, FALSE); 2038 2039 vw->vw_kbdmode = vw->vw_prev_kbdmode; 2040 vt_update_kbd_mode(vw, kbd); 2041 kbdd_disable(kbd); 2042 return (0); 2043 } 2044 2045 static void 2046 vtterm_cngrab(struct terminal *tm) 2047 { 2048 struct vt_device *vd; 2049 struct vt_window *vw; 2050 2051 vw = tm->tm_softc; 2052 vd = vw->vw_device; 2053 2054 /* To be restored after we ungrab. */ 2055 if (vd->vd_grabwindow == NULL) 2056 vd->vd_grabwindow = vd->vd_curwindow; 2057 2058 if (!cold) 2059 vt_window_switch(vw); 2060 2061 vtterm_cngrab_noswitch(vd, vw); 2062 } 2063 2064 static void 2065 vtterm_cnungrab(struct terminal *tm) 2066 { 2067 struct vt_device *vd; 2068 struct vt_window *vw, *grabwindow; 2069 2070 vw = tm->tm_softc; 2071 vd = vw->vw_device; 2072 2073 MPASS(vd->vd_grabwindow != NULL); 2074 if (vtterm_cnungrab_noswitch(vd, vw) != 0) 2075 return; 2076 2077 /* 2078 * We set `vd_grabwindow` to NULL before calling vt_window_switch() 2079 * because it allows the underlying vt(4) backend to distinguish a 2080 * "grab" from an "ungrab" of the console. 2081 * 2082 * This is used by `vt_drmfb` in drm-kmod to call either 2083 * fb_debug_enter() or fb_debug_leave() appropriately. 2084 */ 2085 grabwindow = vd->vd_grabwindow; 2086 vd->vd_grabwindow = NULL; 2087 2088 if (!cold) 2089 vt_window_switch(grabwindow); 2090 } 2091 2092 static void 2093 vtterm_opened(struct terminal *tm, int opened) 2094 { 2095 struct vt_window *vw = tm->tm_softc; 2096 struct vt_device *vd = vw->vw_device; 2097 2098 VT_LOCK(vd); 2099 vd->vd_flags &= ~VDF_SPLASH; 2100 if (opened) 2101 vw->vw_flags |= VWF_OPENED; 2102 else { 2103 vw->vw_flags &= ~VWF_OPENED; 2104 /* TODO: finish ACQ/REL */ 2105 } 2106 VT_UNLOCK(vd); 2107 } 2108 2109 static int 2110 vt_change_font(struct vt_window *vw, struct vt_font *vf) 2111 { 2112 struct vt_device *vd = vw->vw_device; 2113 struct terminal *tm = vw->vw_terminal; 2114 term_pos_t size; 2115 struct winsize wsz; 2116 2117 /* 2118 * Changing fonts. 2119 * 2120 * Changing fonts is a little tricky. We must prevent 2121 * simultaneous access to the device, so we must stop 2122 * the display timer and the terminal from accessing. 2123 * We need to switch fonts and grow our screen buffer. 2124 * 2125 * XXX: Right now the code uses terminal_mute() to 2126 * prevent data from reaching the console driver while 2127 * resizing the screen buffer. This isn't elegant... 2128 */ 2129 2130 VT_LOCK(vd); 2131 if (vw->vw_flags & VWF_BUSY) { 2132 /* Another process is changing the font. */ 2133 VT_UNLOCK(vd); 2134 return (EBUSY); 2135 } 2136 vw->vw_flags |= VWF_BUSY; 2137 VT_UNLOCK(vd); 2138 2139 vt_termsize(vd, vf, &size); 2140 vt_winsize(vd, vf, &wsz); 2141 2142 /* Grow the screen buffer and terminal. */ 2143 terminal_mute(tm, 1); 2144 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size); 2145 terminal_set_winsize_blank(tm, &wsz, 0, NULL); 2146 terminal_set_cursor(tm, &vw->vw_buf.vb_cursor); 2147 terminal_mute(tm, 0); 2148 2149 /* Actually apply the font to the current window. */ 2150 VT_LOCK(vd); 2151 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) { 2152 /* 2153 * In case vt_change_font called to update size we don't need 2154 * to update font link. 2155 */ 2156 vtfont_unref(vw->vw_font); 2157 vw->vw_font = vtfont_ref(vf); 2158 } 2159 2160 /* 2161 * Compute the drawable area and move the mouse cursor inside 2162 * it, in case the new area is smaller than the previous one. 2163 */ 2164 vt_compute_drawable_area(vw); 2165 vd->vd_mx = min(vd->vd_mx, 2166 vw->vw_draw_area.tr_end.tp_col - 2167 vw->vw_draw_area.tr_begin.tp_col - 1); 2168 vd->vd_my = min(vd->vd_my, 2169 vw->vw_draw_area.tr_end.tp_row - 2170 vw->vw_draw_area.tr_begin.tp_row - 1); 2171 2172 /* Force a full redraw the next timer tick. */ 2173 if (vd->vd_curwindow == vw) { 2174 vd->vd_flags |= VDF_INVALID; 2175 vt_resume_flush_timer(vw, 0); 2176 } 2177 vw->vw_flags &= ~VWF_BUSY; 2178 VT_UNLOCK(vd); 2179 return (0); 2180 } 2181 2182 static int 2183 vt_proc_alive(struct vt_window *vw) 2184 { 2185 struct proc *p; 2186 2187 if (vw->vw_smode.mode != VT_PROCESS) 2188 return (FALSE); 2189 2190 if (vw->vw_proc) { 2191 if ((p = pfind(vw->vw_pid)) != NULL) 2192 PROC_UNLOCK(p); 2193 if (vw->vw_proc == p) 2194 return (TRUE); 2195 vw->vw_proc = NULL; 2196 vw->vw_smode.mode = VT_AUTO; 2197 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid); 2198 vw->vw_pid = 0; 2199 } 2200 return (FALSE); 2201 } 2202 2203 static int 2204 signal_vt_rel(struct vt_window *vw) 2205 { 2206 2207 if (vw->vw_smode.mode != VT_PROCESS) 2208 return (FALSE); 2209 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) { 2210 vw->vw_proc = NULL; 2211 vw->vw_pid = 0; 2212 return (TRUE); 2213 } 2214 vw->vw_flags |= VWF_SWWAIT_REL; 2215 PROC_LOCK(vw->vw_proc); 2216 kern_psignal(vw->vw_proc, vw->vw_smode.relsig); 2217 PROC_UNLOCK(vw->vw_proc); 2218 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid); 2219 return (TRUE); 2220 } 2221 2222 static int 2223 signal_vt_acq(struct vt_window *vw) 2224 { 2225 2226 if (vw->vw_smode.mode != VT_PROCESS) 2227 return (FALSE); 2228 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 2229 cnavailable(vw->vw_terminal->consdev, FALSE); 2230 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) { 2231 vw->vw_proc = NULL; 2232 vw->vw_pid = 0; 2233 return (TRUE); 2234 } 2235 vw->vw_flags |= VWF_SWWAIT_ACQ; 2236 PROC_LOCK(vw->vw_proc); 2237 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig); 2238 PROC_UNLOCK(vw->vw_proc); 2239 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid); 2240 return (TRUE); 2241 } 2242 2243 static int 2244 finish_vt_rel(struct vt_window *vw, int release, int *s) 2245 { 2246 2247 if (vw->vw_flags & VWF_SWWAIT_REL) { 2248 vw->vw_flags &= ~VWF_SWWAIT_REL; 2249 if (release) { 2250 taskqueue_drain_timeout(taskqueue_thread, &vw->vw_timeout_task_dead); 2251 (void)vt_late_window_switch(vw->vw_switch_to); 2252 } 2253 return (0); 2254 } 2255 return (EINVAL); 2256 } 2257 2258 static int 2259 finish_vt_acq(struct vt_window *vw) 2260 { 2261 2262 if (vw->vw_flags & VWF_SWWAIT_ACQ) { 2263 vw->vw_flags &= ~VWF_SWWAIT_ACQ; 2264 return (0); 2265 } 2266 return (EINVAL); 2267 } 2268 2269 #ifndef SC_NO_CUTPASTE 2270 static void 2271 vt_mouse_terminput_button(struct vt_device *vd, int button) 2272 { 2273 struct vt_window *vw; 2274 struct vt_font *vf; 2275 char mouseb[6] = "\x1B[M"; 2276 int i, x, y; 2277 2278 vw = vd->vd_curwindow; 2279 vf = vw->vw_font; 2280 2281 /* Translate to char position. */ 2282 x = vd->vd_mx / vf->vf_width; 2283 y = vd->vd_my / vf->vf_height; 2284 /* Avoid overflow. */ 2285 x = MIN(x, 255 - '!'); 2286 y = MIN(y, 255 - '!'); 2287 2288 mouseb[3] = ' ' + button; 2289 mouseb[4] = '!' + x; 2290 mouseb[5] = '!' + y; 2291 2292 for (i = 0; i < sizeof(mouseb); i++) 2293 terminal_input_char(vw->vw_terminal, mouseb[i]); 2294 } 2295 2296 static void 2297 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event, 2298 int cnt) 2299 { 2300 2301 switch (type) { 2302 case MOUSE_BUTTON_EVENT: 2303 if (cnt > 0) { 2304 /* Mouse button pressed. */ 2305 if (event & MOUSE_BUTTON1DOWN) 2306 vt_mouse_terminput_button(vd, 0); 2307 if (event & MOUSE_BUTTON2DOWN) 2308 vt_mouse_terminput_button(vd, 1); 2309 if (event & MOUSE_BUTTON3DOWN) 2310 vt_mouse_terminput_button(vd, 2); 2311 } else { 2312 /* Mouse button released. */ 2313 vt_mouse_terminput_button(vd, 3); 2314 } 2315 break; 2316 #ifdef notyet 2317 case MOUSE_MOTION_EVENT: 2318 if (mouse->u.data.z < 0) { 2319 /* Scroll up. */ 2320 sc_mouse_input_button(vd, 64); 2321 } else if (mouse->u.data.z > 0) { 2322 /* Scroll down. */ 2323 sc_mouse_input_button(vd, 65); 2324 } 2325 break; 2326 #endif 2327 } 2328 } 2329 2330 static void 2331 vt_mouse_paste(void) 2332 { 2333 term_char_t *buf; 2334 int i, len; 2335 2336 len = VD_PASTEBUFLEN(main_vd); 2337 buf = VD_PASTEBUF(main_vd); 2338 len /= sizeof(term_char_t); 2339 for (i = 0; i < len; i++) { 2340 if (TCHAR_CHARACTER(buf[i]) == '\0') 2341 continue; 2342 terminal_input_char(main_vd->vd_curwindow->vw_terminal, 2343 buf[i]); 2344 } 2345 } 2346 2347 void 2348 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel) 2349 { 2350 struct vt_device *vd; 2351 struct vt_window *vw; 2352 struct vt_font *vf; 2353 term_pos_t size; 2354 int len, mark; 2355 2356 vd = main_vd; 2357 vw = vd->vd_curwindow; 2358 vf = vw->vw_font; 2359 2360 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS)) 2361 /* 2362 * Either the mouse is disabled, or the window is in 2363 * "graphics mode". The graphics mode is usually set by 2364 * an X server, using the KDSETMODE ioctl. 2365 */ 2366 return; 2367 2368 if (vf == NULL) /* Text mode. */ 2369 return; 2370 2371 /* 2372 * TODO: add flag about pointer position changed, to not redraw chars 2373 * under mouse pointer when nothing changed. 2374 */ 2375 2376 if (vw->vw_mouse_level > 0) 2377 vt_mouse_terminput(vd, type, x, y, event, cnt); 2378 2379 switch (type) { 2380 case MOUSE_ACTION: 2381 case MOUSE_MOTION_EVENT: 2382 /* Movement */ 2383 x += vd->vd_mx; 2384 y += vd->vd_my; 2385 2386 vt_termsize(vd, vf, &size); 2387 2388 /* Apply limits. */ 2389 x = MAX(x, 0); 2390 y = MAX(y, 0); 2391 x = MIN(x, (size.tp_col * vf->vf_width) - 1); 2392 y = MIN(y, (size.tp_row * vf->vf_height) - 1); 2393 2394 vd->vd_mx = x; 2395 vd->vd_my = y; 2396 if (vd->vd_mstate & (MOUSE_BUTTON1DOWN | VT_MOUSE_EXTENDBUTTON)) 2397 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE, 2398 vd->vd_mx / vf->vf_width, 2399 vd->vd_my / vf->vf_height); 2400 2401 vt_resume_flush_timer(vw, 0); 2402 return; /* Done */ 2403 case MOUSE_BUTTON_EVENT: 2404 /* Buttons */ 2405 break; 2406 default: 2407 return; /* Done */ 2408 } 2409 2410 switch (event) { 2411 case MOUSE_BUTTON1DOWN: 2412 switch (cnt % 4) { 2413 case 0: /* up */ 2414 mark = VTB_MARK_END; 2415 break; 2416 case 1: /* single click: start cut operation */ 2417 mark = VTB_MARK_START; 2418 break; 2419 case 2: /* double click: cut a word */ 2420 mark = VTB_MARK_WORD; 2421 break; 2422 default: /* triple click: cut a line */ 2423 mark = VTB_MARK_ROW; 2424 break; 2425 } 2426 break; 2427 case VT_MOUSE_PASTEBUTTON: 2428 switch (cnt) { 2429 case 0: /* up */ 2430 break; 2431 default: 2432 vt_mouse_paste(); 2433 /* clear paste buffer selection after paste */ 2434 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_START, 2435 vd->vd_mx / vf->vf_width, 2436 vd->vd_my / vf->vf_height); 2437 break; 2438 } 2439 return; /* Done */ 2440 case VT_MOUSE_EXTENDBUTTON: 2441 switch (cnt) { 2442 case 0: /* up */ 2443 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN)) 2444 mark = VTB_MARK_EXTEND; 2445 else 2446 mark = VTB_MARK_NONE; 2447 break; 2448 default: 2449 mark = VTB_MARK_EXTEND; 2450 break; 2451 } 2452 break; 2453 default: 2454 return; /* Done */ 2455 } 2456 2457 /* Save buttons state. */ 2458 if (cnt > 0) 2459 vd->vd_mstate |= event; 2460 else 2461 vd->vd_mstate &= ~event; 2462 2463 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width, 2464 vd->vd_my / vf->vf_height) == 1) { 2465 /* 2466 * We have something marked to copy, so update pointer to 2467 * window with selection. 2468 */ 2469 vt_resume_flush_timer(vw, 0); 2470 2471 switch (mark) { 2472 case VTB_MARK_END: 2473 case VTB_MARK_WORD: 2474 case VTB_MARK_ROW: 2475 case VTB_MARK_EXTEND: 2476 break; 2477 default: 2478 /* Other types of mark do not require to copy data. */ 2479 return; 2480 } 2481 2482 /* Get current selection size in bytes. */ 2483 len = vtbuf_get_marked_len(&vw->vw_buf); 2484 if (len <= 0) 2485 return; 2486 2487 /* Reallocate buffer only if old one is too small. */ 2488 if (len > VD_PASTEBUFSZ(vd)) { 2489 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT, 2490 M_WAITOK | M_ZERO); 2491 /* Update buffer size. */ 2492 VD_PASTEBUFSZ(vd) = len; 2493 } 2494 /* Request copy/paste buffer data, no more than `len' */ 2495 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd), len, mark); 2496 2497 VD_PASTEBUFLEN(vd) = len; 2498 2499 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */ 2500 } 2501 } 2502 2503 void 2504 vt_mouse_state(int show) 2505 { 2506 struct vt_device *vd; 2507 struct vt_window *vw; 2508 2509 vd = main_vd; 2510 vw = vd->vd_curwindow; 2511 2512 switch (show) { 2513 case VT_MOUSE_HIDE: 2514 vw->vw_flags |= VWF_MOUSE_HIDE; 2515 break; 2516 case VT_MOUSE_SHOW: 2517 vw->vw_flags &= ~VWF_MOUSE_HIDE; 2518 break; 2519 } 2520 2521 /* Mark mouse position as dirty. */ 2522 vt_mark_mouse_position_as_dirty(vd, false); 2523 vt_resume_flush_timer(vw, 0); 2524 } 2525 #endif 2526 2527 static int 2528 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr, 2529 int nprot, vm_memattr_t *memattr) 2530 { 2531 struct vt_window *vw = tm->tm_softc; 2532 struct vt_device *vd = vw->vw_device; 2533 2534 if (vd->vd_driver->vd_fb_mmap) 2535 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot, 2536 memattr)); 2537 2538 return (ENXIO); 2539 } 2540 2541 static int 2542 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, 2543 struct thread *td) 2544 { 2545 struct vt_window *vw = tm->tm_softc; 2546 struct vt_device *vd = vw->vw_device; 2547 keyboard_t *kbd; 2548 int error, i, s; 2549 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 2550 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 2551 int ival; 2552 2553 switch (cmd) { 2554 case _IO('v', 4): 2555 cmd = VT_RELDISP; 2556 break; 2557 case _IO('v', 5): 2558 cmd = VT_ACTIVATE; 2559 break; 2560 case _IO('v', 6): 2561 cmd = VT_WAITACTIVE; 2562 break; 2563 case _IO('K', 20): 2564 cmd = KDSKBSTATE; 2565 break; 2566 case _IO('K', 67): 2567 cmd = KDSETRAD; 2568 break; 2569 case _IO('K', 7): 2570 cmd = KDSKBMODE; 2571 break; 2572 case _IO('K', 8): 2573 cmd = KDMKTONE; 2574 break; 2575 case _IO('K', 10): 2576 cmd = KDSETMODE; 2577 break; 2578 case _IO('K', 13): 2579 cmd = KDSBORDER; 2580 break; 2581 case _IO('K', 63): 2582 cmd = KIOCSOUND; 2583 break; 2584 case _IO('K', 66): 2585 cmd = KDSETLED; 2586 break; 2587 case _IO('c', 104): 2588 cmd = CONS_SETWINORG; 2589 break; 2590 case _IO('c', 110): 2591 cmd = CONS_SETKBD; 2592 break; 2593 default: 2594 goto skip_thunk; 2595 } 2596 ival = IOCPARM_IVAL(data); 2597 data = (caddr_t)&ival; 2598 skip_thunk: 2599 #endif 2600 2601 switch (cmd) { 2602 case KDSETRAD: /* set keyboard repeat & delay rates (old) */ 2603 if (*(int *)data & ~0x7f) 2604 return (EINVAL); 2605 /* FALLTHROUGH */ 2606 case GIO_KEYMAP: 2607 case PIO_KEYMAP: 2608 case GIO_DEADKEYMAP: 2609 case PIO_DEADKEYMAP: 2610 #ifdef COMPAT_FREEBSD13 2611 case OGIO_DEADKEYMAP: 2612 case OPIO_DEADKEYMAP: 2613 #endif /* COMPAT_FREEBSD13 */ 2614 case GETFKEY: 2615 case SETFKEY: 2616 case KDGKBINFO: 2617 case KDGKBTYPE: 2618 case KDGETREPEAT: /* get keyboard repeat & delay rates */ 2619 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */ 2620 case KBADDKBD: /* add keyboard to mux */ 2621 case KBRELKBD: { /* release keyboard from mux */ 2622 error = 0; 2623 2624 mtx_lock(&Giant); 2625 if ((kbd = vd->vd_keyboard) != NULL) 2626 error = kbdd_ioctl(kbd, cmd, data); 2627 mtx_unlock(&Giant); 2628 if (error == ENOIOCTL) { 2629 if (cmd == KDGKBTYPE) { 2630 /* always return something? XXX */ 2631 *(int *)data = 0; 2632 } else { 2633 return (ENODEV); 2634 } 2635 } 2636 return (error); 2637 } 2638 case KDGKBSTATE: { /* get keyboard state (locks) */ 2639 error = 0; 2640 2641 if (vw == vd->vd_curwindow) { 2642 mtx_lock(&Giant); 2643 if ((kbd = vd->vd_keyboard) != NULL) 2644 error = vt_save_kbd_state(vw, kbd); 2645 mtx_unlock(&Giant); 2646 2647 if (error != 0) 2648 return (error); 2649 } 2650 2651 *(int *)data = vw->vw_kbdstate & LOCK_MASK; 2652 2653 return (error); 2654 } 2655 case KDSKBSTATE: { /* set keyboard state (locks) */ 2656 int state; 2657 2658 state = *(int *)data; 2659 if (state & ~LOCK_MASK) 2660 return (EINVAL); 2661 2662 vw->vw_kbdstate &= ~LOCK_MASK; 2663 vw->vw_kbdstate |= state; 2664 2665 error = 0; 2666 if (vw == vd->vd_curwindow) { 2667 mtx_lock(&Giant); 2668 if ((kbd = vd->vd_keyboard) != NULL) 2669 error = vt_update_kbd_state(vw, kbd); 2670 mtx_unlock(&Giant); 2671 } 2672 2673 return (error); 2674 } 2675 case KDGETLED: { /* get keyboard LED status */ 2676 error = 0; 2677 2678 if (vw == vd->vd_curwindow) { 2679 mtx_lock(&Giant); 2680 if ((kbd = vd->vd_keyboard) != NULL) 2681 error = vt_save_kbd_leds(vw, kbd); 2682 mtx_unlock(&Giant); 2683 2684 if (error != 0) 2685 return (error); 2686 } 2687 2688 *(int *)data = vw->vw_kbdstate & LED_MASK; 2689 2690 return (error); 2691 } 2692 case KDSETLED: { /* set keyboard LED status */ 2693 int leds; 2694 2695 leds = *(int *)data; 2696 if (leds & ~LED_MASK) 2697 return (EINVAL); 2698 2699 vw->vw_kbdstate &= ~LED_MASK; 2700 vw->vw_kbdstate |= leds; 2701 2702 error = 0; 2703 if (vw == vd->vd_curwindow) { 2704 mtx_lock(&Giant); 2705 if ((kbd = vd->vd_keyboard) != NULL) 2706 error = vt_update_kbd_leds(vw, kbd); 2707 mtx_unlock(&Giant); 2708 } 2709 2710 return (error); 2711 } 2712 case KDGETMODE: 2713 *(int *)data = (vw->vw_flags & VWF_GRAPHICS) ? 2714 KD_GRAPHICS : KD_TEXT; 2715 return (0); 2716 case KDGKBMODE: { 2717 error = 0; 2718 2719 if (vw == vd->vd_curwindow) { 2720 mtx_lock(&Giant); 2721 if ((kbd = vd->vd_keyboard) != NULL) 2722 error = vt_save_kbd_mode(vw, kbd); 2723 mtx_unlock(&Giant); 2724 2725 if (error != 0) 2726 return (error); 2727 } 2728 2729 *(int *)data = vw->vw_kbdmode; 2730 2731 return (error); 2732 } 2733 case KDSKBMODE: { 2734 int mode; 2735 2736 mode = *(int *)data; 2737 switch (mode) { 2738 case K_XLATE: 2739 case K_RAW: 2740 case K_CODE: 2741 vw->vw_kbdmode = mode; 2742 2743 error = 0; 2744 if (vw == vd->vd_curwindow) { 2745 mtx_lock(&Giant); 2746 if ((kbd = vd->vd_keyboard) != NULL) 2747 error = vt_update_kbd_mode(vw, kbd); 2748 mtx_unlock(&Giant); 2749 } 2750 2751 return (error); 2752 default: 2753 return (EINVAL); 2754 } 2755 } 2756 case FBIOGTYPE: 2757 case FBIO_GETWINORG: /* get frame buffer window origin */ 2758 case FBIO_GETDISPSTART: /* get display start address */ 2759 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */ 2760 case FBIO_BLANK: /* blank display */ 2761 case FBIO_GETRGBOFFS: /* get RGB offsets */ 2762 if (vd->vd_driver->vd_fb_ioctl) 2763 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td)); 2764 break; 2765 case CONS_BLANKTIME: 2766 /* XXX */ 2767 return (0); 2768 case CONS_HISTORY: 2769 if (*(int *)data < 0) 2770 return EINVAL; 2771 if (*(int *)data != vw->vw_buf.vb_history_size) 2772 vtbuf_sethistory_size(&vw->vw_buf, *(int *)data); 2773 return (0); 2774 case CONS_CLRHIST: 2775 vtbuf_clearhistory(&vw->vw_buf); 2776 /* 2777 * Invalidate the entire visible window; it is not guaranteed 2778 * that this operation will be immediately followed by a scroll 2779 * event, so it would otherwise be possible for prior artifacts 2780 * to remain visible. 2781 */ 2782 VT_LOCK(vd); 2783 if (vw == vd->vd_curwindow) { 2784 vd->vd_flags |= VDF_INVALID; 2785 vt_resume_flush_timer(vw, 0); 2786 } 2787 VT_UNLOCK(vd); 2788 return (0); 2789 case CONS_GET: 2790 /* XXX */ 2791 *(int *)data = M_CG640x480; 2792 return (0); 2793 case CONS_BELLTYPE: /* set bell type sound */ 2794 if ((*(int *)data) & CONS_QUIET_BELL) 2795 vd->vd_flags |= VDF_QUIET_BELL; 2796 else 2797 vd->vd_flags &= ~VDF_QUIET_BELL; 2798 return (0); 2799 case CONS_GETINFO: { 2800 vid_info_t *vi = (vid_info_t *)data; 2801 if (vi->size != sizeof(struct vid_info)) 2802 return (EINVAL); 2803 2804 if (vw == vd->vd_curwindow) { 2805 mtx_lock(&Giant); 2806 if ((kbd = vd->vd_keyboard) != NULL) 2807 vt_save_kbd_state(vw, kbd); 2808 mtx_unlock(&Giant); 2809 } 2810 2811 vi->m_num = vd->vd_curwindow->vw_number + 1; 2812 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK; 2813 /* XXX: other fields! */ 2814 return (0); 2815 } 2816 case CONS_GETVERS: 2817 *(int *)data = 0x200; 2818 return (0); 2819 case CONS_MODEINFO: 2820 /* XXX */ 2821 return (0); 2822 case CONS_MOUSECTL: { 2823 mouse_info_t *mouse = (mouse_info_t*)data; 2824 2825 /* 2826 * All the commands except MOUSE_SHOW nd MOUSE_HIDE 2827 * should not be applied to individual TTYs, but only to 2828 * consolectl. 2829 */ 2830 switch (mouse->operation) { 2831 case MOUSE_HIDE: 2832 if (vd->vd_flags & VDF_MOUSECURSOR) { 2833 vd->vd_flags &= ~VDF_MOUSECURSOR; 2834 #ifndef SC_NO_CUTPASTE 2835 vt_mouse_state(VT_MOUSE_HIDE); 2836 #endif 2837 } 2838 return (0); 2839 case MOUSE_SHOW: 2840 if (!(vd->vd_flags & VDF_MOUSECURSOR)) { 2841 vd->vd_flags |= VDF_MOUSECURSOR; 2842 vd->vd_mx = vd->vd_width / 2; 2843 vd->vd_my = vd->vd_height / 2; 2844 #ifndef SC_NO_CUTPASTE 2845 vt_mouse_state(VT_MOUSE_SHOW); 2846 #endif 2847 } 2848 return (0); 2849 default: 2850 return (EINVAL); 2851 } 2852 } 2853 case PIO_VFONT: { 2854 struct vt_font *vf; 2855 2856 if (vd->vd_flags & VDF_TEXTMODE) 2857 return (ENOTSUP); 2858 2859 error = vtfont_load((void *)data, &vf); 2860 if (error != 0) 2861 return (error); 2862 2863 error = vt_change_font(vw, vf); 2864 vtfont_unref(vf); 2865 return (error); 2866 } 2867 case PIO_VFONT_DEFAULT: { 2868 /* Reset to default font. */ 2869 error = vt_change_font(vw, vt_font_assigned); 2870 return (error); 2871 } 2872 case GIO_SCRNMAP: { 2873 scrmap_t *sm = (scrmap_t *)data; 2874 2875 /* We don't have screen maps, so return a handcrafted one. */ 2876 for (i = 0; i < 256; i++) 2877 sm->scrmap[i] = i; 2878 return (0); 2879 } 2880 case KDSETMODE: 2881 /* 2882 * FIXME: This implementation is incomplete compared to 2883 * syscons. 2884 */ 2885 switch (*(int *)data) { 2886 case KD_TEXT: 2887 case KD_TEXT1: 2888 case KD_PIXEL: 2889 vw->vw_flags &= ~VWF_GRAPHICS; 2890 break; 2891 case KD_GRAPHICS: 2892 vw->vw_flags |= VWF_GRAPHICS; 2893 break; 2894 } 2895 return (0); 2896 case KDENABIO: /* allow io operations */ 2897 error = priv_check(td, PRIV_IO); 2898 if (error != 0) 2899 return (error); 2900 error = securelevel_gt(td->td_ucred, 0); 2901 if (error != 0) 2902 return (error); 2903 #if defined(__i386__) 2904 td->td_frame->tf_eflags |= PSL_IOPL; 2905 #elif defined(__amd64__) 2906 td->td_frame->tf_rflags |= PSL_IOPL; 2907 #endif 2908 return (0); 2909 case KDDISABIO: /* disallow io operations (default) */ 2910 #if defined(__i386__) 2911 td->td_frame->tf_eflags &= ~PSL_IOPL; 2912 #elif defined(__amd64__) 2913 td->td_frame->tf_rflags &= ~PSL_IOPL; 2914 #endif 2915 return (0); 2916 case KDMKTONE: /* sound the bell */ 2917 vtterm_beep(tm, *(u_int *)data); 2918 return (0); 2919 case KIOCSOUND: /* make tone (*data) hz */ 2920 /* TODO */ 2921 return (0); 2922 case CONS_SETKBD: /* set the new keyboard */ 2923 mtx_lock(&Giant); 2924 error = 0; 2925 if (vd->vd_keyboard == NULL || 2926 vd->vd_keyboard->kb_index != *(int *)data) { 2927 kbd = kbd_get_keyboard(*(int *)data); 2928 if (kbd == NULL) { 2929 mtx_unlock(&Giant); 2930 return (EINVAL); 2931 } 2932 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, 2933 (void *)vd, vt_kbdevent, vd); 2934 if (i >= 0) { 2935 if ((kbd = vd->vd_keyboard) != NULL) { 2936 vt_save_kbd_state(vd->vd_curwindow, kbd); 2937 kbd_release(kbd, (void *)vd); 2938 } 2939 kbd = vd->vd_keyboard = kbd_get_keyboard(i); 2940 2941 vt_update_kbd_mode(vd->vd_curwindow, kbd); 2942 vt_update_kbd_state(vd->vd_curwindow, kbd); 2943 } else { 2944 error = EPERM; /* XXX */ 2945 } 2946 } 2947 mtx_unlock(&Giant); 2948 return (error); 2949 case CONS_RELKBD: /* release the current keyboard */ 2950 mtx_lock(&Giant); 2951 error = 0; 2952 if ((kbd = vd->vd_keyboard) != NULL) { 2953 vt_save_kbd_state(vd->vd_curwindow, kbd); 2954 error = kbd_release(kbd, (void *)vd); 2955 if (error == 0) { 2956 vd->vd_keyboard = NULL; 2957 } 2958 } 2959 mtx_unlock(&Giant); 2960 return (error); 2961 case VT_ACTIVATE: { 2962 int win; 2963 win = *(int *)data - 1; 2964 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME, 2965 VT_UNIT(vw), win); 2966 if ((win >= VT_MAXWINDOWS) || (win < 0)) 2967 return (EINVAL); 2968 return (vt_proc_window_switch(vd->vd_windows[win])); 2969 } 2970 case VT_GETACTIVE: 2971 *(int *)data = vd->vd_curwindow->vw_number + 1; 2972 return (0); 2973 case VT_GETINDEX: 2974 *(int *)data = vw->vw_number + 1; 2975 return (0); 2976 case VT_LOCKSWITCH: 2977 /* TODO: Check current state, switching can be in progress. */ 2978 if ((*(int *)data) == 0x01) 2979 vw->vw_flags |= VWF_VTYLOCK; 2980 else if ((*(int *)data) == 0x02) 2981 vw->vw_flags &= ~VWF_VTYLOCK; 2982 else 2983 return (EINVAL); 2984 return (0); 2985 case VT_OPENQRY: 2986 VT_LOCK(vd); 2987 for (i = 0; i < VT_MAXWINDOWS; i++) { 2988 vw = vd->vd_windows[i]; 2989 if (vw == NULL) 2990 continue; 2991 if (!(vw->vw_flags & VWF_OPENED)) { 2992 *(int *)data = vw->vw_number + 1; 2993 VT_UNLOCK(vd); 2994 return (0); 2995 } 2996 } 2997 VT_UNLOCK(vd); 2998 return (EINVAL); 2999 case VT_WAITACTIVE: { 3000 unsigned int idx; 3001 3002 error = 0; 3003 3004 idx = *(unsigned int *)data; 3005 if (idx > VT_MAXWINDOWS) 3006 return (EINVAL); 3007 if (idx > 0) 3008 vw = vd->vd_windows[idx - 1]; 3009 3010 VT_LOCK(vd); 3011 while (vd->vd_curwindow != vw && error == 0) 3012 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock); 3013 VT_UNLOCK(vd); 3014 return (error); 3015 } 3016 case VT_SETMODE: { /* set screen switcher mode */ 3017 struct vt_mode *mode; 3018 struct proc *p1; 3019 3020 mode = (struct vt_mode *)data; 3021 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw)); 3022 if (vw->vw_smode.mode == VT_PROCESS) { 3023 p1 = pfind(vw->vw_pid); 3024 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) { 3025 if (p1) 3026 PROC_UNLOCK(p1); 3027 DPRINTF(5, "error EPERM\n"); 3028 return (EPERM); 3029 } 3030 if (p1) 3031 PROC_UNLOCK(p1); 3032 } 3033 if (mode->mode == VT_AUTO) { 3034 vw->vw_smode.mode = VT_AUTO; 3035 vw->vw_proc = NULL; 3036 vw->vw_pid = 0; 3037 DPRINTF(5, "VT_AUTO, "); 3038 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 3039 cnavailable(vw->vw_terminal->consdev, TRUE); 3040 /* were we in the middle of the vty switching process? */ 3041 if (finish_vt_rel(vw, TRUE, &s) == 0) 3042 DPRINTF(5, "reset WAIT_REL, "); 3043 if (finish_vt_acq(vw) == 0) 3044 DPRINTF(5, "reset WAIT_ACQ, "); 3045 return (0); 3046 } else if (mode->mode == VT_PROCESS) { 3047 if (!ISSIGVALID(mode->relsig) || 3048 !ISSIGVALID(mode->acqsig) || 3049 !ISSIGVALID(mode->frsig)) { 3050 DPRINTF(5, "error EINVAL\n"); 3051 return (EINVAL); 3052 } 3053 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid); 3054 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode)); 3055 vw->vw_proc = td->td_proc; 3056 vw->vw_pid = vw->vw_proc->p_pid; 3057 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 3058 cnavailable(vw->vw_terminal->consdev, FALSE); 3059 } else { 3060 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n", 3061 mode->mode); 3062 return (EINVAL); 3063 } 3064 DPRINTF(5, "\n"); 3065 return (0); 3066 } 3067 case VT_GETMODE: /* get screen switcher mode */ 3068 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode)); 3069 return (0); 3070 3071 case VT_RELDISP: /* screen switcher ioctl */ 3072 /* 3073 * This must be the current vty which is in the VT_PROCESS 3074 * switching mode... 3075 */ 3076 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode != 3077 VT_PROCESS)) { 3078 return (EINVAL); 3079 } 3080 /* ...and this process is controlling it. */ 3081 if (vw->vw_proc != td->td_proc) { 3082 return (EPERM); 3083 } 3084 error = EINVAL; 3085 switch(*(int *)data) { 3086 case VT_FALSE: /* user refuses to release screen, abort */ 3087 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0) 3088 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n", 3089 SC_DRIVER_NAME, VT_UNIT(vw)); 3090 break; 3091 case VT_TRUE: /* user has released screen, go on */ 3092 /* finish_vt_rel(..., TRUE, ...) should not be locked */ 3093 if (vw->vw_flags & VWF_SWWAIT_REL) { 3094 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0) 3095 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n", 3096 SC_DRIVER_NAME, VT_UNIT(vw)); 3097 } else { 3098 error = EINVAL; 3099 } 3100 return (error); 3101 case VT_ACKACQ: /* acquire acknowledged, switch completed */ 3102 if ((error = finish_vt_acq(vw)) == 0) 3103 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n", 3104 SC_DRIVER_NAME, VT_UNIT(vw)); 3105 break; 3106 default: 3107 break; 3108 } 3109 return (error); 3110 } 3111 3112 return (ENOIOCTL); 3113 } 3114 3115 static struct vt_window * 3116 vt_allocate_window(struct vt_device *vd, unsigned int window) 3117 { 3118 struct vt_window *vw; 3119 struct terminal *tm; 3120 term_pos_t size; 3121 struct winsize wsz; 3122 3123 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO); 3124 vw->vw_device = vd; 3125 vw->vw_number = window; 3126 vw->vw_kbdmode = K_XLATE; 3127 3128 if ((vd->vd_flags & VDF_TEXTMODE) == 0) { 3129 vw->vw_font = vtfont_ref(vt_font_assigned); 3130 vt_compute_drawable_area(vw); 3131 } 3132 3133 vt_termsize(vd, vw->vw_font, &size); 3134 vt_winsize(vd, vw->vw_font, &wsz); 3135 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw); 3136 vw->vw_buf.vb_terminal = tm; /* must be set before vtbuf_init() */ 3137 vtbuf_init(&vw->vw_buf, &size); 3138 3139 terminal_set_winsize(tm, &wsz); 3140 vd->vd_windows[window] = vw; 3141 TIMEOUT_TASK_INIT(taskqueue_thread, &vw->vw_timeout_task_dead, 0, &vt_switch_timer, vw); 3142 3143 return (vw); 3144 } 3145 3146 void 3147 vt_upgrade(struct vt_device *vd) 3148 { 3149 struct vt_window *vw; 3150 unsigned int i; 3151 int register_handlers; 3152 3153 if (!vty_enabled(VTY_VT)) 3154 return; 3155 if (main_vd->vd_driver == NULL) 3156 return; 3157 3158 for (i = 0; i < VT_MAXWINDOWS; i++) { 3159 vw = vd->vd_windows[i]; 3160 if (vw == NULL) { 3161 /* New window. */ 3162 vw = vt_allocate_window(vd, i); 3163 } 3164 if (!(vw->vw_flags & VWF_READY)) { 3165 TIMEOUT_TASK_INIT(taskqueue_thread, &vw->vw_timeout_task_dead, 0, &vt_switch_timer, vw); 3166 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw)); 3167 vw->vw_flags |= VWF_READY; 3168 if (vw->vw_flags & VWF_CONSOLE) { 3169 /* For existing console window. */ 3170 EVENTHANDLER_REGISTER(shutdown_pre_sync, 3171 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT); 3172 } 3173 } 3174 } 3175 VT_LOCK(vd); 3176 if (vd->vd_curwindow == NULL) 3177 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW]; 3178 3179 register_handlers = 0; 3180 if (!(vd->vd_flags & VDF_ASYNC)) { 3181 /* Attach keyboard. */ 3182 vt_allocate_keyboard(vd); 3183 3184 /* Init 25 Hz timer. */ 3185 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0); 3186 3187 /* 3188 * Start timer when everything ready. 3189 * Note that the operations here are purposefully ordered. 3190 * We need to ensure vd_timer_armed is non-zero before we set 3191 * the VDF_ASYNC flag. That prevents this function from 3192 * racing with vt_resume_flush_timer() to update the 3193 * callout structure. 3194 */ 3195 atomic_add_acq_int(&vd->vd_timer_armed, 1); 3196 vd->vd_flags |= VDF_ASYNC; 3197 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd); 3198 register_handlers = 1; 3199 } 3200 3201 VT_UNLOCK(vd); 3202 3203 /* Refill settings with new sizes. */ 3204 vt_resize(vd); 3205 3206 if (register_handlers) { 3207 /* Register suspend/resume handlers. */ 3208 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler, 3209 vd, EVENTHANDLER_PRI_ANY); 3210 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd, 3211 EVENTHANDLER_PRI_ANY); 3212 } 3213 } 3214 3215 static void 3216 vt_resize(struct vt_device *vd) 3217 { 3218 struct vt_window *vw; 3219 int i; 3220 3221 for (i = 0; i < VT_MAXWINDOWS; i++) { 3222 vw = vd->vd_windows[i]; 3223 VT_LOCK(vd); 3224 /* Assign default font to window, if not textmode. */ 3225 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL) 3226 vw->vw_font = vtfont_ref(vt_font_assigned); 3227 VT_UNLOCK(vd); 3228 3229 /* Resize terminal windows */ 3230 while (vt_change_font(vw, vw->vw_font) == EBUSY) { 3231 DPRINTF(100, "%s: vt_change_font() is busy, " 3232 "window %d\n", __func__, i); 3233 } 3234 } 3235 } 3236 3237 static void 3238 vt_replace_backend(const struct vt_driver *drv, void *softc) 3239 { 3240 struct vt_device *vd; 3241 3242 vd = main_vd; 3243 3244 if (vd->vd_flags & VDF_ASYNC) { 3245 /* Stop vt_flush periodic task. */ 3246 VT_LOCK(vd); 3247 vt_suspend_flush_timer(vd); 3248 VT_UNLOCK(vd); 3249 /* 3250 * Mute current terminal until we done. vt_change_font (called 3251 * from vt_resize) will unmute it. 3252 */ 3253 terminal_mute(vd->vd_curwindow->vw_terminal, 1); 3254 } 3255 3256 /* 3257 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will 3258 * set it. 3259 */ 3260 VT_LOCK(vd); 3261 vd->vd_flags &= ~VDF_TEXTMODE; 3262 3263 if (drv != NULL) { 3264 /* 3265 * We want to upgrade from the current driver to the 3266 * given driver. 3267 */ 3268 3269 vd->vd_prev_driver = vd->vd_driver; 3270 vd->vd_prev_softc = vd->vd_softc; 3271 vd->vd_driver = drv; 3272 vd->vd_softc = softc; 3273 3274 vd->vd_driver->vd_init(vd); 3275 } else if (vd->vd_prev_driver != NULL && vd->vd_prev_softc != NULL) { 3276 /* 3277 * No driver given: we want to downgrade to the previous 3278 * driver. 3279 */ 3280 const struct vt_driver *old_drv; 3281 void *old_softc; 3282 3283 old_drv = vd->vd_driver; 3284 old_softc = vd->vd_softc; 3285 3286 vd->vd_driver = vd->vd_prev_driver; 3287 vd->vd_softc = vd->vd_prev_softc; 3288 vd->vd_prev_driver = NULL; 3289 vd->vd_prev_softc = NULL; 3290 3291 vd->vd_flags |= VDF_DOWNGRADE; 3292 3293 vd->vd_driver->vd_init(vd); 3294 3295 if (old_drv->vd_fini) 3296 old_drv->vd_fini(vd, old_softc); 3297 3298 vd->vd_flags &= ~VDF_DOWNGRADE; 3299 } 3300 3301 VT_UNLOCK(vd); 3302 3303 /* Update windows sizes and initialize last items. */ 3304 vt_upgrade(vd); 3305 3306 /* 3307 * Give a chance to the new backend to run the post-switch code, for 3308 * instance to refresh the screen. 3309 */ 3310 if (vd->vd_driver->vd_postswitch) 3311 vd->vd_driver->vd_postswitch(vd); 3312 3313 #ifdef DEV_SPLASH 3314 if (vd->vd_flags & VDF_SPLASH) 3315 vtterm_splash(vd); 3316 #endif 3317 3318 if (vd->vd_flags & VDF_ASYNC) { 3319 /* Allow to put chars now. */ 3320 terminal_mute(vd->vd_curwindow->vw_terminal, 0); 3321 /* Rerun timer for screen updates. */ 3322 vt_resume_flush_timer(vd->vd_curwindow, 0); 3323 } 3324 3325 /* 3326 * Register as console. If it already registered, cnadd() will ignore 3327 * it. 3328 */ 3329 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal); 3330 } 3331 3332 static void 3333 vt_suspend_handler(void *priv) 3334 { 3335 struct vt_device *vd; 3336 3337 vd = priv; 3338 vd->vd_flags |= VDF_SUSPENDED; 3339 if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL) 3340 vd->vd_driver->vd_suspend(vd); 3341 } 3342 3343 static void 3344 vt_resume_handler(void *priv) 3345 { 3346 struct vt_device *vd; 3347 3348 vd = priv; 3349 if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL) 3350 vd->vd_driver->vd_resume(vd); 3351 vd->vd_flags &= ~VDF_SUSPENDED; 3352 } 3353 3354 int 3355 vt_allocate(const struct vt_driver *drv, void *softc) 3356 { 3357 3358 if (!vty_enabled(VTY_VT)) 3359 return (EINVAL); 3360 3361 if (main_vd->vd_driver == NULL) { 3362 main_vd->vd_driver = drv; 3363 printf("VT: initialize with new VT driver \"%s\".\n", 3364 drv->vd_name); 3365 } else { 3366 /* 3367 * Check if have rights to replace current driver. For example: 3368 * it is bad idea to replace KMS driver with generic VGA one. 3369 */ 3370 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) { 3371 printf("VT: Driver priority %d too low. Current %d\n ", 3372 drv->vd_priority, main_vd->vd_driver->vd_priority); 3373 return (EEXIST); 3374 } 3375 printf("VT: Replacing driver \"%s\" with new \"%s\".\n", 3376 main_vd->vd_driver->vd_name, drv->vd_name); 3377 } 3378 3379 vt_replace_backend(drv, softc); 3380 3381 return (0); 3382 } 3383 3384 int 3385 vt_deallocate(const struct vt_driver *drv, void *softc) 3386 { 3387 3388 if (!vty_enabled(VTY_VT)) 3389 return (EINVAL); 3390 3391 if (main_vd->vd_prev_driver == NULL || 3392 main_vd->vd_driver != drv || 3393 main_vd->vd_softc != softc) 3394 return (EPERM); 3395 3396 printf("VT: Switching back from \"%s\" to \"%s\".\n", 3397 main_vd->vd_driver->vd_name, main_vd->vd_prev_driver->vd_name); 3398 3399 vt_replace_backend(NULL, NULL); 3400 3401 return (0); 3402 } 3403 3404 void 3405 vt_suspend(struct vt_device *vd) 3406 { 3407 int error; 3408 3409 if (vt_suspendswitch == 0) 3410 return; 3411 /* Save current window. */ 3412 vd->vd_savedwindow = vd->vd_curwindow; 3413 /* Ask holding process to free window and switch to console window */ 3414 vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]); 3415 3416 /* Wait for the window switch to complete. */ 3417 error = 0; 3418 VT_LOCK(vd); 3419 while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0) 3420 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock); 3421 VT_UNLOCK(vd); 3422 } 3423 3424 void 3425 vt_resume(struct vt_device *vd) 3426 { 3427 3428 if (vt_suspendswitch == 0) 3429 return; 3430 /* Switch back to saved window, if any */ 3431 vt_proc_window_switch(vd->vd_savedwindow); 3432 vd->vd_savedwindow = NULL; 3433 } 3434