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