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 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
808 return (1);
809 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
810 /* Suspend machine. */
811 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
812 return (1);
813 }
814
815 return (0);
816 }
817
818 static void
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 FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
956 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
957 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
958 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
959 /* F1 through F12 keys. */
960 terminal_input_special(vw->vw_terminal,
961 TKEY_F1 + c - (FKEY | F(1)));
962 break;
963 case FKEY | F(49): /* Home key. */
964 terminal_input_special(vw->vw_terminal, TKEY_HOME);
965 break;
966 case FKEY | F(50): /* Arrow up. */
967 terminal_input_special(vw->vw_terminal, TKEY_UP);
968 break;
969 case FKEY | F(51): /* Page up. */
970 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
971 break;
972 case FKEY | F(53): /* Arrow left. */
973 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
974 break;
975 case FKEY | F(55): /* Arrow right. */
976 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
977 break;
978 case FKEY | F(57): /* End key. */
979 terminal_input_special(vw->vw_terminal, TKEY_END);
980 break;
981 case FKEY | F(58): /* Arrow down. */
982 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
983 break;
984 case FKEY | F(59): /* Page down. */
985 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
986 break;
987 case FKEY | F(60): /* Insert key. */
988 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
989 break;
990 case FKEY | F(61): /* Delete key. */
991 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
992 break;
993 }
994 } else if (KEYFLAGS(c) == 0) {
995 /* Don't do UTF-8 conversion when doing raw mode. */
996 if (vw->vw_kbdmode == K_XLATE) {
997 #if VT_ALT_TO_ESC_HACK
998 if (vd->vd_kbstate & ALKED) {
999 /*
1000 * Prepend ESC sequence if one of ALT keys down.
1001 */
1002 terminal_input_char(vw->vw_terminal, 0x1b);
1003 }
1004 #endif
1005 #if defined(KDB)
1006 kdb_alt_break(c, &vd->vd_altbrk);
1007 #endif
1008 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
1009 } else
1010 terminal_input_raw(vw->vw_terminal, c);
1011 }
1012 return (0);
1013 }
1014
1015 static int
vt_kbdevent(keyboard_t * kbd,int event,void * arg)1016 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
1017 {
1018 struct vt_device *vd = arg;
1019 int c;
1020
1021 switch (event) {
1022 case KBDIO_KEYINPUT:
1023 break;
1024 case KBDIO_UNLOADING:
1025 mtx_lock(&Giant);
1026 vd->vd_keyboard = NULL;
1027 kbd_release(kbd, (void *)vd);
1028 mtx_unlock(&Giant);
1029 return (0);
1030 default:
1031 return (EINVAL);
1032 }
1033
1034 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
1035 vt_processkey(kbd, vd, c);
1036
1037 return (0);
1038 }
1039
1040 static int
vt_allocate_keyboard(struct vt_device * vd)1041 vt_allocate_keyboard(struct vt_device *vd)
1042 {
1043 int grabbed, i, idx0, idx;
1044 keyboard_t *k0, *k;
1045 keyboard_info_t ki;
1046
1047 /*
1048 * If vt_upgrade() happens while the console is grabbed, we are
1049 * potentially going to switch keyboard devices while the keyboard is in
1050 * use. Unwind the grabbing of the current keyboard first, then we will
1051 * re-grab the new keyboard below, before we return.
1052 */
1053 if (vd->vd_curwindow == &vt_conswindow) {
1054 grabbed = vd->vd_curwindow->vw_grabbed;
1055 for (i = 0; i < grabbed; ++i)
1056 vtterm_cnungrab_noswitch(vd, vd->vd_curwindow);
1057 }
1058
1059 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
1060 if (idx0 >= 0) {
1061 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
1062 k0 = kbd_get_keyboard(idx0);
1063
1064 for (idx = kbd_find_keyboard2("*", -1, 0);
1065 idx != -1;
1066 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
1067 k = kbd_get_keyboard(idx);
1068
1069 if (idx == idx0 || KBD_IS_BUSY(k))
1070 continue;
1071
1072 bzero(&ki, sizeof(ki));
1073 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
1074 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
1075 ki.kb_unit = k->kb_unit;
1076
1077 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
1078 }
1079 } else {
1080 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
1081 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
1082 if (idx0 < 0) {
1083 DPRINTF(10, "%s: No keyboard found.\n", __func__);
1084 return (-1);
1085 }
1086 k0 = kbd_get_keyboard(idx0);
1087 }
1088 vd->vd_keyboard = k0;
1089 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__,
1090 vd->vd_keyboard->kb_index);
1091
1092 if (vd->vd_curwindow == &vt_conswindow) {
1093 for (i = 0; i < grabbed; ++i)
1094 vtterm_cngrab_noswitch(vd, vd->vd_curwindow);
1095 }
1096
1097 return (idx0);
1098 }
1099
1100 #define DEVCTL_LEN 64
1101 static void
vtterm_devctl(bool enabled,bool hushed,int hz,sbintime_t duration)1102 vtterm_devctl(bool enabled, bool hushed, int hz, sbintime_t duration)
1103 {
1104 struct sbuf sb;
1105 char *buf;
1106
1107 buf = malloc(DEVCTL_LEN, M_VT, M_NOWAIT);
1108 if (buf == NULL)
1109 return;
1110 sbuf_new(&sb, buf, DEVCTL_LEN, SBUF_FIXEDLEN);
1111 sbuf_printf(&sb, "enabled=%s hushed=%s hz=%d duration_ms=%d",
1112 enabled ? "true" : "false", hushed ? "true" : "false",
1113 hz, (int)(duration / SBT_1MS));
1114 sbuf_finish(&sb);
1115 if (sbuf_error(&sb) == 0)
1116 devctl_notify("VT", "BELL", "RING", sbuf_data(&sb));
1117 sbuf_delete(&sb);
1118 free(buf, M_VT);
1119 }
1120
1121 static void
vtterm_bell(struct terminal * tm)1122 vtterm_bell(struct terminal *tm)
1123 {
1124 struct vt_window *vw = tm->tm_softc;
1125 struct vt_device *vd = vw->vw_device;
1126
1127 vtterm_devctl(vt_enable_bell, vd->vd_flags & VDF_QUIET_BELL,
1128 vw->vw_bell_pitch, vw->vw_bell_duration);
1129
1130 if (!vt_enable_bell)
1131 return;
1132
1133 if (vd->vd_flags & VDF_QUIET_BELL)
1134 return;
1135
1136 if (vw->vw_bell_pitch == 0 ||
1137 vw->vw_bell_duration == 0)
1138 return;
1139
1140 sysbeep(vw->vw_bell_pitch, vw->vw_bell_duration);
1141 }
1142
1143 /*
1144 * Beep with user-provided frequency and duration as specified by a KDMKTONE
1145 * ioctl (compatible with Linux). The frequency is specified as a 8254 PIT
1146 * divisor for a 1.19MHz clock.
1147 *
1148 * See https://tldp.org/LDP/lpg/node83.html.
1149 */
1150 static void
vtterm_beep(struct terminal * tm,u_int param)1151 vtterm_beep(struct terminal *tm, u_int param)
1152 {
1153 u_int freq;
1154 sbintime_t period;
1155 struct vt_window *vw = tm->tm_softc;
1156 struct vt_device *vd = vw->vw_device;
1157
1158 if ((param == 0) || ((param & 0xffff) == 0)) {
1159 vtterm_bell(tm);
1160 return;
1161 }
1162
1163 /* XXX period unit is supposed to be "timer ticks." */
1164 period = ((param >> 16) & 0xffff) * SBT_1MS;
1165 freq = 1193182 / (param & 0xffff);
1166
1167 vtterm_devctl(vt_enable_bell, vd->vd_flags & VDF_QUIET_BELL,
1168 freq, period);
1169
1170 if (!vt_enable_bell)
1171 return;
1172
1173 sysbeep(freq, period);
1174 }
1175
1176 static void
vtterm_cursor(struct terminal * tm,const term_pos_t * p)1177 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
1178 {
1179 struct vt_window *vw = tm->tm_softc;
1180
1181 vtbuf_cursor_position(&vw->vw_buf, p);
1182 }
1183
1184 static void
vtterm_putchar(struct terminal * tm,const term_pos_t * p,term_char_t c)1185 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
1186 {
1187 struct vt_window *vw = tm->tm_softc;
1188
1189 vtbuf_putchar(&vw->vw_buf, p, c);
1190 }
1191
1192 static void
vtterm_fill(struct terminal * tm,const term_rect_t * r,term_char_t c)1193 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
1194 {
1195 struct vt_window *vw = tm->tm_softc;
1196
1197 vtbuf_fill(&vw->vw_buf, r, c);
1198 }
1199
1200 static void
vtterm_copy(struct terminal * tm,const term_rect_t * r,const term_pos_t * p)1201 vtterm_copy(struct terminal *tm, const term_rect_t *r,
1202 const term_pos_t *p)
1203 {
1204 struct vt_window *vw = tm->tm_softc;
1205
1206 vtbuf_copy(&vw->vw_buf, r, p);
1207 }
1208
1209 static void
vtterm_param(struct terminal * tm,int cmd,unsigned int arg)1210 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
1211 {
1212 struct vt_window *vw = tm->tm_softc;
1213
1214 switch (cmd) {
1215 case TP_SETLOCALCURSOR:
1216 /*
1217 * 0 means normal (usually block), 1 means hidden, and
1218 * 2 means blinking (always block) for compatibility with
1219 * syscons. We don't support any changes except hiding,
1220 * so must map 2 to 0.
1221 */
1222 arg = (arg == 1) ? 0 : 1;
1223 /* FALLTHROUGH */
1224 case TP_SHOWCURSOR:
1225 vtbuf_cursor_visibility(&vw->vw_buf, arg);
1226 vt_resume_flush_timer(vw, 0);
1227 break;
1228 case TP_MOUSE:
1229 vw->vw_mouse_level = arg;
1230 break;
1231 case TP_SETBELLPD:
1232 vw->vw_bell_pitch = TP_SETBELLPD_PITCH(arg);
1233 vw->vw_bell_duration =
1234 TICKS_2_MSEC(TP_SETBELLPD_DURATION(arg)) * SBT_1MS;
1235 break;
1236 }
1237 }
1238
1239 void
vt_determine_colors(term_char_t c,int cursor,term_color_t * fg,term_color_t * bg)1240 vt_determine_colors(term_char_t c, int cursor,
1241 term_color_t *fg, term_color_t *bg)
1242 {
1243 term_color_t tmp;
1244 int invert;
1245
1246 invert = 0;
1247
1248 *fg = TCHAR_FGCOLOR(c);
1249 if (TCHAR_FORMAT(c) & TF_BOLD)
1250 *fg = TCOLOR_LIGHT(*fg);
1251 *bg = TCHAR_BGCOLOR(c);
1252 if (TCHAR_FORMAT(c) & TF_BLINK)
1253 *bg = TCOLOR_LIGHT(*bg);
1254
1255 if (TCHAR_FORMAT(c) & TF_REVERSE)
1256 invert ^= 1;
1257 if (cursor)
1258 invert ^= 1;
1259
1260 if (invert) {
1261 tmp = *fg;
1262 *fg = *bg;
1263 *bg = tmp;
1264 }
1265 }
1266
1267 #ifndef SC_NO_CUTPASTE
1268 int
vt_is_cursor_in_area(const struct vt_device * vd,const term_rect_t * area)1269 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1270 {
1271 unsigned int mx, my;
1272
1273 /*
1274 * We use the cursor position saved during the current refresh,
1275 * in case the cursor moved since.
1276 */
1277 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1278 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1279
1280 if (mx >= area->tr_end.tp_col ||
1281 mx + vd->vd_mcursor->width <= area->tr_begin.tp_col ||
1282 my >= area->tr_end.tp_row ||
1283 my + vd->vd_mcursor->height <= area->tr_begin.tp_row)
1284 return (0);
1285 return (1);
1286 }
1287
1288 static void
vt_mark_mouse_position_as_dirty(struct vt_device * vd,int locked)1289 vt_mark_mouse_position_as_dirty(struct vt_device *vd, int locked)
1290 {
1291 term_rect_t area;
1292 struct vt_window *vw;
1293 struct vt_font *vf;
1294 int x, y;
1295
1296 vw = vd->vd_curwindow;
1297 vf = vw->vw_font;
1298
1299 x = vd->vd_mx_drawn;
1300 y = vd->vd_my_drawn;
1301
1302 if (vf != NULL) {
1303 area.tr_begin.tp_col = x / vf->vf_width;
1304 area.tr_begin.tp_row = y / vf->vf_height;
1305 area.tr_end.tp_col =
1306 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1307 area.tr_end.tp_row =
1308 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1309 } else {
1310 /*
1311 * No font loaded (ie. vt_vga operating in textmode).
1312 *
1313 * FIXME: This fake area needs to be revisited once the
1314 * mouse cursor is supported in vt_vga's textmode.
1315 */
1316 area.tr_begin.tp_col = x;
1317 area.tr_begin.tp_row = y;
1318 area.tr_end.tp_col = x + 2;
1319 area.tr_end.tp_row = y + 2;
1320 }
1321
1322 if (!locked)
1323 vtbuf_lock(&vw->vw_buf);
1324 if (vd->vd_driver->vd_invalidate_text)
1325 vd->vd_driver->vd_invalidate_text(vd, &area);
1326 vtbuf_dirty(&vw->vw_buf, &area);
1327 if (!locked)
1328 vtbuf_unlock(&vw->vw_buf);
1329 }
1330 #endif
1331
1332 static void
vt_set_border(struct vt_device * vd,const term_rect_t * area,term_color_t c)1333 vt_set_border(struct vt_device *vd, const term_rect_t *area,
1334 term_color_t c)
1335 {
1336 vd_drawrect_t *drawrect = vd->vd_driver->vd_drawrect;
1337
1338 if (drawrect == NULL)
1339 return;
1340
1341 /* Top bar */
1342 if (area->tr_begin.tp_row > 0)
1343 drawrect(vd, 0, 0, vd->vd_width - 1,
1344 area->tr_begin.tp_row - 1, 1, c);
1345
1346 /* Left bar */
1347 if (area->tr_begin.tp_col > 0)
1348 drawrect(vd, 0, area->tr_begin.tp_row,
1349 area->tr_begin.tp_col - 1, area->tr_end.tp_row - 1, 1, c);
1350
1351 /* Right bar */
1352 if (area->tr_end.tp_col < vd->vd_width)
1353 drawrect(vd, area->tr_end.tp_col, area->tr_begin.tp_row,
1354 vd->vd_width - 1, area->tr_end.tp_row - 1, 1, c);
1355
1356 /* Bottom bar */
1357 if (area->tr_end.tp_row < vd->vd_height)
1358 drawrect(vd, 0, area->tr_end.tp_row, vd->vd_width - 1,
1359 vd->vd_height - 1, 1, c);
1360 }
1361
1362 static void
vt_flush_to_buffer(struct vt_device * vd,const struct vt_window * vw,const term_rect_t * area)1363 vt_flush_to_buffer(struct vt_device *vd,
1364 const struct vt_window *vw, const term_rect_t *area)
1365 {
1366 unsigned int col, row;
1367 term_char_t c;
1368 term_color_t fg, bg;
1369 size_t z;
1370
1371 for (row = area->tr_begin.tp_row; row < area->tr_end.tp_row; ++row) {
1372 for (col = area->tr_begin.tp_col; col < area->tr_end.tp_col;
1373 ++col) {
1374 z = row * PIXEL_WIDTH(VT_FB_MAX_WIDTH) + col;
1375 if (z >= PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) *
1376 PIXEL_WIDTH(VT_FB_MAX_WIDTH))
1377 continue;
1378
1379 c = VTBUF_GET_FIELD(&vw->vw_buf, row, col);
1380 vt_determine_colors(c,
1381 VTBUF_ISCURSOR(&vw->vw_buf, row, col), &fg, &bg);
1382
1383 if (vd->vd_drawn && (vd->vd_drawn[z] == c) &&
1384 vd->vd_drawnfg && (vd->vd_drawnfg[z] == fg) &&
1385 vd->vd_drawnbg && (vd->vd_drawnbg[z] == bg)) {
1386 vd->vd_pos_to_flush[z] = false;
1387 continue;
1388 }
1389
1390 vd->vd_pos_to_flush[z] = true;
1391
1392 if (vd->vd_drawn)
1393 vd->vd_drawn[z] = c;
1394 if (vd->vd_drawnfg)
1395 vd->vd_drawnfg[z] = fg;
1396 if (vd->vd_drawnbg)
1397 vd->vd_drawnbg[z] = bg;
1398 }
1399 }
1400 }
1401
1402 static void
vt_bitblt_buffer(struct vt_device * vd,const struct vt_window * vw,const term_rect_t * area)1403 vt_bitblt_buffer(struct vt_device *vd, const struct vt_window *vw,
1404 const term_rect_t *area)
1405 {
1406 unsigned int col, row, x, y;
1407 struct vt_font *vf;
1408 term_char_t c;
1409 term_color_t fg, bg;
1410 const uint8_t *pattern;
1411 size_t z;
1412
1413 vf = vw->vw_font;
1414
1415 for (row = area->tr_begin.tp_row; row < area->tr_end.tp_row; ++row) {
1416 for (col = area->tr_begin.tp_col; col < area->tr_end.tp_col;
1417 ++col) {
1418 x = col * vf->vf_width +
1419 vw->vw_draw_area.tr_begin.tp_col;
1420 y = row * vf->vf_height +
1421 vw->vw_draw_area.tr_begin.tp_row;
1422
1423 z = row * PIXEL_WIDTH(VT_FB_MAX_WIDTH) + col;
1424 if (z >= PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) *
1425 PIXEL_WIDTH(VT_FB_MAX_WIDTH))
1426 continue;
1427 if (!vd->vd_pos_to_flush[z])
1428 continue;
1429
1430 c = vd->vd_drawn[z];
1431 fg = vd->vd_drawnfg[z];
1432 bg = vd->vd_drawnbg[z];
1433
1434 pattern = vtfont_lookup(vf, c);
1435 vd->vd_driver->vd_bitblt_bmp(vd, vw,
1436 pattern, NULL, vf->vf_width, vf->vf_height,
1437 x, y, fg, bg);
1438 }
1439 }
1440
1441 #ifndef SC_NO_CUTPASTE
1442 if (!vd->vd_mshown)
1443 return;
1444
1445 term_rect_t drawn_area;
1446
1447 drawn_area.tr_begin.tp_col = area->tr_begin.tp_col * vf->vf_width;
1448 drawn_area.tr_begin.tp_row = area->tr_begin.tp_row * vf->vf_height;
1449 drawn_area.tr_end.tp_col = area->tr_end.tp_col * vf->vf_width;
1450 drawn_area.tr_end.tp_row = area->tr_end.tp_row * vf->vf_height;
1451
1452 if (vt_is_cursor_in_area(vd, &drawn_area)) {
1453 vd->vd_driver->vd_bitblt_bmp(vd, vw,
1454 vd->vd_mcursor->map, vd->vd_mcursor->mask,
1455 vd->vd_mcursor->width, vd->vd_mcursor->height,
1456 vd->vd_mx_drawn + vw->vw_draw_area.tr_begin.tp_col,
1457 vd->vd_my_drawn + vw->vw_draw_area.tr_begin.tp_row,
1458 vd->vd_mcursor_fg, vd->vd_mcursor_bg);
1459 }
1460 #endif
1461 }
1462
1463 static void
vt_draw_decorations(struct vt_device * vd)1464 vt_draw_decorations(struct vt_device *vd)
1465 {
1466 struct vt_window *vw;
1467 const teken_attr_t *a;
1468
1469 vw = vd->vd_curwindow;
1470
1471 a = teken_get_curattr(&vw->vw_terminal->tm_emulator);
1472 vt_set_border(vd, &vw->vw_draw_area, a->ta_bgcolor);
1473
1474 if (vt_draw_logo_cpus)
1475 vtterm_draw_cpu_logos(vd);
1476 }
1477
1478 static int
vt_flush(struct vt_device * vd)1479 vt_flush(struct vt_device *vd)
1480 {
1481 struct vt_window *vw;
1482 struct vt_font *vf;
1483 term_rect_t tarea;
1484 #ifndef SC_NO_CUTPASTE
1485 int cursor_was_shown, cursor_moved;
1486 #endif
1487 bool needs_refresh;
1488
1489 if (inside_vt_flush && KERNEL_PANICKED())
1490 return (0);
1491
1492 vw = vd->vd_curwindow;
1493 if (vw == NULL)
1494 return (0);
1495
1496 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1497 return (0);
1498
1499 vf = vw->vw_font;
1500 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1501 return (0);
1502
1503 VT_FLUSH_LOCK(vd);
1504
1505 vtbuf_lock(&vw->vw_buf);
1506 inside_vt_flush = true;
1507
1508 #ifndef SC_NO_CUTPASTE
1509 cursor_was_shown = vd->vd_mshown;
1510 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1511 vd->vd_my != vd->vd_my_drawn);
1512
1513 /* Check if the cursor should be displayed or not. */
1514 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1515 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
1516 !kdb_active && !KERNEL_PANICKED()) { /* DDB inactive. */
1517 vd->vd_mshown = 1;
1518 } else {
1519 vd->vd_mshown = 0;
1520 }
1521
1522 /*
1523 * If the cursor changed display state or moved, we must mark
1524 * the old position as dirty, so that it's erased.
1525 */
1526 if (cursor_was_shown != vd->vd_mshown ||
1527 (vd->vd_mshown && cursor_moved))
1528 vt_mark_mouse_position_as_dirty(vd, true);
1529
1530 /*
1531 * Save position of the mouse cursor. It's used by backends to
1532 * know where to draw the cursor and during the next refresh to
1533 * erase the previous position.
1534 */
1535 vd->vd_mx_drawn = vd->vd_mx;
1536 vd->vd_my_drawn = vd->vd_my;
1537
1538 /*
1539 * If the cursor is displayed and has moved since last refresh,
1540 * mark the new position as dirty.
1541 */
1542 if (vd->vd_mshown && cursor_moved)
1543 vt_mark_mouse_position_as_dirty(vd, true);
1544 #endif
1545
1546 vtbuf_undirty(&vw->vw_buf, &tarea);
1547
1548 /* Force a full redraw when the screen contents might be invalid. */
1549 needs_refresh = false;
1550 if (vd->vd_flags & (VDF_INVALID | VDF_SUSPENDED)) {
1551 needs_refresh = true;
1552 vd->vd_flags &= ~VDF_INVALID;
1553
1554 vt_termrect(vd, vf, &tarea);
1555 if (vd->vd_driver->vd_invalidate_text)
1556 vd->vd_driver->vd_invalidate_text(vd, &tarea);
1557 }
1558
1559 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1560 if (vd->vd_driver->vd_bitblt_after_vtbuf_unlock) {
1561 /*
1562 * When `vd_bitblt_after_vtbuf_unlock` is set to true,
1563 * we first remember the characters to redraw. They are
1564 * already copied to the `vd_drawn` arrays.
1565 *
1566 * We then unlock vt_buf and proceed with the actual
1567 * drawing using the backend driver.
1568 */
1569 vt_flush_to_buffer(vd, vw, &tarea);
1570 vtbuf_unlock(&vw->vw_buf);
1571 vt_bitblt_buffer(vd, vw, &tarea);
1572
1573 if (needs_refresh)
1574 vt_draw_decorations(vd);
1575
1576 /*
1577 * We can reset `inside_vt_flush` after unlocking vtbuf
1578 * here because we also hold vt_flush_lock in this code
1579 * path.
1580 */
1581 inside_vt_flush = false;
1582 } else {
1583 /*
1584 * When `vd_bitblt_after_vtbuf_unlock` is false, we use
1585 * the backend's `vd_bitblt_text` callback directly.
1586 */
1587 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1588
1589 if (needs_refresh)
1590 vt_draw_decorations(vd);
1591
1592 inside_vt_flush = false;
1593 vtbuf_unlock(&vw->vw_buf);
1594 }
1595
1596 VT_FLUSH_UNLOCK(vd);
1597
1598 return (1);
1599 }
1600
1601 inside_vt_flush = false;
1602 vtbuf_unlock(&vw->vw_buf);
1603
1604 VT_FLUSH_UNLOCK(vd);
1605
1606 return (0);
1607 }
1608
1609 static void
vt_timer(void * arg)1610 vt_timer(void *arg)
1611 {
1612 struct vt_device *vd;
1613 int changed;
1614
1615 vd = arg;
1616 /* Update screen if required. */
1617 changed = vt_flush(vd);
1618
1619 /* Schedule for next update. */
1620 if (changed)
1621 vt_schedule_flush(vd, 0);
1622 else
1623 vd->vd_timer_armed = 0;
1624 }
1625
1626 static void
vtterm_pre_input(struct terminal * tm)1627 vtterm_pre_input(struct terminal *tm)
1628 {
1629 struct vt_window *vw = tm->tm_softc;
1630
1631 if (inside_vt_flush && KERNEL_PANICKED())
1632 return;
1633
1634 vtbuf_lock(&vw->vw_buf);
1635 }
1636
1637 static void
vtterm_post_input(struct terminal * tm)1638 vtterm_post_input(struct terminal *tm)
1639 {
1640 struct vt_window *vw = tm->tm_softc;
1641
1642 if (inside_vt_flush && KERNEL_PANICKED())
1643 return;
1644
1645 vtbuf_unlock(&vw->vw_buf);
1646 vt_resume_flush_timer(vw, 0);
1647 }
1648
1649 static void
vtterm_done(struct terminal * tm)1650 vtterm_done(struct terminal *tm)
1651 {
1652 struct vt_window *vw = tm->tm_softc;
1653 struct vt_device *vd = vw->vw_device;
1654
1655 if (kdb_active || KERNEL_PANICKED()) {
1656 /* Switch to the debugger. */
1657 if (vd->vd_curwindow != vw) {
1658 vd->vd_curwindow = vw;
1659 vd->vd_flags |= VDF_INVALID;
1660 if (vd->vd_driver->vd_postswitch)
1661 vd->vd_driver->vd_postswitch(vd);
1662 }
1663 vd->vd_flags &= ~VDF_SPLASH;
1664 vt_flush(vd);
1665 } else if (vt_slow_down > 0) {
1666 int i, j;
1667 for (i = 0; i < vt_slow_down; i++) {
1668 for (j = 0; j < 1000; j++)
1669 vt_flush(vd);
1670 }
1671 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1672 vt_flush(vd);
1673 }
1674 }
1675
1676 #ifdef DEV_SPLASH
1677 static void
vtterm_splash(struct vt_device * vd)1678 vtterm_splash(struct vt_device *vd)
1679 {
1680 struct splash_info *si;
1681 uintptr_t image;
1682 vt_axis_t top, left;
1683
1684 si = MD_FETCH(preload_kmdp, MODINFOMD_SPLASH, struct splash_info *);
1685 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1686 if (si == NULL) {
1687 top = (vd->vd_height - vt_logo_height) / 2;
1688 left = (vd->vd_width - vt_logo_width) / 2;
1689 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1690 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1691 left, top, TC_WHITE, TC_BLACK);
1692 } else {
1693 if (si->si_depth != 4)
1694 return;
1695 image = (uintptr_t)si + sizeof(struct splash_info);
1696 image = roundup2(image, 8);
1697 top = (vd->vd_height - si->si_height) / 2;
1698 left = (vd->vd_width - si->si_width) / 2;
1699 vd->vd_driver->vd_bitblt_argb(vd, vd->vd_curwindow,
1700 (unsigned char *)image, si->si_width, si->si_height,
1701 left, top);
1702 }
1703 vd->vd_flags |= VDF_SPLASH;
1704 }
1705 }
1706 #endif
1707
1708 static struct vt_font *
parse_font_info_static(struct font_info * fi)1709 parse_font_info_static(struct font_info *fi)
1710 {
1711 struct vt_font *vfp;
1712 uintptr_t ptr;
1713 uint32_t checksum;
1714
1715 if (fi == NULL)
1716 return (NULL);
1717
1718 ptr = (uintptr_t)fi;
1719 /*
1720 * Compute and verify checksum. The total sum of all the fields
1721 * must be 0.
1722 */
1723 checksum = fi->fi_width;
1724 checksum += fi->fi_height;
1725 checksum += fi->fi_bitmap_size;
1726 for (unsigned i = 0; i < VFNT_MAPS; i++)
1727 checksum += fi->fi_map_count[i];
1728
1729 if (checksum + fi->fi_checksum != 0)
1730 return (NULL);
1731
1732 ptr += sizeof(struct font_info);
1733 ptr = roundup2(ptr, 8);
1734
1735 vfp = &vt_font_loader;
1736 vfp->vf_height = fi->fi_height;
1737 vfp->vf_width = fi->fi_width;
1738 /* This is default font, set refcount 1 to disable removal. */
1739 vfp->vf_refcount = 1;
1740 for (unsigned i = 0; i < VFNT_MAPS; i++) {
1741 if (fi->fi_map_count[i] == 0)
1742 continue;
1743 vfp->vf_map_count[i] = fi->fi_map_count[i];
1744 vfp->vf_map[i] = (vfnt_map_t *)ptr;
1745 ptr += (fi->fi_map_count[i] * sizeof(vfnt_map_t));
1746 ptr = roundup2(ptr, 8);
1747 }
1748 vfp->vf_bytes = (uint8_t *)ptr;
1749 return (vfp);
1750 }
1751
1752 /*
1753 * Set up default font with allocated data structures.
1754 * However, we can not set refcount here, because it is already set and
1755 * incremented in vtterm_cnprobe() to avoid being released by font load from
1756 * userland.
1757 */
1758 static struct vt_font *
parse_font_info(struct font_info * fi)1759 parse_font_info(struct font_info *fi)
1760 {
1761 struct vt_font *vfp;
1762 uintptr_t ptr;
1763 uint32_t checksum;
1764 size_t size;
1765
1766 if (fi == NULL)
1767 return (NULL);
1768
1769 ptr = (uintptr_t)fi;
1770 /*
1771 * Compute and verify checksum. The total sum of all the fields
1772 * must be 0.
1773 */
1774 checksum = fi->fi_width;
1775 checksum += fi->fi_height;
1776 checksum += fi->fi_bitmap_size;
1777 for (unsigned i = 0; i < VFNT_MAPS; i++)
1778 checksum += fi->fi_map_count[i];
1779
1780 if (checksum + fi->fi_checksum != 0)
1781 return (NULL);
1782
1783 ptr += sizeof(struct font_info);
1784 ptr = roundup2(ptr, 8);
1785
1786 vfp = &vt_font_loader;
1787 vfp->vf_height = fi->fi_height;
1788 vfp->vf_width = fi->fi_width;
1789 for (unsigned i = 0; i < VFNT_MAPS; i++) {
1790 if (fi->fi_map_count[i] == 0)
1791 continue;
1792 vfp->vf_map_count[i] = fi->fi_map_count[i];
1793 size = fi->fi_map_count[i] * sizeof(vfnt_map_t);
1794 vfp->vf_map[i] = malloc(size, M_VT, M_WAITOK | M_ZERO);
1795 bcopy((vfnt_map_t *)ptr, vfp->vf_map[i], size);
1796 ptr += size;
1797 ptr = roundup2(ptr, 8);
1798 }
1799 vfp->vf_bytes = malloc(fi->fi_bitmap_size, M_VT, M_WAITOK | M_ZERO);
1800 bcopy((uint8_t *)ptr, vfp->vf_bytes, fi->fi_bitmap_size);
1801 return (vfp);
1802 }
1803
1804 static void
vt_init_font(void * arg)1805 vt_init_font(void *arg)
1806 {
1807 struct font_info *fi;
1808 struct vt_font *font;
1809
1810 fi = MD_FETCH(preload_kmdp, MODINFOMD_FONT, struct font_info *);
1811
1812 font = parse_font_info(fi);
1813 if (font != NULL)
1814 vt_font_assigned = font;
1815 }
1816
1817 SYSINIT(vt_init_font, SI_SUB_KMEM, SI_ORDER_ANY, vt_init_font, &vt_consdev);
1818
1819 static void
vt_init_font_static(void)1820 vt_init_font_static(void)
1821 {
1822 struct font_info *fi;
1823 struct vt_font *font;
1824
1825 fi = MD_FETCH(preload_kmdp, MODINFOMD_FONT, struct font_info *);
1826
1827 font = parse_font_info_static(fi);
1828 if (font != NULL)
1829 vt_font_assigned = font;
1830 }
1831
1832 static void
vtterm_cnprobe(struct terminal * tm,struct consdev * cp)1833 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1834 {
1835 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1836 struct vt_window *vw = tm->tm_softc;
1837 struct vt_device *vd = vw->vw_device;
1838 struct winsize wsz;
1839 const term_attr_t *a;
1840
1841 if (!vty_enabled(VTY_VT))
1842 return;
1843
1844 if (vd->vd_flags & VDF_INITIALIZED)
1845 /* Initialization already done. */
1846 return;
1847
1848 SET_FOREACH(vtdlist, vt_drv_set) {
1849 vtd = *vtdlist;
1850 if (vtd->vd_probe == NULL)
1851 continue;
1852 if (vtd->vd_probe(vd) == CN_DEAD)
1853 continue;
1854 if ((vtdbest == NULL) ||
1855 (vtd->vd_priority > vtdbest->vd_priority))
1856 vtdbest = vtd;
1857 }
1858 if (vtdbest == NULL) {
1859 cp->cn_pri = CN_DEAD;
1860 vd->vd_flags |= VDF_DEAD;
1861 } else {
1862 vd->vd_driver = vtdbest;
1863 cp->cn_pri = vd->vd_driver->vd_init(vd);
1864 }
1865
1866 /* Check if driver's vt_init return CN_DEAD. */
1867 if (cp->cn_pri == CN_DEAD) {
1868 vd->vd_flags |= VDF_DEAD;
1869 }
1870
1871 /* Initialize any early-boot keyboard drivers */
1872 kbd_configure(KB_CONF_PROBE_ONLY);
1873
1874 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1875 vd->vd_windows[VT_CONSWINDOW] = vw;
1876 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1877
1878 vt_init_font_static();
1879
1880 /* Attach default font if not in TEXTMODE. */
1881 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1882 vw->vw_font = vtfont_ref(vt_font_assigned);
1883 vt_compute_drawable_area(vw);
1884 }
1885
1886 /*
1887 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1888 * that we have the real viewable size, fix it in the static
1889 * buffer.
1890 */
1891 if (vd->vd_width != 0 && vd->vd_height != 0)
1892 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1893
1894 /* We need to access terminal attributes from vtbuf */
1895 vw->vw_buf.vb_terminal = tm;
1896 vtbuf_init_early(&vw->vw_buf);
1897 vt_winsize(vd, vw->vw_font, &wsz);
1898 a = teken_get_curattr(&tm->tm_emulator);
1899 terminal_set_winsize_blank(tm, &wsz, 1, a);
1900
1901 if (vtdbest != NULL) {
1902 #ifdef DEV_SPLASH
1903 if (!vt_splash_cpu)
1904 vtterm_splash(vd);
1905 #endif
1906 vd->vd_flags |= VDF_INITIALIZED;
1907 }
1908 }
1909
1910 static int
vtterm_cngetc(struct terminal * tm)1911 vtterm_cngetc(struct terminal *tm)
1912 {
1913 struct vt_window *vw = tm->tm_softc;
1914 struct vt_device *vd = vw->vw_device;
1915 keyboard_t *kbd;
1916 u_int c;
1917
1918 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1919 return (*vw->vw_kbdsq++);
1920
1921 /* Make sure the splash screen is not there. */
1922 if (vd->vd_flags & VDF_SPLASH) {
1923 /* Remove splash */
1924 vd->vd_flags &= ~VDF_SPLASH;
1925 /* Mark screen as invalid to force update */
1926 vd->vd_flags |= VDF_INVALID;
1927 vt_flush(vd);
1928 }
1929
1930 /* Stripped down keyboard handler. */
1931 if ((kbd = vd->vd_keyboard) == NULL)
1932 return (-1);
1933
1934 /* Force keyboard input mode to K_XLATE */
1935 vw->vw_kbdmode = K_XLATE;
1936 vt_update_kbd_mode(vw, kbd);
1937
1938 /* Switch the keyboard to polling to make it work here. */
1939 kbdd_poll(kbd, TRUE);
1940 c = kbdd_read_char(kbd, 0);
1941 kbdd_poll(kbd, FALSE);
1942 if (c & RELKEY)
1943 return (-1);
1944
1945 if (vw->vw_flags & VWF_SCROLL) {
1946 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1947 vt_flush(vd);
1948 return (-1);
1949 }
1950
1951 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1952 if (c & SPCLKEY) {
1953 switch (c) {
1954 case SPCLKEY | SLK:
1955 vt_save_kbd_state(vw, kbd);
1956 if (vw->vw_kbdstate & SLKED) {
1957 /* Turn scrolling on. */
1958 vw->vw_flags |= VWF_SCROLL;
1959 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1960 } else {
1961 /* Turn scrolling off. */
1962 vt_scroll(vw, 0, VHS_END);
1963 vw->vw_flags &= ~VWF_SCROLL;
1964 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1965 }
1966 break;
1967 /* XXX: KDB can handle history. */
1968 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1969 vw->vw_kbdsq = "\x1b[A";
1970 break;
1971 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1972 vw->vw_kbdsq = "\x1b[B";
1973 break;
1974 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1975 vw->vw_kbdsq = "\x1b[C";
1976 break;
1977 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1978 vw->vw_kbdsq = "\x1b[D";
1979 break;
1980 }
1981
1982 /* Force refresh to make scrollback work. */
1983 vt_flush(vd);
1984 } else if (KEYFLAGS(c) == 0) {
1985 return (KEYCHAR(c));
1986 }
1987
1988 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1989 return (*vw->vw_kbdsq++);
1990
1991 return (-1);
1992 }
1993
1994 /*
1995 * These two do most of what we want to do in vtterm_cnungrab, but without
1996 * actually switching windows. This is necessary for, e.g.,
1997 * vt_allocate_keyboard() to get the current keyboard into the state it needs to
1998 * be in without damaging the device's window state.
1999 *
2000 * Both return the current grab count, though it's only used in vtterm_cnungrab.
2001 */
2002 static int
vtterm_cngrab_noswitch(struct vt_device * vd,struct vt_window * vw)2003 vtterm_cngrab_noswitch(struct vt_device *vd, struct vt_window *vw)
2004 {
2005 keyboard_t *kbd;
2006
2007 if (vw->vw_grabbed++ > 0)
2008 return (vw->vw_grabbed);
2009
2010 if ((kbd = vd->vd_keyboard) == NULL)
2011 return (1);
2012
2013 /*
2014 * Make sure the keyboard is accessible even when the kbd device
2015 * driver is disabled.
2016 */
2017 kbdd_enable(kbd);
2018
2019 /* We shall always use the keyboard in the XLATE mode here. */
2020 vw->vw_prev_kbdmode = vw->vw_kbdmode;
2021 vw->vw_kbdmode = K_XLATE;
2022 vt_update_kbd_mode(vw, kbd);
2023
2024 kbdd_poll(kbd, TRUE);
2025 return (1);
2026 }
2027
2028 static int
vtterm_cnungrab_noswitch(struct vt_device * vd,struct vt_window * vw)2029 vtterm_cnungrab_noswitch(struct vt_device *vd, struct vt_window *vw)
2030 {
2031 keyboard_t *kbd;
2032
2033 if (--vw->vw_grabbed > 0)
2034 return (vw->vw_grabbed);
2035
2036 if ((kbd = vd->vd_keyboard) == NULL)
2037 return (0);
2038
2039 kbdd_poll(kbd, FALSE);
2040
2041 vw->vw_kbdmode = vw->vw_prev_kbdmode;
2042 vt_update_kbd_mode(vw, kbd);
2043 kbdd_disable(kbd);
2044 return (0);
2045 }
2046
2047 static void
vtterm_cngrab(struct terminal * tm)2048 vtterm_cngrab(struct terminal *tm)
2049 {
2050 struct vt_device *vd;
2051 struct vt_window *vw;
2052
2053 vw = tm->tm_softc;
2054 vd = vw->vw_device;
2055
2056 /* To be restored after we ungrab. */
2057 if (vd->vd_grabwindow == NULL)
2058 vd->vd_grabwindow = vd->vd_curwindow;
2059
2060 if (!cold)
2061 vt_window_switch(vw);
2062
2063 vtterm_cngrab_noswitch(vd, vw);
2064 }
2065
2066 static void
vtterm_cnungrab(struct terminal * tm)2067 vtterm_cnungrab(struct terminal *tm)
2068 {
2069 struct vt_device *vd;
2070 struct vt_window *vw, *grabwindow;
2071
2072 vw = tm->tm_softc;
2073 vd = vw->vw_device;
2074
2075 MPASS(vd->vd_grabwindow != NULL);
2076 if (vtterm_cnungrab_noswitch(vd, vw) != 0)
2077 return;
2078
2079 /*
2080 * We set `vd_grabwindow` to NULL before calling vt_window_switch()
2081 * because it allows the underlying vt(4) backend to distinguish a
2082 * "grab" from an "ungrab" of the console.
2083 *
2084 * This is used by `vt_drmfb` in drm-kmod to call either
2085 * fb_debug_enter() or fb_debug_leave() appropriately.
2086 */
2087 grabwindow = vd->vd_grabwindow;
2088 vd->vd_grabwindow = NULL;
2089
2090 if (!cold)
2091 vt_window_switch(grabwindow);
2092 }
2093
2094 static void
vtterm_opened(struct terminal * tm,int opened)2095 vtterm_opened(struct terminal *tm, int opened)
2096 {
2097 struct vt_window *vw = tm->tm_softc;
2098 struct vt_device *vd = vw->vw_device;
2099
2100 VT_LOCK(vd);
2101 vd->vd_flags &= ~VDF_SPLASH;
2102 if (opened)
2103 vw->vw_flags |= VWF_OPENED;
2104 else {
2105 vw->vw_flags &= ~VWF_OPENED;
2106 /* TODO: finish ACQ/REL */
2107 }
2108 VT_UNLOCK(vd);
2109 }
2110
2111 static int
vt_change_font(struct vt_window * vw,struct vt_font * vf)2112 vt_change_font(struct vt_window *vw, struct vt_font *vf)
2113 {
2114 struct vt_device *vd = vw->vw_device;
2115 struct terminal *tm = vw->vw_terminal;
2116 term_pos_t size;
2117 struct winsize wsz;
2118
2119 /*
2120 * Changing fonts.
2121 *
2122 * Changing fonts is a little tricky. We must prevent
2123 * simultaneous access to the device, so we must stop
2124 * the display timer and the terminal from accessing.
2125 * We need to switch fonts and grow our screen buffer.
2126 *
2127 * XXX: Right now the code uses terminal_mute() to
2128 * prevent data from reaching the console driver while
2129 * resizing the screen buffer. This isn't elegant...
2130 */
2131
2132 VT_LOCK(vd);
2133 if (vw->vw_flags & VWF_BUSY) {
2134 /* Another process is changing the font. */
2135 VT_UNLOCK(vd);
2136 return (EBUSY);
2137 }
2138 vw->vw_flags |= VWF_BUSY;
2139 VT_UNLOCK(vd);
2140
2141 vt_termsize(vd, vf, &size);
2142 vt_winsize(vd, vf, &wsz);
2143
2144 /* Grow the screen buffer and terminal. */
2145 terminal_mute(tm, 1);
2146 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
2147 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
2148 terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
2149 terminal_mute(tm, 0);
2150
2151 /* Actually apply the font to the current window. */
2152 VT_LOCK(vd);
2153 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
2154 /*
2155 * In case vt_change_font called to update size we don't need
2156 * to update font link.
2157 */
2158 vtfont_unref(vw->vw_font);
2159 vw->vw_font = vtfont_ref(vf);
2160 }
2161
2162 /*
2163 * Compute the drawable area and move the mouse cursor inside
2164 * it, in case the new area is smaller than the previous one.
2165 */
2166 vt_compute_drawable_area(vw);
2167 vd->vd_mx = min(vd->vd_mx,
2168 vw->vw_draw_area.tr_end.tp_col -
2169 vw->vw_draw_area.tr_begin.tp_col - 1);
2170 vd->vd_my = min(vd->vd_my,
2171 vw->vw_draw_area.tr_end.tp_row -
2172 vw->vw_draw_area.tr_begin.tp_row - 1);
2173
2174 /* Force a full redraw the next timer tick. */
2175 if (vd->vd_curwindow == vw) {
2176 vd->vd_flags |= VDF_INVALID;
2177 vt_resume_flush_timer(vw, 0);
2178 }
2179 vw->vw_flags &= ~VWF_BUSY;
2180 VT_UNLOCK(vd);
2181 return (0);
2182 }
2183
2184 static int
vt_proc_alive(struct vt_window * vw)2185 vt_proc_alive(struct vt_window *vw)
2186 {
2187 struct proc *p;
2188
2189 if (vw->vw_smode.mode != VT_PROCESS)
2190 return (FALSE);
2191
2192 if (vw->vw_proc) {
2193 if ((p = pfind(vw->vw_pid)) != NULL)
2194 PROC_UNLOCK(p);
2195 if (vw->vw_proc == p)
2196 return (TRUE);
2197 vw->vw_proc = NULL;
2198 vw->vw_smode.mode = VT_AUTO;
2199 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
2200 vw->vw_pid = 0;
2201 }
2202 return (FALSE);
2203 }
2204
2205 static int
signal_vt_rel(struct vt_window * vw)2206 signal_vt_rel(struct vt_window *vw)
2207 {
2208
2209 if (vw->vw_smode.mode != VT_PROCESS)
2210 return (FALSE);
2211 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
2212 vw->vw_proc = NULL;
2213 vw->vw_pid = 0;
2214 return (TRUE);
2215 }
2216 vw->vw_flags |= VWF_SWWAIT_REL;
2217 PROC_LOCK(vw->vw_proc);
2218 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
2219 PROC_UNLOCK(vw->vw_proc);
2220 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
2221 return (TRUE);
2222 }
2223
2224 static int
signal_vt_acq(struct vt_window * vw)2225 signal_vt_acq(struct vt_window *vw)
2226 {
2227
2228 if (vw->vw_smode.mode != VT_PROCESS)
2229 return (FALSE);
2230 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2231 cnavailable(vw->vw_terminal->consdev, FALSE);
2232 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
2233 vw->vw_proc = NULL;
2234 vw->vw_pid = 0;
2235 return (TRUE);
2236 }
2237 vw->vw_flags |= VWF_SWWAIT_ACQ;
2238 PROC_LOCK(vw->vw_proc);
2239 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
2240 PROC_UNLOCK(vw->vw_proc);
2241 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
2242 return (TRUE);
2243 }
2244
2245 static int
finish_vt_rel(struct vt_window * vw,int release,int * s)2246 finish_vt_rel(struct vt_window *vw, int release, int *s)
2247 {
2248
2249 if (vw->vw_flags & VWF_SWWAIT_REL) {
2250 vw->vw_flags &= ~VWF_SWWAIT_REL;
2251 if (release) {
2252 taskqueue_drain_timeout(taskqueue_thread, &vw->vw_timeout_task_dead);
2253 (void)vt_late_window_switch(vw->vw_switch_to);
2254 }
2255 return (0);
2256 }
2257 return (EINVAL);
2258 }
2259
2260 static int
finish_vt_acq(struct vt_window * vw)2261 finish_vt_acq(struct vt_window *vw)
2262 {
2263
2264 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
2265 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
2266 return (0);
2267 }
2268 return (EINVAL);
2269 }
2270
2271 #ifndef SC_NO_CUTPASTE
2272 static void
vt_mouse_terminput_button(struct vt_device * vd,int button)2273 vt_mouse_terminput_button(struct vt_device *vd, int button)
2274 {
2275 struct vt_window *vw;
2276 struct vt_font *vf;
2277 char mouseb[6] = "\x1B[M";
2278 int i, x, y;
2279
2280 vw = vd->vd_curwindow;
2281 vf = vw->vw_font;
2282
2283 /* Translate to char position. */
2284 x = vd->vd_mx / vf->vf_width;
2285 y = vd->vd_my / vf->vf_height;
2286 /* Avoid overflow. */
2287 x = MIN(x, 255 - '!');
2288 y = MIN(y, 255 - '!');
2289
2290 mouseb[3] = ' ' + button;
2291 mouseb[4] = '!' + x;
2292 mouseb[5] = '!' + y;
2293
2294 for (i = 0; i < sizeof(mouseb); i++)
2295 terminal_input_char(vw->vw_terminal, mouseb[i]);
2296 }
2297
2298 static void
vt_mouse_terminput(struct vt_device * vd,int type,int x,int y,int event,int cnt)2299 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
2300 int cnt)
2301 {
2302
2303 switch (type) {
2304 case MOUSE_BUTTON_EVENT:
2305 if (cnt > 0) {
2306 /* Mouse button pressed. */
2307 if (event & MOUSE_BUTTON1DOWN)
2308 vt_mouse_terminput_button(vd, 0);
2309 if (event & MOUSE_BUTTON2DOWN)
2310 vt_mouse_terminput_button(vd, 1);
2311 if (event & MOUSE_BUTTON3DOWN)
2312 vt_mouse_terminput_button(vd, 2);
2313 } else {
2314 /* Mouse button released. */
2315 vt_mouse_terminput_button(vd, 3);
2316 }
2317 break;
2318 #ifdef notyet
2319 case MOUSE_MOTION_EVENT:
2320 if (mouse->u.data.z < 0) {
2321 /* Scroll up. */
2322 sc_mouse_input_button(vd, 64);
2323 } else if (mouse->u.data.z > 0) {
2324 /* Scroll down. */
2325 sc_mouse_input_button(vd, 65);
2326 }
2327 break;
2328 #endif
2329 }
2330 }
2331
2332 static void
vt_mouse_paste(void)2333 vt_mouse_paste(void)
2334 {
2335 term_char_t *buf;
2336 int i, len;
2337
2338 len = VD_PASTEBUFLEN(main_vd);
2339 buf = VD_PASTEBUF(main_vd);
2340 len /= sizeof(term_char_t);
2341 for (i = 0; i < len; i++) {
2342 if (TCHAR_CHARACTER(buf[i]) == '\0')
2343 continue;
2344 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
2345 buf[i]);
2346 }
2347 }
2348
2349 void
vt_mouse_event(int type,int x,int y,int event,int cnt,int mlevel)2350 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
2351 {
2352 struct vt_device *vd;
2353 struct vt_window *vw;
2354 struct vt_font *vf;
2355 term_pos_t size;
2356 int len, mark;
2357
2358 vd = main_vd;
2359 vw = vd->vd_curwindow;
2360 vf = vw->vw_font;
2361
2362 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
2363 /*
2364 * Either the mouse is disabled, or the window is in
2365 * "graphics mode". The graphics mode is usually set by
2366 * an X server, using the KDSETMODE ioctl.
2367 */
2368 return;
2369
2370 if (vf == NULL) /* Text mode. */
2371 return;
2372
2373 /*
2374 * TODO: add flag about pointer position changed, to not redraw chars
2375 * under mouse pointer when nothing changed.
2376 */
2377
2378 if (vw->vw_mouse_level > 0)
2379 vt_mouse_terminput(vd, type, x, y, event, cnt);
2380
2381 switch (type) {
2382 case MOUSE_ACTION:
2383 case MOUSE_MOTION_EVENT:
2384 /* Movement */
2385 x += vd->vd_mx;
2386 y += vd->vd_my;
2387
2388 vt_termsize(vd, vf, &size);
2389
2390 /* Apply limits. */
2391 x = MAX(x, 0);
2392 y = MAX(y, 0);
2393 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
2394 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
2395
2396 vd->vd_mx = x;
2397 vd->vd_my = y;
2398 if (vd->vd_mstate & (MOUSE_BUTTON1DOWN | VT_MOUSE_EXTENDBUTTON))
2399 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
2400 vd->vd_mx / vf->vf_width,
2401 vd->vd_my / vf->vf_height);
2402
2403 vt_resume_flush_timer(vw, 0);
2404 return; /* Done */
2405 case MOUSE_BUTTON_EVENT:
2406 /* Buttons */
2407 break;
2408 default:
2409 return; /* Done */
2410 }
2411
2412 switch (event) {
2413 case MOUSE_BUTTON1DOWN:
2414 switch (cnt % 4) {
2415 case 0: /* up */
2416 mark = VTB_MARK_END;
2417 break;
2418 case 1: /* single click: start cut operation */
2419 mark = VTB_MARK_START;
2420 break;
2421 case 2: /* double click: cut a word */
2422 mark = VTB_MARK_WORD;
2423 break;
2424 default: /* triple click: cut a line */
2425 mark = VTB_MARK_ROW;
2426 break;
2427 }
2428 break;
2429 case VT_MOUSE_PASTEBUTTON:
2430 switch (cnt) {
2431 case 0: /* up */
2432 break;
2433 default:
2434 vt_mouse_paste();
2435 /* clear paste buffer selection after paste */
2436 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_START,
2437 vd->vd_mx / vf->vf_width,
2438 vd->vd_my / vf->vf_height);
2439 break;
2440 }
2441 return; /* Done */
2442 case VT_MOUSE_EXTENDBUTTON:
2443 switch (cnt) {
2444 case 0: /* up */
2445 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
2446 mark = VTB_MARK_EXTEND;
2447 else
2448 mark = VTB_MARK_NONE;
2449 break;
2450 default:
2451 mark = VTB_MARK_EXTEND;
2452 break;
2453 }
2454 break;
2455 default:
2456 return; /* Done */
2457 }
2458
2459 /* Save buttons state. */
2460 if (cnt > 0)
2461 vd->vd_mstate |= event;
2462 else
2463 vd->vd_mstate &= ~event;
2464
2465 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
2466 vd->vd_my / vf->vf_height) == 1) {
2467 /*
2468 * We have something marked to copy, so update pointer to
2469 * window with selection.
2470 */
2471 vt_resume_flush_timer(vw, 0);
2472
2473 switch (mark) {
2474 case VTB_MARK_END:
2475 case VTB_MARK_WORD:
2476 case VTB_MARK_ROW:
2477 case VTB_MARK_EXTEND:
2478 break;
2479 default:
2480 /* Other types of mark do not require to copy data. */
2481 return;
2482 }
2483
2484 /* Get current selection size in bytes. */
2485 len = vtbuf_get_marked_len(&vw->vw_buf);
2486 if (len <= 0)
2487 return;
2488
2489 /* Reallocate buffer only if old one is too small. */
2490 if (len > VD_PASTEBUFSZ(vd)) {
2491 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
2492 M_WAITOK | M_ZERO);
2493 /* Update buffer size. */
2494 VD_PASTEBUFSZ(vd) = len;
2495 }
2496 /* Request copy/paste buffer data, no more than `len' */
2497 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd), len, mark);
2498
2499 VD_PASTEBUFLEN(vd) = len;
2500
2501 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
2502 }
2503 }
2504
2505 void
vt_mouse_state(int show)2506 vt_mouse_state(int show)
2507 {
2508 struct vt_device *vd;
2509 struct vt_window *vw;
2510
2511 vd = main_vd;
2512 vw = vd->vd_curwindow;
2513
2514 switch (show) {
2515 case VT_MOUSE_HIDE:
2516 vw->vw_flags |= VWF_MOUSE_HIDE;
2517 break;
2518 case VT_MOUSE_SHOW:
2519 vw->vw_flags &= ~VWF_MOUSE_HIDE;
2520 break;
2521 }
2522
2523 /* Mark mouse position as dirty. */
2524 vt_mark_mouse_position_as_dirty(vd, false);
2525 vt_resume_flush_timer(vw, 0);
2526 }
2527 #endif
2528
2529 static int
vtterm_mmap(struct terminal * tm,vm_ooffset_t offset,vm_paddr_t * paddr,int nprot,vm_memattr_t * memattr)2530 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
2531 int nprot, vm_memattr_t *memattr)
2532 {
2533 struct vt_window *vw = tm->tm_softc;
2534 struct vt_device *vd = vw->vw_device;
2535
2536 if (vd->vd_driver->vd_fb_mmap)
2537 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
2538 memattr));
2539
2540 return (ENXIO);
2541 }
2542
2543 static int
vtterm_ioctl(struct terminal * tm,u_long cmd,caddr_t data,struct thread * td)2544 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
2545 struct thread *td)
2546 {
2547 struct vt_window *vw = tm->tm_softc;
2548 struct vt_device *vd = vw->vw_device;
2549 keyboard_t *kbd;
2550 int error, i, s;
2551 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
2552 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
2553 int ival;
2554
2555 switch (cmd) {
2556 case _IO('v', 4):
2557 cmd = VT_RELDISP;
2558 break;
2559 case _IO('v', 5):
2560 cmd = VT_ACTIVATE;
2561 break;
2562 case _IO('v', 6):
2563 cmd = VT_WAITACTIVE;
2564 break;
2565 case _IO('K', 20):
2566 cmd = KDSKBSTATE;
2567 break;
2568 case _IO('K', 67):
2569 cmd = KDSETRAD;
2570 break;
2571 case _IO('K', 7):
2572 cmd = KDSKBMODE;
2573 break;
2574 case _IO('K', 8):
2575 cmd = KDMKTONE;
2576 break;
2577 case _IO('K', 10):
2578 cmd = KDSETMODE;
2579 break;
2580 case _IO('K', 13):
2581 cmd = KDSBORDER;
2582 break;
2583 case _IO('K', 63):
2584 cmd = KIOCSOUND;
2585 break;
2586 case _IO('K', 66):
2587 cmd = KDSETLED;
2588 break;
2589 case _IO('c', 104):
2590 cmd = CONS_SETWINORG;
2591 break;
2592 case _IO('c', 110):
2593 cmd = CONS_SETKBD;
2594 break;
2595 default:
2596 goto skip_thunk;
2597 }
2598 ival = IOCPARM_IVAL(data);
2599 data = (caddr_t)&ival;
2600 skip_thunk:
2601 #endif
2602
2603 switch (cmd) {
2604 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
2605 if (*(int *)data & ~0x7f)
2606 return (EINVAL);
2607 /* FALLTHROUGH */
2608 case GIO_KEYMAP:
2609 case PIO_KEYMAP:
2610 case GIO_DEADKEYMAP:
2611 case PIO_DEADKEYMAP:
2612 #ifdef COMPAT_FREEBSD13
2613 case OGIO_DEADKEYMAP:
2614 case OPIO_DEADKEYMAP:
2615 #endif /* COMPAT_FREEBSD13 */
2616 case GETFKEY:
2617 case SETFKEY:
2618 case KDGKBINFO:
2619 case KDGKBTYPE:
2620 case KDGETREPEAT: /* get keyboard repeat & delay rates */
2621 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
2622 case KBADDKBD: /* add keyboard to mux */
2623 case KBRELKBD: { /* release keyboard from mux */
2624 error = 0;
2625
2626 mtx_lock(&Giant);
2627 if ((kbd = vd->vd_keyboard) != NULL)
2628 error = kbdd_ioctl(kbd, cmd, data);
2629 mtx_unlock(&Giant);
2630 if (error == ENOIOCTL) {
2631 if (cmd == KDGKBTYPE) {
2632 /* always return something? XXX */
2633 *(int *)data = 0;
2634 } else {
2635 return (ENODEV);
2636 }
2637 }
2638 return (error);
2639 }
2640 case KDGKBSTATE: { /* get keyboard state (locks) */
2641 error = 0;
2642
2643 if (vw == vd->vd_curwindow) {
2644 mtx_lock(&Giant);
2645 if ((kbd = vd->vd_keyboard) != NULL)
2646 error = vt_save_kbd_state(vw, kbd);
2647 mtx_unlock(&Giant);
2648
2649 if (error != 0)
2650 return (error);
2651 }
2652
2653 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2654
2655 return (error);
2656 }
2657 case KDSKBSTATE: { /* set keyboard state (locks) */
2658 int state;
2659
2660 state = *(int *)data;
2661 if (state & ~LOCK_MASK)
2662 return (EINVAL);
2663
2664 vw->vw_kbdstate &= ~LOCK_MASK;
2665 vw->vw_kbdstate |= state;
2666
2667 error = 0;
2668 if (vw == vd->vd_curwindow) {
2669 mtx_lock(&Giant);
2670 if ((kbd = vd->vd_keyboard) != NULL)
2671 error = vt_update_kbd_state(vw, kbd);
2672 mtx_unlock(&Giant);
2673 }
2674
2675 return (error);
2676 }
2677 case KDGETLED: { /* get keyboard LED status */
2678 error = 0;
2679
2680 if (vw == vd->vd_curwindow) {
2681 mtx_lock(&Giant);
2682 if ((kbd = vd->vd_keyboard) != NULL)
2683 error = vt_save_kbd_leds(vw, kbd);
2684 mtx_unlock(&Giant);
2685
2686 if (error != 0)
2687 return (error);
2688 }
2689
2690 *(int *)data = vw->vw_kbdstate & LED_MASK;
2691
2692 return (error);
2693 }
2694 case KDSETLED: { /* set keyboard LED status */
2695 int leds;
2696
2697 leds = *(int *)data;
2698 if (leds & ~LED_MASK)
2699 return (EINVAL);
2700
2701 vw->vw_kbdstate &= ~LED_MASK;
2702 vw->vw_kbdstate |= leds;
2703
2704 error = 0;
2705 if (vw == vd->vd_curwindow) {
2706 mtx_lock(&Giant);
2707 if ((kbd = vd->vd_keyboard) != NULL)
2708 error = vt_update_kbd_leds(vw, kbd);
2709 mtx_unlock(&Giant);
2710 }
2711
2712 return (error);
2713 }
2714 case KDGETMODE:
2715 *(int *)data = (vw->vw_flags & VWF_GRAPHICS) ?
2716 KD_GRAPHICS : KD_TEXT;
2717 return (0);
2718 case KDGKBMODE: {
2719 error = 0;
2720
2721 if (vw == vd->vd_curwindow) {
2722 mtx_lock(&Giant);
2723 if ((kbd = vd->vd_keyboard) != NULL)
2724 error = vt_save_kbd_mode(vw, kbd);
2725 mtx_unlock(&Giant);
2726
2727 if (error != 0)
2728 return (error);
2729 }
2730
2731 *(int *)data = vw->vw_kbdmode;
2732
2733 return (error);
2734 }
2735 case KDSKBMODE: {
2736 int mode;
2737
2738 mode = *(int *)data;
2739 switch (mode) {
2740 case K_XLATE:
2741 case K_RAW:
2742 case K_CODE:
2743 vw->vw_kbdmode = mode;
2744
2745 error = 0;
2746 if (vw == vd->vd_curwindow) {
2747 mtx_lock(&Giant);
2748 if ((kbd = vd->vd_keyboard) != NULL)
2749 error = vt_update_kbd_mode(vw, kbd);
2750 mtx_unlock(&Giant);
2751 }
2752
2753 return (error);
2754 default:
2755 return (EINVAL);
2756 }
2757 }
2758 case FBIOGTYPE:
2759 case FBIO_GETWINORG: /* get frame buffer window origin */
2760 case FBIO_GETDISPSTART: /* get display start address */
2761 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2762 case FBIO_BLANK: /* blank display */
2763 case FBIO_GETRGBOFFS: /* get RGB offsets */
2764 if (vd->vd_driver->vd_fb_ioctl)
2765 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2766 break;
2767 case CONS_BLANKTIME:
2768 /* XXX */
2769 return (0);
2770 case CONS_HISTORY:
2771 if (*(int *)data < 0)
2772 return EINVAL;
2773 if (*(int *)data != vw->vw_buf.vb_history_size)
2774 vtbuf_sethistory_size(&vw->vw_buf, *(int *)data);
2775 return (0);
2776 case CONS_CLRHIST:
2777 vtbuf_clearhistory(&vw->vw_buf);
2778 /*
2779 * Invalidate the entire visible window; it is not guaranteed
2780 * that this operation will be immediately followed by a scroll
2781 * event, so it would otherwise be possible for prior artifacts
2782 * to remain visible.
2783 */
2784 VT_LOCK(vd);
2785 if (vw == vd->vd_curwindow) {
2786 vd->vd_flags |= VDF_INVALID;
2787 vt_resume_flush_timer(vw, 0);
2788 }
2789 VT_UNLOCK(vd);
2790 return (0);
2791 case CONS_GET:
2792 /* XXX */
2793 *(int *)data = M_CG640x480;
2794 return (0);
2795 case CONS_BELLTYPE: /* set bell type sound */
2796 if ((*(int *)data) & CONS_QUIET_BELL)
2797 vd->vd_flags |= VDF_QUIET_BELL;
2798 else
2799 vd->vd_flags &= ~VDF_QUIET_BELL;
2800 return (0);
2801 case CONS_GETINFO: {
2802 vid_info_t *vi = (vid_info_t *)data;
2803 if (vi->size != sizeof(struct vid_info))
2804 return (EINVAL);
2805
2806 if (vw == vd->vd_curwindow) {
2807 mtx_lock(&Giant);
2808 if ((kbd = vd->vd_keyboard) != NULL)
2809 vt_save_kbd_state(vw, kbd);
2810 mtx_unlock(&Giant);
2811 }
2812
2813 vi->m_num = vd->vd_curwindow->vw_number + 1;
2814 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2815 /* XXX: other fields! */
2816 return (0);
2817 }
2818 case CONS_GETVERS:
2819 *(int *)data = 0x200;
2820 return (0);
2821 case CONS_MODEINFO:
2822 /* XXX */
2823 return (0);
2824 case CONS_MOUSECTL: {
2825 mouse_info_t *mouse = (mouse_info_t*)data;
2826
2827 /*
2828 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2829 * should not be applied to individual TTYs, but only to
2830 * consolectl.
2831 */
2832 switch (mouse->operation) {
2833 case MOUSE_HIDE:
2834 if (vd->vd_flags & VDF_MOUSECURSOR) {
2835 vd->vd_flags &= ~VDF_MOUSECURSOR;
2836 #ifndef SC_NO_CUTPASTE
2837 vt_mouse_state(VT_MOUSE_HIDE);
2838 #endif
2839 }
2840 return (0);
2841 case MOUSE_SHOW:
2842 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2843 vd->vd_flags |= VDF_MOUSECURSOR;
2844 vd->vd_mx = vd->vd_width / 2;
2845 vd->vd_my = vd->vd_height / 2;
2846 #ifndef SC_NO_CUTPASTE
2847 vt_mouse_state(VT_MOUSE_SHOW);
2848 #endif
2849 }
2850 return (0);
2851 default:
2852 return (EINVAL);
2853 }
2854 }
2855 case PIO_VFONT: {
2856 struct vt_font *vf;
2857
2858 if (vd->vd_flags & VDF_TEXTMODE)
2859 return (ENOTSUP);
2860
2861 error = vtfont_load((void *)data, &vf);
2862 if (error != 0)
2863 return (error);
2864
2865 error = vt_change_font(vw, vf);
2866 vtfont_unref(vf);
2867 return (error);
2868 }
2869 case PIO_VFONT_DEFAULT: {
2870 /* Reset to default font. */
2871 error = vt_change_font(vw, vt_font_assigned);
2872 return (error);
2873 }
2874 case GIO_SCRNMAP: {
2875 scrmap_t *sm = (scrmap_t *)data;
2876
2877 /* We don't have screen maps, so return a handcrafted one. */
2878 for (i = 0; i < 256; i++)
2879 sm->scrmap[i] = i;
2880 return (0);
2881 }
2882 case KDSETMODE:
2883 /*
2884 * FIXME: This implementation is incomplete compared to
2885 * syscons.
2886 */
2887 switch (*(int *)data) {
2888 case KD_TEXT:
2889 case KD_TEXT1:
2890 case KD_PIXEL:
2891 vw->vw_flags &= ~VWF_GRAPHICS;
2892 break;
2893 case KD_GRAPHICS:
2894 vw->vw_flags |= VWF_GRAPHICS;
2895 break;
2896 }
2897 return (0);
2898 case KDENABIO: /* allow io operations */
2899 error = priv_check(td, PRIV_IO);
2900 if (error != 0)
2901 return (error);
2902 error = securelevel_gt(td->td_ucred, 0);
2903 if (error != 0)
2904 return (error);
2905 #if defined(__i386__)
2906 td->td_frame->tf_eflags |= PSL_IOPL;
2907 #elif defined(__amd64__)
2908 td->td_frame->tf_rflags |= PSL_IOPL;
2909 #endif
2910 return (0);
2911 case KDDISABIO: /* disallow io operations (default) */
2912 #if defined(__i386__)
2913 td->td_frame->tf_eflags &= ~PSL_IOPL;
2914 #elif defined(__amd64__)
2915 td->td_frame->tf_rflags &= ~PSL_IOPL;
2916 #endif
2917 return (0);
2918 case KDMKTONE: /* sound the bell */
2919 vtterm_beep(tm, *(u_int *)data);
2920 return (0);
2921 case KIOCSOUND: /* make tone (*data) hz */
2922 /* TODO */
2923 return (0);
2924 case CONS_SETKBD: /* set the new keyboard */
2925 mtx_lock(&Giant);
2926 error = 0;
2927 if (vd->vd_keyboard == NULL ||
2928 vd->vd_keyboard->kb_index != *(int *)data) {
2929 kbd = kbd_get_keyboard(*(int *)data);
2930 if (kbd == NULL) {
2931 mtx_unlock(&Giant);
2932 return (EINVAL);
2933 }
2934 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2935 (void *)vd, vt_kbdevent, vd);
2936 if (i >= 0) {
2937 if ((kbd = vd->vd_keyboard) != NULL) {
2938 vt_save_kbd_state(vd->vd_curwindow, kbd);
2939 kbd_release(kbd, (void *)vd);
2940 }
2941 kbd = vd->vd_keyboard = kbd_get_keyboard(i);
2942
2943 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2944 vt_update_kbd_state(vd->vd_curwindow, kbd);
2945 } else {
2946 error = EPERM; /* XXX */
2947 }
2948 }
2949 mtx_unlock(&Giant);
2950 return (error);
2951 case CONS_RELKBD: /* release the current keyboard */
2952 mtx_lock(&Giant);
2953 error = 0;
2954 if ((kbd = vd->vd_keyboard) != NULL) {
2955 vt_save_kbd_state(vd->vd_curwindow, kbd);
2956 error = kbd_release(kbd, (void *)vd);
2957 if (error == 0) {
2958 vd->vd_keyboard = NULL;
2959 }
2960 }
2961 mtx_unlock(&Giant);
2962 return (error);
2963 case VT_ACTIVATE: {
2964 int win;
2965 win = *(int *)data - 1;
2966 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2967 VT_UNIT(vw), win);
2968 if ((win >= VT_MAXWINDOWS) || (win < 0))
2969 return (EINVAL);
2970 return (vt_proc_window_switch(vd->vd_windows[win]));
2971 }
2972 case VT_GETACTIVE:
2973 *(int *)data = vd->vd_curwindow->vw_number + 1;
2974 return (0);
2975 case VT_GETINDEX:
2976 *(int *)data = vw->vw_number + 1;
2977 return (0);
2978 case VT_LOCKSWITCH:
2979 /* TODO: Check current state, switching can be in progress. */
2980 if ((*(int *)data) == 0x01)
2981 vw->vw_flags |= VWF_VTYLOCK;
2982 else if ((*(int *)data) == 0x02)
2983 vw->vw_flags &= ~VWF_VTYLOCK;
2984 else
2985 return (EINVAL);
2986 return (0);
2987 case VT_OPENQRY:
2988 VT_LOCK(vd);
2989 for (i = 0; i < VT_MAXWINDOWS; i++) {
2990 vw = vd->vd_windows[i];
2991 if (vw == NULL)
2992 continue;
2993 if (!(vw->vw_flags & VWF_OPENED)) {
2994 *(int *)data = vw->vw_number + 1;
2995 VT_UNLOCK(vd);
2996 return (0);
2997 }
2998 }
2999 VT_UNLOCK(vd);
3000 return (EINVAL);
3001 case VT_WAITACTIVE: {
3002 unsigned int idx;
3003
3004 error = 0;
3005
3006 idx = *(unsigned int *)data;
3007 if (idx > VT_MAXWINDOWS)
3008 return (EINVAL);
3009 if (idx > 0)
3010 vw = vd->vd_windows[idx - 1];
3011
3012 VT_LOCK(vd);
3013 while (vd->vd_curwindow != vw && error == 0)
3014 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
3015 VT_UNLOCK(vd);
3016 return (error);
3017 }
3018 case VT_SETMODE: { /* set screen switcher mode */
3019 struct vt_mode *mode;
3020 struct proc *p1;
3021
3022 mode = (struct vt_mode *)data;
3023 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
3024 if (vw->vw_smode.mode == VT_PROCESS) {
3025 p1 = pfind(vw->vw_pid);
3026 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
3027 if (p1)
3028 PROC_UNLOCK(p1);
3029 DPRINTF(5, "error EPERM\n");
3030 return (EPERM);
3031 }
3032 if (p1)
3033 PROC_UNLOCK(p1);
3034 }
3035 if (mode->mode == VT_AUTO) {
3036 vw->vw_smode.mode = VT_AUTO;
3037 vw->vw_proc = NULL;
3038 vw->vw_pid = 0;
3039 DPRINTF(5, "VT_AUTO, ");
3040 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
3041 cnavailable(vw->vw_terminal->consdev, TRUE);
3042 /* were we in the middle of the vty switching process? */
3043 if (finish_vt_rel(vw, TRUE, &s) == 0)
3044 DPRINTF(5, "reset WAIT_REL, ");
3045 if (finish_vt_acq(vw) == 0)
3046 DPRINTF(5, "reset WAIT_ACQ, ");
3047 return (0);
3048 } else if (mode->mode == VT_PROCESS) {
3049 if (!ISSIGVALID(mode->relsig) ||
3050 !ISSIGVALID(mode->acqsig) ||
3051 !ISSIGVALID(mode->frsig)) {
3052 DPRINTF(5, "error EINVAL\n");
3053 return (EINVAL);
3054 }
3055 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
3056 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
3057 vw->vw_proc = td->td_proc;
3058 vw->vw_pid = vw->vw_proc->p_pid;
3059 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
3060 cnavailable(vw->vw_terminal->consdev, FALSE);
3061 } else {
3062 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
3063 mode->mode);
3064 return (EINVAL);
3065 }
3066 DPRINTF(5, "\n");
3067 return (0);
3068 }
3069 case VT_GETMODE: /* get screen switcher mode */
3070 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
3071 return (0);
3072
3073 case VT_RELDISP: /* screen switcher ioctl */
3074 /*
3075 * This must be the current vty which is in the VT_PROCESS
3076 * switching mode...
3077 */
3078 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
3079 VT_PROCESS)) {
3080 return (EINVAL);
3081 }
3082 /* ...and this process is controlling it. */
3083 if (vw->vw_proc != td->td_proc) {
3084 return (EPERM);
3085 }
3086 error = EINVAL;
3087 switch(*(int *)data) {
3088 case VT_FALSE: /* user refuses to release screen, abort */
3089 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
3090 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
3091 SC_DRIVER_NAME, VT_UNIT(vw));
3092 break;
3093 case VT_TRUE: /* user has released screen, go on */
3094 /* finish_vt_rel(..., TRUE, ...) should not be locked */
3095 if (vw->vw_flags & VWF_SWWAIT_REL) {
3096 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
3097 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
3098 SC_DRIVER_NAME, VT_UNIT(vw));
3099 } else {
3100 error = EINVAL;
3101 }
3102 return (error);
3103 case VT_ACKACQ: /* acquire acknowledged, switch completed */
3104 if ((error = finish_vt_acq(vw)) == 0)
3105 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
3106 SC_DRIVER_NAME, VT_UNIT(vw));
3107 break;
3108 default:
3109 break;
3110 }
3111 return (error);
3112 }
3113
3114 return (ENOIOCTL);
3115 }
3116
3117 static struct vt_window *
vt_allocate_window(struct vt_device * vd,unsigned int window)3118 vt_allocate_window(struct vt_device *vd, unsigned int window)
3119 {
3120 struct vt_window *vw;
3121 struct terminal *tm;
3122 term_pos_t size;
3123 struct winsize wsz;
3124
3125 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
3126 vw->vw_device = vd;
3127 vw->vw_number = window;
3128 vw->vw_kbdmode = K_XLATE;
3129
3130 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
3131 vw->vw_font = vtfont_ref(vt_font_assigned);
3132 vt_compute_drawable_area(vw);
3133 }
3134
3135 vt_termsize(vd, vw->vw_font, &size);
3136 vt_winsize(vd, vw->vw_font, &wsz);
3137 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
3138 vw->vw_buf.vb_terminal = tm; /* must be set before vtbuf_init() */
3139 vtbuf_init(&vw->vw_buf, &size);
3140
3141 terminal_set_winsize(tm, &wsz);
3142 vd->vd_windows[window] = vw;
3143 TIMEOUT_TASK_INIT(taskqueue_thread, &vw->vw_timeout_task_dead, 0, &vt_switch_timer, vw);
3144
3145 return (vw);
3146 }
3147
3148 void
vt_upgrade(struct vt_device * vd)3149 vt_upgrade(struct vt_device *vd)
3150 {
3151 struct vt_window *vw;
3152 unsigned int i;
3153 int register_handlers;
3154
3155 if (!vty_enabled(VTY_VT))
3156 return;
3157 if (main_vd->vd_driver == NULL)
3158 return;
3159
3160 for (i = 0; i < VT_MAXWINDOWS; i++) {
3161 vw = vd->vd_windows[i];
3162 if (vw == NULL) {
3163 /* New window. */
3164 vw = vt_allocate_window(vd, i);
3165 }
3166 if (!(vw->vw_flags & VWF_READY)) {
3167 TIMEOUT_TASK_INIT(taskqueue_thread, &vw->vw_timeout_task_dead, 0, &vt_switch_timer, vw);
3168 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
3169 vw->vw_flags |= VWF_READY;
3170 if (vw->vw_flags & VWF_CONSOLE) {
3171 /* For existing console window. */
3172 EVENTHANDLER_REGISTER(shutdown_pre_sync,
3173 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
3174 }
3175 }
3176 }
3177 VT_LOCK(vd);
3178 if (vd->vd_curwindow == NULL)
3179 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
3180
3181 register_handlers = 0;
3182 if (!(vd->vd_flags & VDF_ASYNC)) {
3183 /* Attach keyboard. */
3184 vt_allocate_keyboard(vd);
3185
3186 /* Init 25 Hz timer. */
3187 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
3188
3189 /*
3190 * Start timer when everything ready.
3191 * Note that the operations here are purposefully ordered.
3192 * We need to ensure vd_timer_armed is non-zero before we set
3193 * the VDF_ASYNC flag. That prevents this function from
3194 * racing with vt_resume_flush_timer() to update the
3195 * callout structure.
3196 */
3197 atomic_add_acq_int(&vd->vd_timer_armed, 1);
3198 vd->vd_flags |= VDF_ASYNC;
3199 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
3200 register_handlers = 1;
3201 }
3202
3203 VT_UNLOCK(vd);
3204
3205 /* Refill settings with new sizes. */
3206 vt_resize(vd);
3207
3208 if (register_handlers) {
3209 /* Register suspend/resume handlers. */
3210 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler,
3211 vd, EVENTHANDLER_PRI_ANY);
3212 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd,
3213 EVENTHANDLER_PRI_ANY);
3214 }
3215 }
3216
3217 static void
vt_resize(struct vt_device * vd)3218 vt_resize(struct vt_device *vd)
3219 {
3220 struct vt_window *vw;
3221 int i;
3222
3223 for (i = 0; i < VT_MAXWINDOWS; i++) {
3224 vw = vd->vd_windows[i];
3225 VT_LOCK(vd);
3226 /* Assign default font to window, if not textmode. */
3227 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
3228 vw->vw_font = vtfont_ref(vt_font_assigned);
3229 VT_UNLOCK(vd);
3230
3231 /* Resize terminal windows */
3232 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
3233 DPRINTF(100, "%s: vt_change_font() is busy, "
3234 "window %d\n", __func__, i);
3235 }
3236 }
3237 }
3238
3239 static void
vt_replace_backend(const struct vt_driver * drv,void * softc)3240 vt_replace_backend(const struct vt_driver *drv, void *softc)
3241 {
3242 struct vt_device *vd;
3243
3244 vd = main_vd;
3245
3246 if (vd->vd_flags & VDF_ASYNC) {
3247 /* Stop vt_flush periodic task. */
3248 VT_LOCK(vd);
3249 vt_suspend_flush_timer(vd);
3250 VT_UNLOCK(vd);
3251 /*
3252 * Mute current terminal until we done. vt_change_font (called
3253 * from vt_resize) will unmute it.
3254 */
3255 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
3256 }
3257
3258 /*
3259 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
3260 * set it.
3261 */
3262 VT_LOCK(vd);
3263 vd->vd_flags &= ~VDF_TEXTMODE;
3264
3265 if (drv != NULL) {
3266 /*
3267 * We want to upgrade from the current driver to the
3268 * given driver.
3269 */
3270
3271 vd->vd_prev_driver = vd->vd_driver;
3272 vd->vd_prev_softc = vd->vd_softc;
3273 vd->vd_driver = drv;
3274 vd->vd_softc = softc;
3275
3276 vd->vd_driver->vd_init(vd);
3277 } else if (vd->vd_prev_driver != NULL && vd->vd_prev_softc != NULL) {
3278 /*
3279 * No driver given: we want to downgrade to the previous
3280 * driver.
3281 */
3282 const struct vt_driver *old_drv;
3283 void *old_softc;
3284
3285 old_drv = vd->vd_driver;
3286 old_softc = vd->vd_softc;
3287
3288 vd->vd_driver = vd->vd_prev_driver;
3289 vd->vd_softc = vd->vd_prev_softc;
3290 vd->vd_prev_driver = NULL;
3291 vd->vd_prev_softc = NULL;
3292
3293 vd->vd_flags |= VDF_DOWNGRADE;
3294
3295 vd->vd_driver->vd_init(vd);
3296
3297 if (old_drv->vd_fini)
3298 old_drv->vd_fini(vd, old_softc);
3299
3300 vd->vd_flags &= ~VDF_DOWNGRADE;
3301 }
3302
3303 VT_UNLOCK(vd);
3304
3305 /* Update windows sizes and initialize last items. */
3306 vt_upgrade(vd);
3307
3308 /*
3309 * Give a chance to the new backend to run the post-switch code, for
3310 * instance to refresh the screen.
3311 */
3312 if (vd->vd_driver->vd_postswitch)
3313 vd->vd_driver->vd_postswitch(vd);
3314
3315 #ifdef DEV_SPLASH
3316 if (vd->vd_flags & VDF_SPLASH)
3317 vtterm_splash(vd);
3318 #endif
3319
3320 if (vd->vd_flags & VDF_ASYNC) {
3321 /* Allow to put chars now. */
3322 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
3323 /* Rerun timer for screen updates. */
3324 vt_resume_flush_timer(vd->vd_curwindow, 0);
3325 }
3326
3327 /*
3328 * Register as console. If it already registered, cnadd() will ignore
3329 * it.
3330 */
3331 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
3332 }
3333
3334 static void
vt_suspend_handler(void * priv,enum power_stype stype)3335 vt_suspend_handler(void *priv, enum power_stype stype)
3336 {
3337 struct vt_device *vd;
3338
3339 vd = priv;
3340 vd->vd_flags |= VDF_SUSPENDED;
3341 if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL)
3342 vd->vd_driver->vd_suspend(vd);
3343 }
3344
3345 static void
vt_resume_handler(void * priv,enum power_stype stype)3346 vt_resume_handler(void *priv, enum power_stype stype)
3347 {
3348 struct vt_device *vd;
3349
3350 vd = priv;
3351 if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL)
3352 vd->vd_driver->vd_resume(vd);
3353 vd->vd_flags &= ~VDF_SUSPENDED;
3354 }
3355
3356 int
vt_allocate(const struct vt_driver * drv,void * softc)3357 vt_allocate(const struct vt_driver *drv, void *softc)
3358 {
3359
3360 if (!vty_enabled(VTY_VT))
3361 return (EINVAL);
3362
3363 if (main_vd->vd_driver == NULL) {
3364 main_vd->vd_driver = drv;
3365 printf("VT: initialize with new VT driver \"%s\".\n",
3366 drv->vd_name);
3367 } else {
3368 /*
3369 * Check if have rights to replace current driver. For example:
3370 * it is bad idea to replace KMS driver with generic VGA one.
3371 */
3372 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
3373 printf("VT: Driver priority %d too low. Current %d\n ",
3374 drv->vd_priority, main_vd->vd_driver->vd_priority);
3375 return (EEXIST);
3376 }
3377 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
3378 main_vd->vd_driver->vd_name, drv->vd_name);
3379 }
3380
3381 vt_replace_backend(drv, softc);
3382
3383 return (0);
3384 }
3385
3386 int
vt_deallocate(const struct vt_driver * drv,void * softc)3387 vt_deallocate(const struct vt_driver *drv, void *softc)
3388 {
3389
3390 if (!vty_enabled(VTY_VT))
3391 return (EINVAL);
3392
3393 if (main_vd->vd_prev_driver == NULL ||
3394 main_vd->vd_driver != drv ||
3395 main_vd->vd_softc != softc)
3396 return (EPERM);
3397
3398 printf("VT: Switching back from \"%s\" to \"%s\".\n",
3399 main_vd->vd_driver->vd_name, main_vd->vd_prev_driver->vd_name);
3400
3401 vt_replace_backend(NULL, NULL);
3402
3403 return (0);
3404 }
3405
3406 void
vt_suspend(struct vt_device * vd)3407 vt_suspend(struct vt_device *vd)
3408 {
3409 int error;
3410
3411 if (vt_suspendswitch == 0)
3412 return;
3413 /* Save current window. */
3414 vd->vd_savedwindow = vd->vd_curwindow;
3415 /* Ask holding process to free window and switch to console window */
3416 vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]);
3417
3418 /* Wait for the window switch to complete. */
3419 error = 0;
3420 VT_LOCK(vd);
3421 while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0)
3422 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
3423 VT_UNLOCK(vd);
3424 }
3425
3426 void
vt_resume(struct vt_device * vd)3427 vt_resume(struct vt_device *vd)
3428 {
3429
3430 if (vt_suspendswitch == 0)
3431 return;
3432 /* Switch back to saved window, if any */
3433 vt_proc_window_switch(vd->vd_savedwindow);
3434 vd->vd_savedwindow = NULL;
3435 }
3436