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