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