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