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 caddr_t kmdp; 1679 struct splash_info *si; 1680 uintptr_t image; 1681 vt_axis_t top, left; 1682 1683 kmdp = preload_search_by_type("elf kernel"); 1684 if (kmdp == NULL) 1685 kmdp = preload_search_by_type("elf64 kernel"); 1686 si = MD_FETCH(kmdp, MODINFOMD_SPLASH, struct splash_info *); 1687 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) { 1688 if (si == NULL) { 1689 top = (vd->vd_height - vt_logo_height) / 2; 1690 left = (vd->vd_width - vt_logo_width) / 2; 1691 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow, 1692 vt_logo_image, NULL, vt_logo_width, vt_logo_height, 1693 left, top, TC_WHITE, TC_BLACK); 1694 } else { 1695 if (si->si_depth != 4) 1696 return; 1697 image = (uintptr_t)si + sizeof(struct splash_info); 1698 image = roundup2(image, 8); 1699 top = (vd->vd_height - si->si_height) / 2; 1700 left = (vd->vd_width - si->si_width) / 2; 1701 vd->vd_driver->vd_bitblt_argb(vd, vd->vd_curwindow, 1702 (unsigned char *)image, si->si_width, si->si_height, 1703 left, top); 1704 } 1705 vd->vd_flags |= VDF_SPLASH; 1706 } 1707 } 1708 #endif 1709 1710 static struct vt_font * 1711 parse_font_info_static(struct font_info *fi) 1712 { 1713 struct vt_font *vfp; 1714 uintptr_t ptr; 1715 uint32_t checksum; 1716 1717 if (fi == NULL) 1718 return (NULL); 1719 1720 ptr = (uintptr_t)fi; 1721 /* 1722 * Compute and verify checksum. The total sum of all the fields 1723 * must be 0. 1724 */ 1725 checksum = fi->fi_width; 1726 checksum += fi->fi_height; 1727 checksum += fi->fi_bitmap_size; 1728 for (unsigned i = 0; i < VFNT_MAPS; i++) 1729 checksum += fi->fi_map_count[i]; 1730 1731 if (checksum + fi->fi_checksum != 0) 1732 return (NULL); 1733 1734 ptr += sizeof(struct font_info); 1735 ptr = roundup2(ptr, 8); 1736 1737 vfp = &vt_font_loader; 1738 vfp->vf_height = fi->fi_height; 1739 vfp->vf_width = fi->fi_width; 1740 /* This is default font, set refcount 1 to disable removal. */ 1741 vfp->vf_refcount = 1; 1742 for (unsigned i = 0; i < VFNT_MAPS; i++) { 1743 if (fi->fi_map_count[i] == 0) 1744 continue; 1745 vfp->vf_map_count[i] = fi->fi_map_count[i]; 1746 vfp->vf_map[i] = (vfnt_map_t *)ptr; 1747 ptr += (fi->fi_map_count[i] * sizeof(vfnt_map_t)); 1748 ptr = roundup2(ptr, 8); 1749 } 1750 vfp->vf_bytes = (uint8_t *)ptr; 1751 return (vfp); 1752 } 1753 1754 /* 1755 * Set up default font with allocated data structures. 1756 * However, we can not set refcount here, because it is already set and 1757 * incremented in vtterm_cnprobe() to avoid being released by font load from 1758 * userland. 1759 */ 1760 static struct vt_font * 1761 parse_font_info(struct font_info *fi) 1762 { 1763 struct vt_font *vfp; 1764 uintptr_t ptr; 1765 uint32_t checksum; 1766 size_t size; 1767 1768 if (fi == NULL) 1769 return (NULL); 1770 1771 ptr = (uintptr_t)fi; 1772 /* 1773 * Compute and verify checksum. The total sum of all the fields 1774 * must be 0. 1775 */ 1776 checksum = fi->fi_width; 1777 checksum += fi->fi_height; 1778 checksum += fi->fi_bitmap_size; 1779 for (unsigned i = 0; i < VFNT_MAPS; i++) 1780 checksum += fi->fi_map_count[i]; 1781 1782 if (checksum + fi->fi_checksum != 0) 1783 return (NULL); 1784 1785 ptr += sizeof(struct font_info); 1786 ptr = roundup2(ptr, 8); 1787 1788 vfp = &vt_font_loader; 1789 vfp->vf_height = fi->fi_height; 1790 vfp->vf_width = fi->fi_width; 1791 for (unsigned i = 0; i < VFNT_MAPS; i++) { 1792 if (fi->fi_map_count[i] == 0) 1793 continue; 1794 vfp->vf_map_count[i] = fi->fi_map_count[i]; 1795 size = fi->fi_map_count[i] * sizeof(vfnt_map_t); 1796 vfp->vf_map[i] = malloc(size, M_VT, M_WAITOK | M_ZERO); 1797 bcopy((vfnt_map_t *)ptr, vfp->vf_map[i], size); 1798 ptr += size; 1799 ptr = roundup2(ptr, 8); 1800 } 1801 vfp->vf_bytes = malloc(fi->fi_bitmap_size, M_VT, M_WAITOK | M_ZERO); 1802 bcopy((uint8_t *)ptr, vfp->vf_bytes, fi->fi_bitmap_size); 1803 return (vfp); 1804 } 1805 1806 static void 1807 vt_init_font(void *arg) 1808 { 1809 caddr_t kmdp; 1810 struct font_info *fi; 1811 struct vt_font *font; 1812 1813 kmdp = preload_search_by_type("elf kernel"); 1814 if (kmdp == NULL) 1815 kmdp = preload_search_by_type("elf64 kernel"); 1816 fi = MD_FETCH(kmdp, MODINFOMD_FONT, struct font_info *); 1817 1818 font = parse_font_info(fi); 1819 if (font != NULL) 1820 vt_font_assigned = font; 1821 } 1822 1823 SYSINIT(vt_init_font, SI_SUB_KMEM, SI_ORDER_ANY, vt_init_font, &vt_consdev); 1824 1825 static void 1826 vt_init_font_static(void) 1827 { 1828 caddr_t kmdp; 1829 struct font_info *fi; 1830 struct vt_font *font; 1831 1832 kmdp = preload_search_by_type("elf kernel"); 1833 if (kmdp == NULL) 1834 kmdp = preload_search_by_type("elf64 kernel"); 1835 fi = MD_FETCH(kmdp, MODINFOMD_FONT, struct font_info *); 1836 1837 font = parse_font_info_static(fi); 1838 if (font != NULL) 1839 vt_font_assigned = font; 1840 } 1841 1842 static void 1843 vtterm_cnprobe(struct terminal *tm, struct consdev *cp) 1844 { 1845 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL; 1846 struct vt_window *vw = tm->tm_softc; 1847 struct vt_device *vd = vw->vw_device; 1848 struct winsize wsz; 1849 const term_attr_t *a; 1850 1851 if (!vty_enabled(VTY_VT)) 1852 return; 1853 1854 if (vd->vd_flags & VDF_INITIALIZED) 1855 /* Initialization already done. */ 1856 return; 1857 1858 SET_FOREACH(vtdlist, vt_drv_set) { 1859 vtd = *vtdlist; 1860 if (vtd->vd_probe == NULL) 1861 continue; 1862 if (vtd->vd_probe(vd) == CN_DEAD) 1863 continue; 1864 if ((vtdbest == NULL) || 1865 (vtd->vd_priority > vtdbest->vd_priority)) 1866 vtdbest = vtd; 1867 } 1868 if (vtdbest == NULL) { 1869 cp->cn_pri = CN_DEAD; 1870 vd->vd_flags |= VDF_DEAD; 1871 } else { 1872 vd->vd_driver = vtdbest; 1873 cp->cn_pri = vd->vd_driver->vd_init(vd); 1874 } 1875 1876 /* Check if driver's vt_init return CN_DEAD. */ 1877 if (cp->cn_pri == CN_DEAD) { 1878 vd->vd_flags |= VDF_DEAD; 1879 } 1880 1881 /* Initialize any early-boot keyboard drivers */ 1882 kbd_configure(KB_CONF_PROBE_ONLY); 1883 1884 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1); 1885 vd->vd_windows[VT_CONSWINDOW] = vw; 1886 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw)); 1887 1888 vt_init_font_static(); 1889 1890 /* Attach default font if not in TEXTMODE. */ 1891 if ((vd->vd_flags & VDF_TEXTMODE) == 0) { 1892 vw->vw_font = vtfont_ref(vt_font_assigned); 1893 vt_compute_drawable_area(vw); 1894 } 1895 1896 /* 1897 * The original screen size was faked (_VTDEFW x _VTDEFH). Now 1898 * that we have the real viewable size, fix it in the static 1899 * buffer. 1900 */ 1901 if (vd->vd_width != 0 && vd->vd_height != 0) 1902 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size); 1903 1904 /* We need to access terminal attributes from vtbuf */ 1905 vw->vw_buf.vb_terminal = tm; 1906 vtbuf_init_early(&vw->vw_buf); 1907 vt_winsize(vd, vw->vw_font, &wsz); 1908 a = teken_get_curattr(&tm->tm_emulator); 1909 terminal_set_winsize_blank(tm, &wsz, 1, a); 1910 1911 if (vtdbest != NULL) { 1912 #ifdef DEV_SPLASH 1913 if (!vt_splash_cpu) 1914 vtterm_splash(vd); 1915 #endif 1916 vd->vd_flags |= VDF_INITIALIZED; 1917 } 1918 } 1919 1920 static int 1921 vtterm_cngetc(struct terminal *tm) 1922 { 1923 struct vt_window *vw = tm->tm_softc; 1924 struct vt_device *vd = vw->vw_device; 1925 keyboard_t *kbd; 1926 u_int c; 1927 1928 if (vw->vw_kbdsq && *vw->vw_kbdsq) 1929 return (*vw->vw_kbdsq++); 1930 1931 /* Make sure the splash screen is not there. */ 1932 if (vd->vd_flags & VDF_SPLASH) { 1933 /* Remove splash */ 1934 vd->vd_flags &= ~VDF_SPLASH; 1935 /* Mark screen as invalid to force update */ 1936 vd->vd_flags |= VDF_INVALID; 1937 vt_flush(vd); 1938 } 1939 1940 /* Stripped down keyboard handler. */ 1941 if ((kbd = vd->vd_keyboard) == NULL) 1942 return (-1); 1943 1944 /* Force keyboard input mode to K_XLATE */ 1945 vw->vw_kbdmode = K_XLATE; 1946 vt_update_kbd_mode(vw, kbd); 1947 1948 /* Switch the keyboard to polling to make it work here. */ 1949 kbdd_poll(kbd, TRUE); 1950 c = kbdd_read_char(kbd, 0); 1951 kbdd_poll(kbd, FALSE); 1952 if (c & RELKEY) 1953 return (-1); 1954 1955 if (vw->vw_flags & VWF_SCROLL) { 1956 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */); 1957 vt_flush(vd); 1958 return (-1); 1959 } 1960 1961 /* Stripped down handling of vt_kbdevent(), without locking, etc. */ 1962 if (c & SPCLKEY) { 1963 switch (c) { 1964 case SPCLKEY | SLK: 1965 vt_save_kbd_state(vw, kbd); 1966 if (vw->vw_kbdstate & SLKED) { 1967 /* Turn scrolling on. */ 1968 vw->vw_flags |= VWF_SCROLL; 1969 VTBUF_SLCK_ENABLE(&vw->vw_buf); 1970 } else { 1971 /* Turn scrolling off. */ 1972 vt_scroll(vw, 0, VHS_END); 1973 vw->vw_flags &= ~VWF_SCROLL; 1974 VTBUF_SLCK_DISABLE(&vw->vw_buf); 1975 } 1976 break; 1977 /* XXX: KDB can handle history. */ 1978 case SPCLKEY | FKEY | F(50): /* Arrow up. */ 1979 vw->vw_kbdsq = "\x1b[A"; 1980 break; 1981 case SPCLKEY | FKEY | F(58): /* Arrow down. */ 1982 vw->vw_kbdsq = "\x1b[B"; 1983 break; 1984 case SPCLKEY | FKEY | F(55): /* Arrow right. */ 1985 vw->vw_kbdsq = "\x1b[C"; 1986 break; 1987 case SPCLKEY | FKEY | F(53): /* Arrow left. */ 1988 vw->vw_kbdsq = "\x1b[D"; 1989 break; 1990 } 1991 1992 /* Force refresh to make scrollback work. */ 1993 vt_flush(vd); 1994 } else if (KEYFLAGS(c) == 0) { 1995 return (KEYCHAR(c)); 1996 } 1997 1998 if (vw->vw_kbdsq && *vw->vw_kbdsq) 1999 return (*vw->vw_kbdsq++); 2000 2001 return (-1); 2002 } 2003 2004 /* 2005 * These two do most of what we want to do in vtterm_cnungrab, but without 2006 * actually switching windows. This is necessary for, e.g., 2007 * vt_allocate_keyboard() to get the current keyboard into the state it needs to 2008 * be in without damaging the device's window state. 2009 * 2010 * Both return the current grab count, though it's only used in vtterm_cnungrab. 2011 */ 2012 static int 2013 vtterm_cngrab_noswitch(struct vt_device *vd, struct vt_window *vw) 2014 { 2015 keyboard_t *kbd; 2016 2017 if (vw->vw_grabbed++ > 0) 2018 return (vw->vw_grabbed); 2019 2020 if ((kbd = vd->vd_keyboard) == NULL) 2021 return (1); 2022 2023 /* 2024 * Make sure the keyboard is accessible even when the kbd device 2025 * driver is disabled. 2026 */ 2027 kbdd_enable(kbd); 2028 2029 /* We shall always use the keyboard in the XLATE mode here. */ 2030 vw->vw_prev_kbdmode = vw->vw_kbdmode; 2031 vw->vw_kbdmode = K_XLATE; 2032 vt_update_kbd_mode(vw, kbd); 2033 2034 kbdd_poll(kbd, TRUE); 2035 return (1); 2036 } 2037 2038 static int 2039 vtterm_cnungrab_noswitch(struct vt_device *vd, struct vt_window *vw) 2040 { 2041 keyboard_t *kbd; 2042 2043 if (--vw->vw_grabbed > 0) 2044 return (vw->vw_grabbed); 2045 2046 if ((kbd = vd->vd_keyboard) == NULL) 2047 return (0); 2048 2049 kbdd_poll(kbd, FALSE); 2050 2051 vw->vw_kbdmode = vw->vw_prev_kbdmode; 2052 vt_update_kbd_mode(vw, kbd); 2053 kbdd_disable(kbd); 2054 return (0); 2055 } 2056 2057 static void 2058 vtterm_cngrab(struct terminal *tm) 2059 { 2060 struct vt_device *vd; 2061 struct vt_window *vw; 2062 2063 vw = tm->tm_softc; 2064 vd = vw->vw_device; 2065 2066 /* To be restored after we ungrab. */ 2067 if (vd->vd_grabwindow == NULL) 2068 vd->vd_grabwindow = vd->vd_curwindow; 2069 2070 if (!cold) 2071 vt_window_switch(vw); 2072 2073 vtterm_cngrab_noswitch(vd, vw); 2074 } 2075 2076 static void 2077 vtterm_cnungrab(struct terminal *tm) 2078 { 2079 struct vt_device *vd; 2080 struct vt_window *vw, *grabwindow; 2081 2082 vw = tm->tm_softc; 2083 vd = vw->vw_device; 2084 2085 MPASS(vd->vd_grabwindow != NULL); 2086 if (vtterm_cnungrab_noswitch(vd, vw) != 0) 2087 return; 2088 2089 /* 2090 * We set `vd_grabwindow` to NULL before calling vt_window_switch() 2091 * because it allows the underlying vt(4) backend to distinguish a 2092 * "grab" from an "ungrab" of the console. 2093 * 2094 * This is used by `vt_drmfb` in drm-kmod to call either 2095 * fb_debug_enter() or fb_debug_leave() appropriately. 2096 */ 2097 grabwindow = vd->vd_grabwindow; 2098 vd->vd_grabwindow = NULL; 2099 2100 if (!cold) 2101 vt_window_switch(grabwindow); 2102 } 2103 2104 static void 2105 vtterm_opened(struct terminal *tm, int opened) 2106 { 2107 struct vt_window *vw = tm->tm_softc; 2108 struct vt_device *vd = vw->vw_device; 2109 2110 VT_LOCK(vd); 2111 vd->vd_flags &= ~VDF_SPLASH; 2112 if (opened) 2113 vw->vw_flags |= VWF_OPENED; 2114 else { 2115 vw->vw_flags &= ~VWF_OPENED; 2116 /* TODO: finish ACQ/REL */ 2117 } 2118 VT_UNLOCK(vd); 2119 } 2120 2121 static int 2122 vt_change_font(struct vt_window *vw, struct vt_font *vf) 2123 { 2124 struct vt_device *vd = vw->vw_device; 2125 struct terminal *tm = vw->vw_terminal; 2126 term_pos_t size; 2127 struct winsize wsz; 2128 2129 /* 2130 * Changing fonts. 2131 * 2132 * Changing fonts is a little tricky. We must prevent 2133 * simultaneous access to the device, so we must stop 2134 * the display timer and the terminal from accessing. 2135 * We need to switch fonts and grow our screen buffer. 2136 * 2137 * XXX: Right now the code uses terminal_mute() to 2138 * prevent data from reaching the console driver while 2139 * resizing the screen buffer. This isn't elegant... 2140 */ 2141 2142 VT_LOCK(vd); 2143 if (vw->vw_flags & VWF_BUSY) { 2144 /* Another process is changing the font. */ 2145 VT_UNLOCK(vd); 2146 return (EBUSY); 2147 } 2148 vw->vw_flags |= VWF_BUSY; 2149 VT_UNLOCK(vd); 2150 2151 vt_termsize(vd, vf, &size); 2152 vt_winsize(vd, vf, &wsz); 2153 2154 /* Grow the screen buffer and terminal. */ 2155 terminal_mute(tm, 1); 2156 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size); 2157 terminal_set_winsize_blank(tm, &wsz, 0, NULL); 2158 terminal_set_cursor(tm, &vw->vw_buf.vb_cursor); 2159 terminal_mute(tm, 0); 2160 2161 /* Actually apply the font to the current window. */ 2162 VT_LOCK(vd); 2163 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) { 2164 /* 2165 * In case vt_change_font called to update size we don't need 2166 * to update font link. 2167 */ 2168 vtfont_unref(vw->vw_font); 2169 vw->vw_font = vtfont_ref(vf); 2170 } 2171 2172 /* 2173 * Compute the drawable area and move the mouse cursor inside 2174 * it, in case the new area is smaller than the previous one. 2175 */ 2176 vt_compute_drawable_area(vw); 2177 vd->vd_mx = min(vd->vd_mx, 2178 vw->vw_draw_area.tr_end.tp_col - 2179 vw->vw_draw_area.tr_begin.tp_col - 1); 2180 vd->vd_my = min(vd->vd_my, 2181 vw->vw_draw_area.tr_end.tp_row - 2182 vw->vw_draw_area.tr_begin.tp_row - 1); 2183 2184 /* Force a full redraw the next timer tick. */ 2185 if (vd->vd_curwindow == vw) { 2186 vd->vd_flags |= VDF_INVALID; 2187 vt_resume_flush_timer(vw, 0); 2188 } 2189 vw->vw_flags &= ~VWF_BUSY; 2190 VT_UNLOCK(vd); 2191 return (0); 2192 } 2193 2194 static int 2195 vt_proc_alive(struct vt_window *vw) 2196 { 2197 struct proc *p; 2198 2199 if (vw->vw_smode.mode != VT_PROCESS) 2200 return (FALSE); 2201 2202 if (vw->vw_proc) { 2203 if ((p = pfind(vw->vw_pid)) != NULL) 2204 PROC_UNLOCK(p); 2205 if (vw->vw_proc == p) 2206 return (TRUE); 2207 vw->vw_proc = NULL; 2208 vw->vw_smode.mode = VT_AUTO; 2209 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid); 2210 vw->vw_pid = 0; 2211 } 2212 return (FALSE); 2213 } 2214 2215 static int 2216 signal_vt_rel(struct vt_window *vw) 2217 { 2218 2219 if (vw->vw_smode.mode != VT_PROCESS) 2220 return (FALSE); 2221 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) { 2222 vw->vw_proc = NULL; 2223 vw->vw_pid = 0; 2224 return (TRUE); 2225 } 2226 vw->vw_flags |= VWF_SWWAIT_REL; 2227 PROC_LOCK(vw->vw_proc); 2228 kern_psignal(vw->vw_proc, vw->vw_smode.relsig); 2229 PROC_UNLOCK(vw->vw_proc); 2230 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid); 2231 return (TRUE); 2232 } 2233 2234 static int 2235 signal_vt_acq(struct vt_window *vw) 2236 { 2237 2238 if (vw->vw_smode.mode != VT_PROCESS) 2239 return (FALSE); 2240 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 2241 cnavailable(vw->vw_terminal->consdev, FALSE); 2242 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) { 2243 vw->vw_proc = NULL; 2244 vw->vw_pid = 0; 2245 return (TRUE); 2246 } 2247 vw->vw_flags |= VWF_SWWAIT_ACQ; 2248 PROC_LOCK(vw->vw_proc); 2249 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig); 2250 PROC_UNLOCK(vw->vw_proc); 2251 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid); 2252 return (TRUE); 2253 } 2254 2255 static int 2256 finish_vt_rel(struct vt_window *vw, int release, int *s) 2257 { 2258 2259 if (vw->vw_flags & VWF_SWWAIT_REL) { 2260 vw->vw_flags &= ~VWF_SWWAIT_REL; 2261 if (release) { 2262 taskqueue_drain_timeout(taskqueue_thread, &vw->vw_timeout_task_dead); 2263 (void)vt_late_window_switch(vw->vw_switch_to); 2264 } 2265 return (0); 2266 } 2267 return (EINVAL); 2268 } 2269 2270 static int 2271 finish_vt_acq(struct vt_window *vw) 2272 { 2273 2274 if (vw->vw_flags & VWF_SWWAIT_ACQ) { 2275 vw->vw_flags &= ~VWF_SWWAIT_ACQ; 2276 return (0); 2277 } 2278 return (EINVAL); 2279 } 2280 2281 #ifndef SC_NO_CUTPASTE 2282 static void 2283 vt_mouse_terminput_button(struct vt_device *vd, int button) 2284 { 2285 struct vt_window *vw; 2286 struct vt_font *vf; 2287 char mouseb[6] = "\x1B[M"; 2288 int i, x, y; 2289 2290 vw = vd->vd_curwindow; 2291 vf = vw->vw_font; 2292 2293 /* Translate to char position. */ 2294 x = vd->vd_mx / vf->vf_width; 2295 y = vd->vd_my / vf->vf_height; 2296 /* Avoid overflow. */ 2297 x = MIN(x, 255 - '!'); 2298 y = MIN(y, 255 - '!'); 2299 2300 mouseb[3] = ' ' + button; 2301 mouseb[4] = '!' + x; 2302 mouseb[5] = '!' + y; 2303 2304 for (i = 0; i < sizeof(mouseb); i++) 2305 terminal_input_char(vw->vw_terminal, mouseb[i]); 2306 } 2307 2308 static void 2309 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event, 2310 int cnt) 2311 { 2312 2313 switch (type) { 2314 case MOUSE_BUTTON_EVENT: 2315 if (cnt > 0) { 2316 /* Mouse button pressed. */ 2317 if (event & MOUSE_BUTTON1DOWN) 2318 vt_mouse_terminput_button(vd, 0); 2319 if (event & MOUSE_BUTTON2DOWN) 2320 vt_mouse_terminput_button(vd, 1); 2321 if (event & MOUSE_BUTTON3DOWN) 2322 vt_mouse_terminput_button(vd, 2); 2323 } else { 2324 /* Mouse button released. */ 2325 vt_mouse_terminput_button(vd, 3); 2326 } 2327 break; 2328 #ifdef notyet 2329 case MOUSE_MOTION_EVENT: 2330 if (mouse->u.data.z < 0) { 2331 /* Scroll up. */ 2332 sc_mouse_input_button(vd, 64); 2333 } else if (mouse->u.data.z > 0) { 2334 /* Scroll down. */ 2335 sc_mouse_input_button(vd, 65); 2336 } 2337 break; 2338 #endif 2339 } 2340 } 2341 2342 static void 2343 vt_mouse_paste(void) 2344 { 2345 term_char_t *buf; 2346 int i, len; 2347 2348 len = VD_PASTEBUFLEN(main_vd); 2349 buf = VD_PASTEBUF(main_vd); 2350 len /= sizeof(term_char_t); 2351 for (i = 0; i < len; i++) { 2352 if (TCHAR_CHARACTER(buf[i]) == '\0') 2353 continue; 2354 terminal_input_char(main_vd->vd_curwindow->vw_terminal, 2355 buf[i]); 2356 } 2357 } 2358 2359 void 2360 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel) 2361 { 2362 struct vt_device *vd; 2363 struct vt_window *vw; 2364 struct vt_font *vf; 2365 term_pos_t size; 2366 int len, mark; 2367 2368 vd = main_vd; 2369 vw = vd->vd_curwindow; 2370 vf = vw->vw_font; 2371 2372 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS)) 2373 /* 2374 * Either the mouse is disabled, or the window is in 2375 * "graphics mode". The graphics mode is usually set by 2376 * an X server, using the KDSETMODE ioctl. 2377 */ 2378 return; 2379 2380 if (vf == NULL) /* Text mode. */ 2381 return; 2382 2383 /* 2384 * TODO: add flag about pointer position changed, to not redraw chars 2385 * under mouse pointer when nothing changed. 2386 */ 2387 2388 if (vw->vw_mouse_level > 0) 2389 vt_mouse_terminput(vd, type, x, y, event, cnt); 2390 2391 switch (type) { 2392 case MOUSE_ACTION: 2393 case MOUSE_MOTION_EVENT: 2394 /* Movement */ 2395 x += vd->vd_mx; 2396 y += vd->vd_my; 2397 2398 vt_termsize(vd, vf, &size); 2399 2400 /* Apply limits. */ 2401 x = MAX(x, 0); 2402 y = MAX(y, 0); 2403 x = MIN(x, (size.tp_col * vf->vf_width) - 1); 2404 y = MIN(y, (size.tp_row * vf->vf_height) - 1); 2405 2406 vd->vd_mx = x; 2407 vd->vd_my = y; 2408 if (vd->vd_mstate & (MOUSE_BUTTON1DOWN | VT_MOUSE_EXTENDBUTTON)) 2409 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE, 2410 vd->vd_mx / vf->vf_width, 2411 vd->vd_my / vf->vf_height); 2412 2413 vt_resume_flush_timer(vw, 0); 2414 return; /* Done */ 2415 case MOUSE_BUTTON_EVENT: 2416 /* Buttons */ 2417 break; 2418 default: 2419 return; /* Done */ 2420 } 2421 2422 switch (event) { 2423 case MOUSE_BUTTON1DOWN: 2424 switch (cnt % 4) { 2425 case 0: /* up */ 2426 mark = VTB_MARK_END; 2427 break; 2428 case 1: /* single click: start cut operation */ 2429 mark = VTB_MARK_START; 2430 break; 2431 case 2: /* double click: cut a word */ 2432 mark = VTB_MARK_WORD; 2433 break; 2434 default: /* triple click: cut a line */ 2435 mark = VTB_MARK_ROW; 2436 break; 2437 } 2438 break; 2439 case VT_MOUSE_PASTEBUTTON: 2440 switch (cnt) { 2441 case 0: /* up */ 2442 break; 2443 default: 2444 vt_mouse_paste(); 2445 /* clear paste buffer selection after paste */ 2446 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_START, 2447 vd->vd_mx / vf->vf_width, 2448 vd->vd_my / vf->vf_height); 2449 break; 2450 } 2451 return; /* Done */ 2452 case VT_MOUSE_EXTENDBUTTON: 2453 switch (cnt) { 2454 case 0: /* up */ 2455 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN)) 2456 mark = VTB_MARK_EXTEND; 2457 else 2458 mark = VTB_MARK_NONE; 2459 break; 2460 default: 2461 mark = VTB_MARK_EXTEND; 2462 break; 2463 } 2464 break; 2465 default: 2466 return; /* Done */ 2467 } 2468 2469 /* Save buttons state. */ 2470 if (cnt > 0) 2471 vd->vd_mstate |= event; 2472 else 2473 vd->vd_mstate &= ~event; 2474 2475 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width, 2476 vd->vd_my / vf->vf_height) == 1) { 2477 /* 2478 * We have something marked to copy, so update pointer to 2479 * window with selection. 2480 */ 2481 vt_resume_flush_timer(vw, 0); 2482 2483 switch (mark) { 2484 case VTB_MARK_END: 2485 case VTB_MARK_WORD: 2486 case VTB_MARK_ROW: 2487 case VTB_MARK_EXTEND: 2488 break; 2489 default: 2490 /* Other types of mark do not require to copy data. */ 2491 return; 2492 } 2493 2494 /* Get current selection size in bytes. */ 2495 len = vtbuf_get_marked_len(&vw->vw_buf); 2496 if (len <= 0) 2497 return; 2498 2499 /* Reallocate buffer only if old one is too small. */ 2500 if (len > VD_PASTEBUFSZ(vd)) { 2501 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT, 2502 M_WAITOK | M_ZERO); 2503 /* Update buffer size. */ 2504 VD_PASTEBUFSZ(vd) = len; 2505 } 2506 /* Request copy/paste buffer data, no more than `len' */ 2507 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd), len, mark); 2508 2509 VD_PASTEBUFLEN(vd) = len; 2510 2511 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */ 2512 } 2513 } 2514 2515 void 2516 vt_mouse_state(int show) 2517 { 2518 struct vt_device *vd; 2519 struct vt_window *vw; 2520 2521 vd = main_vd; 2522 vw = vd->vd_curwindow; 2523 2524 switch (show) { 2525 case VT_MOUSE_HIDE: 2526 vw->vw_flags |= VWF_MOUSE_HIDE; 2527 break; 2528 case VT_MOUSE_SHOW: 2529 vw->vw_flags &= ~VWF_MOUSE_HIDE; 2530 break; 2531 } 2532 2533 /* Mark mouse position as dirty. */ 2534 vt_mark_mouse_position_as_dirty(vd, false); 2535 vt_resume_flush_timer(vw, 0); 2536 } 2537 #endif 2538 2539 static int 2540 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr, 2541 int nprot, vm_memattr_t *memattr) 2542 { 2543 struct vt_window *vw = tm->tm_softc; 2544 struct vt_device *vd = vw->vw_device; 2545 2546 if (vd->vd_driver->vd_fb_mmap) 2547 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot, 2548 memattr)); 2549 2550 return (ENXIO); 2551 } 2552 2553 static int 2554 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, 2555 struct thread *td) 2556 { 2557 struct vt_window *vw = tm->tm_softc; 2558 struct vt_device *vd = vw->vw_device; 2559 keyboard_t *kbd; 2560 int error, i, s; 2561 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 2562 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 2563 int ival; 2564 2565 switch (cmd) { 2566 case _IO('v', 4): 2567 cmd = VT_RELDISP; 2568 break; 2569 case _IO('v', 5): 2570 cmd = VT_ACTIVATE; 2571 break; 2572 case _IO('v', 6): 2573 cmd = VT_WAITACTIVE; 2574 break; 2575 case _IO('K', 20): 2576 cmd = KDSKBSTATE; 2577 break; 2578 case _IO('K', 67): 2579 cmd = KDSETRAD; 2580 break; 2581 case _IO('K', 7): 2582 cmd = KDSKBMODE; 2583 break; 2584 case _IO('K', 8): 2585 cmd = KDMKTONE; 2586 break; 2587 case _IO('K', 10): 2588 cmd = KDSETMODE; 2589 break; 2590 case _IO('K', 13): 2591 cmd = KDSBORDER; 2592 break; 2593 case _IO('K', 63): 2594 cmd = KIOCSOUND; 2595 break; 2596 case _IO('K', 66): 2597 cmd = KDSETLED; 2598 break; 2599 case _IO('c', 104): 2600 cmd = CONS_SETWINORG; 2601 break; 2602 case _IO('c', 110): 2603 cmd = CONS_SETKBD; 2604 break; 2605 default: 2606 goto skip_thunk; 2607 } 2608 ival = IOCPARM_IVAL(data); 2609 data = (caddr_t)&ival; 2610 skip_thunk: 2611 #endif 2612 2613 switch (cmd) { 2614 case KDSETRAD: /* set keyboard repeat & delay rates (old) */ 2615 if (*(int *)data & ~0x7f) 2616 return (EINVAL); 2617 /* FALLTHROUGH */ 2618 case GIO_KEYMAP: 2619 case PIO_KEYMAP: 2620 case GIO_DEADKEYMAP: 2621 case PIO_DEADKEYMAP: 2622 #ifdef COMPAT_FREEBSD13 2623 case OGIO_DEADKEYMAP: 2624 case OPIO_DEADKEYMAP: 2625 #endif /* COMPAT_FREEBSD13 */ 2626 case GETFKEY: 2627 case SETFKEY: 2628 case KDGKBINFO: 2629 case KDGKBTYPE: 2630 case KDGETREPEAT: /* get keyboard repeat & delay rates */ 2631 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */ 2632 case KBADDKBD: /* add keyboard to mux */ 2633 case KBRELKBD: { /* release keyboard from mux */ 2634 error = 0; 2635 2636 mtx_lock(&Giant); 2637 if ((kbd = vd->vd_keyboard) != NULL) 2638 error = kbdd_ioctl(kbd, cmd, data); 2639 mtx_unlock(&Giant); 2640 if (error == ENOIOCTL) { 2641 if (cmd == KDGKBTYPE) { 2642 /* always return something? XXX */ 2643 *(int *)data = 0; 2644 } else { 2645 return (ENODEV); 2646 } 2647 } 2648 return (error); 2649 } 2650 case KDGKBSTATE: { /* get keyboard state (locks) */ 2651 error = 0; 2652 2653 if (vw == vd->vd_curwindow) { 2654 mtx_lock(&Giant); 2655 if ((kbd = vd->vd_keyboard) != NULL) 2656 error = vt_save_kbd_state(vw, kbd); 2657 mtx_unlock(&Giant); 2658 2659 if (error != 0) 2660 return (error); 2661 } 2662 2663 *(int *)data = vw->vw_kbdstate & LOCK_MASK; 2664 2665 return (error); 2666 } 2667 case KDSKBSTATE: { /* set keyboard state (locks) */ 2668 int state; 2669 2670 state = *(int *)data; 2671 if (state & ~LOCK_MASK) 2672 return (EINVAL); 2673 2674 vw->vw_kbdstate &= ~LOCK_MASK; 2675 vw->vw_kbdstate |= state; 2676 2677 error = 0; 2678 if (vw == vd->vd_curwindow) { 2679 mtx_lock(&Giant); 2680 if ((kbd = vd->vd_keyboard) != NULL) 2681 error = vt_update_kbd_state(vw, kbd); 2682 mtx_unlock(&Giant); 2683 } 2684 2685 return (error); 2686 } 2687 case KDGETLED: { /* get keyboard LED status */ 2688 error = 0; 2689 2690 if (vw == vd->vd_curwindow) { 2691 mtx_lock(&Giant); 2692 if ((kbd = vd->vd_keyboard) != NULL) 2693 error = vt_save_kbd_leds(vw, kbd); 2694 mtx_unlock(&Giant); 2695 2696 if (error != 0) 2697 return (error); 2698 } 2699 2700 *(int *)data = vw->vw_kbdstate & LED_MASK; 2701 2702 return (error); 2703 } 2704 case KDSETLED: { /* set keyboard LED status */ 2705 int leds; 2706 2707 leds = *(int *)data; 2708 if (leds & ~LED_MASK) 2709 return (EINVAL); 2710 2711 vw->vw_kbdstate &= ~LED_MASK; 2712 vw->vw_kbdstate |= leds; 2713 2714 error = 0; 2715 if (vw == vd->vd_curwindow) { 2716 mtx_lock(&Giant); 2717 if ((kbd = vd->vd_keyboard) != NULL) 2718 error = vt_update_kbd_leds(vw, kbd); 2719 mtx_unlock(&Giant); 2720 } 2721 2722 return (error); 2723 } 2724 case KDGETMODE: 2725 *(int *)data = (vw->vw_flags & VWF_GRAPHICS) ? 2726 KD_GRAPHICS : KD_TEXT; 2727 return (0); 2728 case KDGKBMODE: { 2729 error = 0; 2730 2731 if (vw == vd->vd_curwindow) { 2732 mtx_lock(&Giant); 2733 if ((kbd = vd->vd_keyboard) != NULL) 2734 error = vt_save_kbd_mode(vw, kbd); 2735 mtx_unlock(&Giant); 2736 2737 if (error != 0) 2738 return (error); 2739 } 2740 2741 *(int *)data = vw->vw_kbdmode; 2742 2743 return (error); 2744 } 2745 case KDSKBMODE: { 2746 int mode; 2747 2748 mode = *(int *)data; 2749 switch (mode) { 2750 case K_XLATE: 2751 case K_RAW: 2752 case K_CODE: 2753 vw->vw_kbdmode = mode; 2754 2755 error = 0; 2756 if (vw == vd->vd_curwindow) { 2757 mtx_lock(&Giant); 2758 if ((kbd = vd->vd_keyboard) != NULL) 2759 error = vt_update_kbd_mode(vw, kbd); 2760 mtx_unlock(&Giant); 2761 } 2762 2763 return (error); 2764 default: 2765 return (EINVAL); 2766 } 2767 } 2768 case FBIOGTYPE: 2769 case FBIO_GETWINORG: /* get frame buffer window origin */ 2770 case FBIO_GETDISPSTART: /* get display start address */ 2771 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */ 2772 case FBIO_BLANK: /* blank display */ 2773 case FBIO_GETRGBOFFS: /* get RGB offsets */ 2774 if (vd->vd_driver->vd_fb_ioctl) 2775 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td)); 2776 break; 2777 case CONS_BLANKTIME: 2778 /* XXX */ 2779 return (0); 2780 case CONS_HISTORY: 2781 if (*(int *)data < 0) 2782 return EINVAL; 2783 if (*(int *)data != vw->vw_buf.vb_history_size) 2784 vtbuf_sethistory_size(&vw->vw_buf, *(int *)data); 2785 return (0); 2786 case CONS_CLRHIST: 2787 vtbuf_clearhistory(&vw->vw_buf); 2788 /* 2789 * Invalidate the entire visible window; it is not guaranteed 2790 * that this operation will be immediately followed by a scroll 2791 * event, so it would otherwise be possible for prior artifacts 2792 * to remain visible. 2793 */ 2794 VT_LOCK(vd); 2795 if (vw == vd->vd_curwindow) { 2796 vd->vd_flags |= VDF_INVALID; 2797 vt_resume_flush_timer(vw, 0); 2798 } 2799 VT_UNLOCK(vd); 2800 return (0); 2801 case CONS_GET: 2802 /* XXX */ 2803 *(int *)data = M_CG640x480; 2804 return (0); 2805 case CONS_BELLTYPE: /* set bell type sound */ 2806 if ((*(int *)data) & CONS_QUIET_BELL) 2807 vd->vd_flags |= VDF_QUIET_BELL; 2808 else 2809 vd->vd_flags &= ~VDF_QUIET_BELL; 2810 return (0); 2811 case CONS_GETINFO: { 2812 vid_info_t *vi = (vid_info_t *)data; 2813 if (vi->size != sizeof(struct vid_info)) 2814 return (EINVAL); 2815 2816 if (vw == vd->vd_curwindow) { 2817 mtx_lock(&Giant); 2818 if ((kbd = vd->vd_keyboard) != NULL) 2819 vt_save_kbd_state(vw, kbd); 2820 mtx_unlock(&Giant); 2821 } 2822 2823 vi->m_num = vd->vd_curwindow->vw_number + 1; 2824 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK; 2825 /* XXX: other fields! */ 2826 return (0); 2827 } 2828 case CONS_GETVERS: 2829 *(int *)data = 0x200; 2830 return (0); 2831 case CONS_MODEINFO: 2832 /* XXX */ 2833 return (0); 2834 case CONS_MOUSECTL: { 2835 mouse_info_t *mouse = (mouse_info_t*)data; 2836 2837 /* 2838 * All the commands except MOUSE_SHOW nd MOUSE_HIDE 2839 * should not be applied to individual TTYs, but only to 2840 * consolectl. 2841 */ 2842 switch (mouse->operation) { 2843 case MOUSE_HIDE: 2844 if (vd->vd_flags & VDF_MOUSECURSOR) { 2845 vd->vd_flags &= ~VDF_MOUSECURSOR; 2846 #ifndef SC_NO_CUTPASTE 2847 vt_mouse_state(VT_MOUSE_HIDE); 2848 #endif 2849 } 2850 return (0); 2851 case MOUSE_SHOW: 2852 if (!(vd->vd_flags & VDF_MOUSECURSOR)) { 2853 vd->vd_flags |= VDF_MOUSECURSOR; 2854 vd->vd_mx = vd->vd_width / 2; 2855 vd->vd_my = vd->vd_height / 2; 2856 #ifndef SC_NO_CUTPASTE 2857 vt_mouse_state(VT_MOUSE_SHOW); 2858 #endif 2859 } 2860 return (0); 2861 default: 2862 return (EINVAL); 2863 } 2864 } 2865 case PIO_VFONT: { 2866 struct vt_font *vf; 2867 2868 if (vd->vd_flags & VDF_TEXTMODE) 2869 return (ENOTSUP); 2870 2871 error = vtfont_load((void *)data, &vf); 2872 if (error != 0) 2873 return (error); 2874 2875 error = vt_change_font(vw, vf); 2876 vtfont_unref(vf); 2877 return (error); 2878 } 2879 case PIO_VFONT_DEFAULT: { 2880 /* Reset to default font. */ 2881 error = vt_change_font(vw, vt_font_assigned); 2882 return (error); 2883 } 2884 case GIO_SCRNMAP: { 2885 scrmap_t *sm = (scrmap_t *)data; 2886 2887 /* We don't have screen maps, so return a handcrafted one. */ 2888 for (i = 0; i < 256; i++) 2889 sm->scrmap[i] = i; 2890 return (0); 2891 } 2892 case KDSETMODE: 2893 /* 2894 * FIXME: This implementation is incomplete compared to 2895 * syscons. 2896 */ 2897 switch (*(int *)data) { 2898 case KD_TEXT: 2899 case KD_TEXT1: 2900 case KD_PIXEL: 2901 vw->vw_flags &= ~VWF_GRAPHICS; 2902 break; 2903 case KD_GRAPHICS: 2904 vw->vw_flags |= VWF_GRAPHICS; 2905 break; 2906 } 2907 return (0); 2908 case KDENABIO: /* allow io operations */ 2909 error = priv_check(td, PRIV_IO); 2910 if (error != 0) 2911 return (error); 2912 error = securelevel_gt(td->td_ucred, 0); 2913 if (error != 0) 2914 return (error); 2915 #if defined(__i386__) 2916 td->td_frame->tf_eflags |= PSL_IOPL; 2917 #elif defined(__amd64__) 2918 td->td_frame->tf_rflags |= PSL_IOPL; 2919 #endif 2920 return (0); 2921 case KDDISABIO: /* disallow io operations (default) */ 2922 #if defined(__i386__) 2923 td->td_frame->tf_eflags &= ~PSL_IOPL; 2924 #elif defined(__amd64__) 2925 td->td_frame->tf_rflags &= ~PSL_IOPL; 2926 #endif 2927 return (0); 2928 case KDMKTONE: /* sound the bell */ 2929 vtterm_beep(tm, *(u_int *)data); 2930 return (0); 2931 case KIOCSOUND: /* make tone (*data) hz */ 2932 /* TODO */ 2933 return (0); 2934 case CONS_SETKBD: /* set the new keyboard */ 2935 mtx_lock(&Giant); 2936 error = 0; 2937 if (vd->vd_keyboard == NULL || 2938 vd->vd_keyboard->kb_index != *(int *)data) { 2939 kbd = kbd_get_keyboard(*(int *)data); 2940 if (kbd == NULL) { 2941 mtx_unlock(&Giant); 2942 return (EINVAL); 2943 } 2944 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, 2945 (void *)vd, vt_kbdevent, vd); 2946 if (i >= 0) { 2947 if ((kbd = vd->vd_keyboard) != NULL) { 2948 vt_save_kbd_state(vd->vd_curwindow, kbd); 2949 kbd_release(kbd, (void *)vd); 2950 } 2951 kbd = vd->vd_keyboard = kbd_get_keyboard(i); 2952 2953 vt_update_kbd_mode(vd->vd_curwindow, kbd); 2954 vt_update_kbd_state(vd->vd_curwindow, kbd); 2955 } else { 2956 error = EPERM; /* XXX */ 2957 } 2958 } 2959 mtx_unlock(&Giant); 2960 return (error); 2961 case CONS_RELKBD: /* release the current keyboard */ 2962 mtx_lock(&Giant); 2963 error = 0; 2964 if ((kbd = vd->vd_keyboard) != NULL) { 2965 vt_save_kbd_state(vd->vd_curwindow, kbd); 2966 error = kbd_release(kbd, (void *)vd); 2967 if (error == 0) { 2968 vd->vd_keyboard = NULL; 2969 } 2970 } 2971 mtx_unlock(&Giant); 2972 return (error); 2973 case VT_ACTIVATE: { 2974 int win; 2975 win = *(int *)data - 1; 2976 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME, 2977 VT_UNIT(vw), win); 2978 if ((win >= VT_MAXWINDOWS) || (win < 0)) 2979 return (EINVAL); 2980 return (vt_proc_window_switch(vd->vd_windows[win])); 2981 } 2982 case VT_GETACTIVE: 2983 *(int *)data = vd->vd_curwindow->vw_number + 1; 2984 return (0); 2985 case VT_GETINDEX: 2986 *(int *)data = vw->vw_number + 1; 2987 return (0); 2988 case VT_LOCKSWITCH: 2989 /* TODO: Check current state, switching can be in progress. */ 2990 if ((*(int *)data) == 0x01) 2991 vw->vw_flags |= VWF_VTYLOCK; 2992 else if ((*(int *)data) == 0x02) 2993 vw->vw_flags &= ~VWF_VTYLOCK; 2994 else 2995 return (EINVAL); 2996 return (0); 2997 case VT_OPENQRY: 2998 VT_LOCK(vd); 2999 for (i = 0; i < VT_MAXWINDOWS; i++) { 3000 vw = vd->vd_windows[i]; 3001 if (vw == NULL) 3002 continue; 3003 if (!(vw->vw_flags & VWF_OPENED)) { 3004 *(int *)data = vw->vw_number + 1; 3005 VT_UNLOCK(vd); 3006 return (0); 3007 } 3008 } 3009 VT_UNLOCK(vd); 3010 return (EINVAL); 3011 case VT_WAITACTIVE: { 3012 unsigned int idx; 3013 3014 error = 0; 3015 3016 idx = *(unsigned int *)data; 3017 if (idx > VT_MAXWINDOWS) 3018 return (EINVAL); 3019 if (idx > 0) 3020 vw = vd->vd_windows[idx - 1]; 3021 3022 VT_LOCK(vd); 3023 while (vd->vd_curwindow != vw && error == 0) 3024 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock); 3025 VT_UNLOCK(vd); 3026 return (error); 3027 } 3028 case VT_SETMODE: { /* set screen switcher mode */ 3029 struct vt_mode *mode; 3030 struct proc *p1; 3031 3032 mode = (struct vt_mode *)data; 3033 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw)); 3034 if (vw->vw_smode.mode == VT_PROCESS) { 3035 p1 = pfind(vw->vw_pid); 3036 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) { 3037 if (p1) 3038 PROC_UNLOCK(p1); 3039 DPRINTF(5, "error EPERM\n"); 3040 return (EPERM); 3041 } 3042 if (p1) 3043 PROC_UNLOCK(p1); 3044 } 3045 if (mode->mode == VT_AUTO) { 3046 vw->vw_smode.mode = VT_AUTO; 3047 vw->vw_proc = NULL; 3048 vw->vw_pid = 0; 3049 DPRINTF(5, "VT_AUTO, "); 3050 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 3051 cnavailable(vw->vw_terminal->consdev, TRUE); 3052 /* were we in the middle of the vty switching process? */ 3053 if (finish_vt_rel(vw, TRUE, &s) == 0) 3054 DPRINTF(5, "reset WAIT_REL, "); 3055 if (finish_vt_acq(vw) == 0) 3056 DPRINTF(5, "reset WAIT_ACQ, "); 3057 return (0); 3058 } else if (mode->mode == VT_PROCESS) { 3059 if (!ISSIGVALID(mode->relsig) || 3060 !ISSIGVALID(mode->acqsig) || 3061 !ISSIGVALID(mode->frsig)) { 3062 DPRINTF(5, "error EINVAL\n"); 3063 return (EINVAL); 3064 } 3065 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid); 3066 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode)); 3067 vw->vw_proc = td->td_proc; 3068 vw->vw_pid = vw->vw_proc->p_pid; 3069 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 3070 cnavailable(vw->vw_terminal->consdev, FALSE); 3071 } else { 3072 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n", 3073 mode->mode); 3074 return (EINVAL); 3075 } 3076 DPRINTF(5, "\n"); 3077 return (0); 3078 } 3079 case VT_GETMODE: /* get screen switcher mode */ 3080 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode)); 3081 return (0); 3082 3083 case VT_RELDISP: /* screen switcher ioctl */ 3084 /* 3085 * This must be the current vty which is in the VT_PROCESS 3086 * switching mode... 3087 */ 3088 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode != 3089 VT_PROCESS)) { 3090 return (EINVAL); 3091 } 3092 /* ...and this process is controlling it. */ 3093 if (vw->vw_proc != td->td_proc) { 3094 return (EPERM); 3095 } 3096 error = EINVAL; 3097 switch(*(int *)data) { 3098 case VT_FALSE: /* user refuses to release screen, abort */ 3099 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0) 3100 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n", 3101 SC_DRIVER_NAME, VT_UNIT(vw)); 3102 break; 3103 case VT_TRUE: /* user has released screen, go on */ 3104 /* finish_vt_rel(..., TRUE, ...) should not be locked */ 3105 if (vw->vw_flags & VWF_SWWAIT_REL) { 3106 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0) 3107 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n", 3108 SC_DRIVER_NAME, VT_UNIT(vw)); 3109 } else { 3110 error = EINVAL; 3111 } 3112 return (error); 3113 case VT_ACKACQ: /* acquire acknowledged, switch completed */ 3114 if ((error = finish_vt_acq(vw)) == 0) 3115 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n", 3116 SC_DRIVER_NAME, VT_UNIT(vw)); 3117 break; 3118 default: 3119 break; 3120 } 3121 return (error); 3122 } 3123 3124 return (ENOIOCTL); 3125 } 3126 3127 static struct vt_window * 3128 vt_allocate_window(struct vt_device *vd, unsigned int window) 3129 { 3130 struct vt_window *vw; 3131 struct terminal *tm; 3132 term_pos_t size; 3133 struct winsize wsz; 3134 3135 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO); 3136 vw->vw_device = vd; 3137 vw->vw_number = window; 3138 vw->vw_kbdmode = K_XLATE; 3139 3140 if ((vd->vd_flags & VDF_TEXTMODE) == 0) { 3141 vw->vw_font = vtfont_ref(vt_font_assigned); 3142 vt_compute_drawable_area(vw); 3143 } 3144 3145 vt_termsize(vd, vw->vw_font, &size); 3146 vt_winsize(vd, vw->vw_font, &wsz); 3147 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw); 3148 vw->vw_buf.vb_terminal = tm; /* must be set before vtbuf_init() */ 3149 vtbuf_init(&vw->vw_buf, &size); 3150 3151 terminal_set_winsize(tm, &wsz); 3152 vd->vd_windows[window] = vw; 3153 TIMEOUT_TASK_INIT(taskqueue_thread, &vw->vw_timeout_task_dead, 0, &vt_switch_timer, vw); 3154 3155 return (vw); 3156 } 3157 3158 void 3159 vt_upgrade(struct vt_device *vd) 3160 { 3161 struct vt_window *vw; 3162 unsigned int i; 3163 int register_handlers; 3164 3165 if (!vty_enabled(VTY_VT)) 3166 return; 3167 if (main_vd->vd_driver == NULL) 3168 return; 3169 3170 for (i = 0; i < VT_MAXWINDOWS; i++) { 3171 vw = vd->vd_windows[i]; 3172 if (vw == NULL) { 3173 /* New window. */ 3174 vw = vt_allocate_window(vd, i); 3175 } 3176 if (!(vw->vw_flags & VWF_READY)) { 3177 TIMEOUT_TASK_INIT(taskqueue_thread, &vw->vw_timeout_task_dead, 0, &vt_switch_timer, vw); 3178 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw)); 3179 vw->vw_flags |= VWF_READY; 3180 if (vw->vw_flags & VWF_CONSOLE) { 3181 /* For existing console window. */ 3182 EVENTHANDLER_REGISTER(shutdown_pre_sync, 3183 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT); 3184 } 3185 } 3186 } 3187 VT_LOCK(vd); 3188 if (vd->vd_curwindow == NULL) 3189 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW]; 3190 3191 register_handlers = 0; 3192 if (!(vd->vd_flags & VDF_ASYNC)) { 3193 /* Attach keyboard. */ 3194 vt_allocate_keyboard(vd); 3195 3196 /* Init 25 Hz timer. */ 3197 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0); 3198 3199 /* 3200 * Start timer when everything ready. 3201 * Note that the operations here are purposefully ordered. 3202 * We need to ensure vd_timer_armed is non-zero before we set 3203 * the VDF_ASYNC flag. That prevents this function from 3204 * racing with vt_resume_flush_timer() to update the 3205 * callout structure. 3206 */ 3207 atomic_add_acq_int(&vd->vd_timer_armed, 1); 3208 vd->vd_flags |= VDF_ASYNC; 3209 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd); 3210 register_handlers = 1; 3211 } 3212 3213 VT_UNLOCK(vd); 3214 3215 /* Refill settings with new sizes. */ 3216 vt_resize(vd); 3217 3218 if (register_handlers) { 3219 /* Register suspend/resume handlers. */ 3220 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler, 3221 vd, EVENTHANDLER_PRI_ANY); 3222 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd, 3223 EVENTHANDLER_PRI_ANY); 3224 } 3225 } 3226 3227 static void 3228 vt_resize(struct vt_device *vd) 3229 { 3230 struct vt_window *vw; 3231 int i; 3232 3233 for (i = 0; i < VT_MAXWINDOWS; i++) { 3234 vw = vd->vd_windows[i]; 3235 VT_LOCK(vd); 3236 /* Assign default font to window, if not textmode. */ 3237 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL) 3238 vw->vw_font = vtfont_ref(vt_font_assigned); 3239 VT_UNLOCK(vd); 3240 3241 /* Resize terminal windows */ 3242 while (vt_change_font(vw, vw->vw_font) == EBUSY) { 3243 DPRINTF(100, "%s: vt_change_font() is busy, " 3244 "window %d\n", __func__, i); 3245 } 3246 } 3247 } 3248 3249 static void 3250 vt_replace_backend(const struct vt_driver *drv, void *softc) 3251 { 3252 struct vt_device *vd; 3253 3254 vd = main_vd; 3255 3256 if (vd->vd_flags & VDF_ASYNC) { 3257 /* Stop vt_flush periodic task. */ 3258 VT_LOCK(vd); 3259 vt_suspend_flush_timer(vd); 3260 VT_UNLOCK(vd); 3261 /* 3262 * Mute current terminal until we done. vt_change_font (called 3263 * from vt_resize) will unmute it. 3264 */ 3265 terminal_mute(vd->vd_curwindow->vw_terminal, 1); 3266 } 3267 3268 /* 3269 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will 3270 * set it. 3271 */ 3272 VT_LOCK(vd); 3273 vd->vd_flags &= ~VDF_TEXTMODE; 3274 3275 if (drv != NULL) { 3276 /* 3277 * We want to upgrade from the current driver to the 3278 * given driver. 3279 */ 3280 3281 vd->vd_prev_driver = vd->vd_driver; 3282 vd->vd_prev_softc = vd->vd_softc; 3283 vd->vd_driver = drv; 3284 vd->vd_softc = softc; 3285 3286 vd->vd_driver->vd_init(vd); 3287 } else if (vd->vd_prev_driver != NULL && vd->vd_prev_softc != NULL) { 3288 /* 3289 * No driver given: we want to downgrade to the previous 3290 * driver. 3291 */ 3292 const struct vt_driver *old_drv; 3293 void *old_softc; 3294 3295 old_drv = vd->vd_driver; 3296 old_softc = vd->vd_softc; 3297 3298 vd->vd_driver = vd->vd_prev_driver; 3299 vd->vd_softc = vd->vd_prev_softc; 3300 vd->vd_prev_driver = NULL; 3301 vd->vd_prev_softc = NULL; 3302 3303 vd->vd_flags |= VDF_DOWNGRADE; 3304 3305 vd->vd_driver->vd_init(vd); 3306 3307 if (old_drv->vd_fini) 3308 old_drv->vd_fini(vd, old_softc); 3309 3310 vd->vd_flags &= ~VDF_DOWNGRADE; 3311 } 3312 3313 VT_UNLOCK(vd); 3314 3315 /* Update windows sizes and initialize last items. */ 3316 vt_upgrade(vd); 3317 3318 /* 3319 * Give a chance to the new backend to run the post-switch code, for 3320 * instance to refresh the screen. 3321 */ 3322 if (vd->vd_driver->vd_postswitch) 3323 vd->vd_driver->vd_postswitch(vd); 3324 3325 #ifdef DEV_SPLASH 3326 if (vd->vd_flags & VDF_SPLASH) 3327 vtterm_splash(vd); 3328 #endif 3329 3330 if (vd->vd_flags & VDF_ASYNC) { 3331 /* Allow to put chars now. */ 3332 terminal_mute(vd->vd_curwindow->vw_terminal, 0); 3333 /* Rerun timer for screen updates. */ 3334 vt_resume_flush_timer(vd->vd_curwindow, 0); 3335 } 3336 3337 /* 3338 * Register as console. If it already registered, cnadd() will ignore 3339 * it. 3340 */ 3341 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal); 3342 } 3343 3344 static void 3345 vt_suspend_handler(void *priv) 3346 { 3347 struct vt_device *vd; 3348 3349 vd = priv; 3350 vd->vd_flags |= VDF_SUSPENDED; 3351 if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL) 3352 vd->vd_driver->vd_suspend(vd); 3353 } 3354 3355 static void 3356 vt_resume_handler(void *priv) 3357 { 3358 struct vt_device *vd; 3359 3360 vd = priv; 3361 if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL) 3362 vd->vd_driver->vd_resume(vd); 3363 vd->vd_flags &= ~VDF_SUSPENDED; 3364 } 3365 3366 int 3367 vt_allocate(const struct vt_driver *drv, void *softc) 3368 { 3369 3370 if (!vty_enabled(VTY_VT)) 3371 return (EINVAL); 3372 3373 if (main_vd->vd_driver == NULL) { 3374 main_vd->vd_driver = drv; 3375 printf("VT: initialize with new VT driver \"%s\".\n", 3376 drv->vd_name); 3377 } else { 3378 /* 3379 * Check if have rights to replace current driver. For example: 3380 * it is bad idea to replace KMS driver with generic VGA one. 3381 */ 3382 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) { 3383 printf("VT: Driver priority %d too low. Current %d\n ", 3384 drv->vd_priority, main_vd->vd_driver->vd_priority); 3385 return (EEXIST); 3386 } 3387 printf("VT: Replacing driver \"%s\" with new \"%s\".\n", 3388 main_vd->vd_driver->vd_name, drv->vd_name); 3389 } 3390 3391 vt_replace_backend(drv, softc); 3392 3393 return (0); 3394 } 3395 3396 int 3397 vt_deallocate(const struct vt_driver *drv, void *softc) 3398 { 3399 3400 if (!vty_enabled(VTY_VT)) 3401 return (EINVAL); 3402 3403 if (main_vd->vd_prev_driver == NULL || 3404 main_vd->vd_driver != drv || 3405 main_vd->vd_softc != softc) 3406 return (EPERM); 3407 3408 printf("VT: Switching back from \"%s\" to \"%s\".\n", 3409 main_vd->vd_driver->vd_name, main_vd->vd_prev_driver->vd_name); 3410 3411 vt_replace_backend(NULL, NULL); 3412 3413 return (0); 3414 } 3415 3416 void 3417 vt_suspend(struct vt_device *vd) 3418 { 3419 int error; 3420 3421 if (vt_suspendswitch == 0) 3422 return; 3423 /* Save current window. */ 3424 vd->vd_savedwindow = vd->vd_curwindow; 3425 /* Ask holding process to free window and switch to console window */ 3426 vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]); 3427 3428 /* Wait for the window switch to complete. */ 3429 error = 0; 3430 VT_LOCK(vd); 3431 while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0) 3432 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock); 3433 VT_UNLOCK(vd); 3434 } 3435 3436 void 3437 vt_resume(struct vt_device *vd) 3438 { 3439 3440 if (vt_suspendswitch == 0) 3441 return; 3442 /* Switch back to saved window, if any */ 3443 vt_proc_window_switch(vd->vd_savedwindow); 3444 vd->vd_savedwindow = NULL; 3445 } 3446