xref: /freebsd/sys/kern/subr_terminal.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This software was developed by Ed Schouten under sponsorship from the
8  * FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/cons.h>
37 #include <sys/consio.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/systm.h>
43 #include <sys/terminal.h>
44 #include <sys/tty.h>
45 
46 #include <machine/stdarg.h>
47 
48 static MALLOC_DEFINE(M_TERMINAL, "terminal", "terminal device");
49 
50 /*
51  * Locking.
52  *
53  * Normally we don't need to lock down the terminal emulator, because
54  * the TTY lock is already held when calling teken_input().
55  * Unfortunately this is not the case when the terminal acts as a
56  * console device, because cnputc() can be called at the same time.
57  * This means terminals may need to be locked down using a spin lock.
58  */
59 #define	TERMINAL_LOCK(tm)	do {					\
60 	if ((tm)->tm_flags & TF_CONS)					\
61 		mtx_lock_spin(&(tm)->tm_mtx);				\
62 	else if ((tm)->tm_tty != NULL)					\
63 		tty_lock((tm)->tm_tty);					\
64 } while (0)
65 #define	TERMINAL_UNLOCK(tm)	do {					\
66 	if ((tm)->tm_flags & TF_CONS)					\
67 		mtx_unlock_spin(&(tm)->tm_mtx);				\
68 	else if ((tm)->tm_tty != NULL)					\
69 		tty_unlock((tm)->tm_tty);				\
70 } while (0)
71 #define	TERMINAL_LOCK_TTY(tm)	do {					\
72 	if ((tm)->tm_flags & TF_CONS)					\
73 		mtx_lock_spin(&(tm)->tm_mtx);				\
74 } while (0)
75 #define	TERMINAL_UNLOCK_TTY(tm)	do {					\
76 	if ((tm)->tm_flags & TF_CONS)					\
77 		mtx_unlock_spin(&(tm)->tm_mtx);				\
78 } while (0)
79 #define	TERMINAL_LOCK_CONS(tm)		mtx_lock_spin(&(tm)->tm_mtx)
80 #define	TERMINAL_UNLOCK_CONS(tm)	mtx_unlock_spin(&(tm)->tm_mtx)
81 
82 /*
83  * TTY routines.
84  */
85 
86 static tsw_open_t	termtty_open;
87 static tsw_close_t	termtty_close;
88 static tsw_outwakeup_t	termtty_outwakeup;
89 static tsw_ioctl_t	termtty_ioctl;
90 static tsw_mmap_t	termtty_mmap;
91 
92 static struct ttydevsw terminal_tty_class = {
93 	.tsw_open	= termtty_open,
94 	.tsw_close	= termtty_close,
95 	.tsw_outwakeup	= termtty_outwakeup,
96 	.tsw_ioctl	= termtty_ioctl,
97 	.tsw_mmap	= termtty_mmap,
98 };
99 
100 /*
101  * Terminal emulator routines.
102  */
103 
104 static tf_bell_t	termteken_bell;
105 static tf_cursor_t	termteken_cursor;
106 static tf_putchar_t	termteken_putchar;
107 static tf_fill_t	termteken_fill;
108 static tf_copy_t	termteken_copy;
109 static tf_pre_input_t	termteken_pre_input;
110 static tf_post_input_t	termteken_post_input;
111 static tf_param_t	termteken_param;
112 static tf_respond_t	termteken_respond;
113 
114 static teken_funcs_t terminal_drawmethods = {
115 	.tf_bell	= termteken_bell,
116 	.tf_cursor	= termteken_cursor,
117 	.tf_putchar	= termteken_putchar,
118 	.tf_fill	= termteken_fill,
119 	.tf_copy	= termteken_copy,
120 	.tf_pre_input	= termteken_pre_input,
121 	.tf_post_input	= termteken_post_input,
122 	.tf_param	= termteken_param,
123 	.tf_respond	= termteken_respond,
124 };
125 
126 /* Kernel message formatting. */
127 static const teken_attr_t kernel_message = {
128 	.ta_fgcolor	= TCHAR_FGCOLOR(TERMINAL_KERN_ATTR),
129 	.ta_bgcolor	= TCHAR_BGCOLOR(TERMINAL_KERN_ATTR),
130 	.ta_format	= TCHAR_FORMAT(TERMINAL_KERN_ATTR)
131 };
132 
133 static const teken_attr_t default_message = {
134 	.ta_fgcolor	= TCHAR_FGCOLOR(TERMINAL_NORM_ATTR),
135 	.ta_bgcolor	= TCHAR_BGCOLOR(TERMINAL_NORM_ATTR),
136 	.ta_format	= TCHAR_FORMAT(TERMINAL_NORM_ATTR)
137 };
138 
139 /* Fudge fg brightness as TF_BOLD (shifted). */
140 #define	TCOLOR_FG_FUDGED(color) __extension__ ({			\
141 	teken_color_t _c;						\
142 									\
143 	_c = (color);							\
144 	TCOLOR_FG(_c & 7) | ((_c & 8) << 18);				\
145 })
146 
147 /* Fudge bg brightness as TF_BLINK (shifted). */
148 #define	TCOLOR_BG_FUDGED(color) __extension__ ({			\
149 	teken_color_t _c;						\
150 									\
151 	_c = (color);							\
152 	TCOLOR_BG(_c & 7) | ((_c & 8) << 20);				\
153 })
154 
155 #define	TCOLOR_256TO16(color) __extension__ ({				\
156 	teken_color_t _c;						\
157 									\
158 	_c = (color);							\
159 	if (_c >= 16)							\
160 		_c = teken_256to16(_c);					\
161 	_c;								\
162 })
163 
164 #define	TCHAR_CREATE(c, a)	((c) | TFORMAT((a)->ta_format) |	\
165 	TCOLOR_FG_FUDGED(TCOLOR_256TO16((a)->ta_fgcolor)) |		\
166 	TCOLOR_BG_FUDGED(TCOLOR_256TO16((a)->ta_bgcolor)))
167 
168 static void
169 terminal_init(struct terminal *tm)
170 {
171 
172 	if (tm->tm_flags & TF_CONS)
173 		mtx_init(&tm->tm_mtx, "trmlck", NULL, MTX_SPIN);
174 	teken_init(&tm->tm_emulator, &terminal_drawmethods, tm);
175 	teken_set_defattr(&tm->tm_emulator, &default_message);
176 }
177 
178 struct terminal *
179 terminal_alloc(const struct terminal_class *tc, void *softc)
180 {
181 	struct terminal *tm;
182 
183 	tm = malloc(sizeof(struct terminal), M_TERMINAL, M_WAITOK|M_ZERO);
184 	terminal_init(tm);
185 
186 	tm->tm_class = tc;
187 	tm->tm_softc = softc;
188 
189 	return (tm);
190 }
191 
192 static void
193 terminal_sync_ttysize(struct terminal *tm)
194 {
195 	struct tty *tp;
196 
197 	tp = tm->tm_tty;
198 	if (tp == NULL)
199 		return;
200 
201 	tty_lock(tp);
202 	tty_set_winsize(tp, &tm->tm_winsize);
203 	tty_unlock(tp);
204 }
205 
206 void
207 terminal_maketty(struct terminal *tm, const char *fmt, ...)
208 {
209 	struct tty *tp;
210 	char name[8];
211 	va_list ap;
212 
213 	va_start(ap, fmt);
214 	vsnrprintf(name, sizeof name, 32, fmt, ap);
215 	va_end(ap);
216 
217 	tp = tty_alloc(&terminal_tty_class, tm);
218 	tty_makedev(tp, NULL, "%s", name);
219 	tm->tm_tty = tp;
220 	terminal_sync_ttysize(tm);
221 }
222 
223 void
224 terminal_set_cursor(struct terminal *tm, const term_pos_t *pos)
225 {
226 
227 	teken_set_cursor(&tm->tm_emulator, pos);
228 }
229 
230 void
231 terminal_set_winsize_blank(struct terminal *tm, const struct winsize *size,
232     int blank, const term_attr_t *attr)
233 {
234 	term_rect_t r;
235 
236 	tm->tm_winsize = *size;
237 
238 	r.tr_begin.tp_row = r.tr_begin.tp_col = 0;
239 	r.tr_end.tp_row = size->ws_row;
240 	r.tr_end.tp_col = size->ws_col;
241 
242 	TERMINAL_LOCK(tm);
243 	if (blank == 0)
244 		teken_set_winsize_noreset(&tm->tm_emulator, &r.tr_end);
245 	else
246 		teken_set_winsize(&tm->tm_emulator, &r.tr_end);
247 	TERMINAL_UNLOCK(tm);
248 
249 	if ((blank != 0) && !(tm->tm_flags & TF_MUTE))
250 		tm->tm_class->tc_fill(tm, &r,
251 		    TCHAR_CREATE((teken_char_t)' ', attr));
252 
253 	terminal_sync_ttysize(tm);
254 }
255 
256 void
257 terminal_set_winsize(struct terminal *tm, const struct winsize *size)
258 {
259 
260 	terminal_set_winsize_blank(tm, size, 1,
261 	    (const term_attr_t *)&default_message);
262 }
263 
264 /*
265  * XXX: This function is a kludge.  Drivers like vt(4) need to
266  * temporarily stop input when resizing, etc.  This should ideally be
267  * handled within the driver.
268  */
269 
270 void
271 terminal_mute(struct terminal *tm, int yes)
272 {
273 
274 	TERMINAL_LOCK(tm);
275 	if (yes)
276 		tm->tm_flags |= TF_MUTE;
277 	else
278 		tm->tm_flags &= ~TF_MUTE;
279 	TERMINAL_UNLOCK(tm);
280 }
281 
282 void
283 terminal_input_char(struct terminal *tm, term_char_t c)
284 {
285 	struct tty *tp;
286 
287 	tp = tm->tm_tty;
288 	if (tp == NULL)
289 		return;
290 
291 	/*
292 	 * Strip off any attributes. Also ignore input of second part of
293 	 * CJK fullwidth characters, as we don't want to return these
294 	 * characters twice.
295 	 */
296 	if (TCHAR_FORMAT(c) & TF_CJK_RIGHT)
297 		return;
298 	c = TCHAR_CHARACTER(c);
299 
300 	tty_lock(tp);
301 	/*
302 	 * Conversion to UTF-8.
303 	 */
304 	if (c < 0x80) {
305 		ttydisc_rint(tp, c, 0);
306 	} else if (c < 0x800) {
307 		char str[2] = {
308 			0xc0 | (c >> 6),
309 			0x80 | (c & 0x3f)
310 		};
311 
312 		ttydisc_rint_simple(tp, str, sizeof str);
313 	} else if (c < 0x10000) {
314 		char str[3] = {
315 			0xe0 | (c >> 12),
316 			0x80 | ((c >> 6) & 0x3f),
317 			0x80 | (c & 0x3f)
318 		};
319 
320 		ttydisc_rint_simple(tp, str, sizeof str);
321 	} else {
322 		char str[4] = {
323 			0xf0 | (c >> 18),
324 			0x80 | ((c >> 12) & 0x3f),
325 			0x80 | ((c >> 6) & 0x3f),
326 			0x80 | (c & 0x3f)
327 		};
328 
329 		ttydisc_rint_simple(tp, str, sizeof str);
330 	}
331 	ttydisc_rint_done(tp);
332 	tty_unlock(tp);
333 }
334 
335 void
336 terminal_input_raw(struct terminal *tm, char c)
337 {
338 	struct tty *tp;
339 
340 	tp = tm->tm_tty;
341 	if (tp == NULL)
342 		return;
343 
344 	tty_lock(tp);
345 	ttydisc_rint(tp, c, 0);
346 	ttydisc_rint_done(tp);
347 	tty_unlock(tp);
348 }
349 
350 void
351 terminal_input_special(struct terminal *tm, unsigned int k)
352 {
353 	struct tty *tp;
354 	const char *str;
355 
356 	tp = tm->tm_tty;
357 	if (tp == NULL)
358 		return;
359 
360 	str = teken_get_sequence(&tm->tm_emulator, k);
361 	if (str == NULL)
362 		return;
363 
364 	tty_lock(tp);
365 	ttydisc_rint_simple(tp, str, strlen(str));
366 	ttydisc_rint_done(tp);
367 	tty_unlock(tp);
368 }
369 
370 /*
371  * Binding with the TTY layer.
372  */
373 
374 static int
375 termtty_open(struct tty *tp)
376 {
377 	struct terminal *tm = tty_softc(tp);
378 
379 	tm->tm_class->tc_opened(tm, 1);
380 	return (0);
381 }
382 
383 static void
384 termtty_close(struct tty *tp)
385 {
386 	struct terminal *tm = tty_softc(tp);
387 
388 	tm->tm_class->tc_opened(tm, 0);
389 }
390 
391 static void
392 termtty_outwakeup(struct tty *tp)
393 {
394 	struct terminal *tm = tty_softc(tp);
395 	char obuf[128];
396 	size_t olen;
397 	unsigned int flags = 0;
398 
399 	while ((olen = ttydisc_getc(tp, obuf, sizeof obuf)) > 0) {
400 		TERMINAL_LOCK_TTY(tm);
401 		if (!(tm->tm_flags & TF_MUTE)) {
402 			tm->tm_flags &= ~TF_BELL;
403 			teken_input(&tm->tm_emulator, obuf, olen);
404 			flags |= tm->tm_flags;
405 		}
406 		TERMINAL_UNLOCK_TTY(tm);
407 	}
408 
409 	TERMINAL_LOCK_TTY(tm);
410 	if (!(tm->tm_flags & TF_MUTE))
411 		tm->tm_class->tc_done(tm);
412 	TERMINAL_UNLOCK_TTY(tm);
413 	if (flags & TF_BELL)
414 		tm->tm_class->tc_bell(tm);
415 }
416 
417 static int
418 termtty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
419 {
420 	struct terminal *tm = tty_softc(tp);
421 	int error;
422 
423 	switch (cmd) {
424 	case CONS_GETINFO: {
425 		vid_info_t *vi = (vid_info_t *)data;
426 		const teken_pos_t *p;
427 		int fg, bg;
428 
429 		if (vi->size != sizeof(vid_info_t))
430 			return (EINVAL);
431 
432 		/* Already help the console driver by filling in some data. */
433 		p = teken_get_cursor(&tm->tm_emulator);
434 		vi->mv_row = p->tp_row;
435 		vi->mv_col = p->tp_col;
436 
437 		p = teken_get_winsize(&tm->tm_emulator);
438 		vi->mv_rsz = p->tp_row;
439 		vi->mv_csz = p->tp_col;
440 
441 		teken_get_defattr_cons25(&tm->tm_emulator, &fg, &bg);
442 		vi->mv_norm.fore = fg;
443 		vi->mv_norm.back = bg;
444 		/* XXX: keep vidcontrol happy; bold backgrounds. */
445 		vi->mv_rev.fore = bg;
446 		vi->mv_rev.back = fg & 0x7;
447 		break;
448 	}
449 	}
450 
451 	/*
452 	 * Unlike various other drivers, this driver will never
453 	 * deallocate TTYs.  This means it's safe to temporarily unlock
454 	 * the TTY when handling ioctls.
455 	 */
456 	tty_unlock(tp);
457 	error = tm->tm_class->tc_ioctl(tm, cmd, data, td);
458 	tty_lock(tp);
459 	return (error);
460 }
461 
462 static int
463 termtty_mmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t * paddr,
464     int nprot, vm_memattr_t *memattr)
465 {
466 	struct terminal *tm = tty_softc(tp);
467 
468 	return (tm->tm_class->tc_mmap(tm, offset, paddr, nprot, memattr));
469 }
470 
471 /*
472  * Binding with the kernel and debug console.
473  */
474 
475 static cn_probe_t	termcn_cnprobe;
476 static cn_init_t	termcn_cninit;
477 static cn_term_t	termcn_cnterm;
478 static cn_getc_t	termcn_cngetc;
479 static cn_putc_t	termcn_cnputc;
480 static cn_grab_t	termcn_cngrab;
481 static cn_ungrab_t	termcn_cnungrab;
482 
483 const struct consdev_ops termcn_cnops = {
484 	.cn_probe	= termcn_cnprobe,
485 	.cn_init	= termcn_cninit,
486 	.cn_term	= termcn_cnterm,
487 	.cn_getc	= termcn_cngetc,
488 	.cn_putc	= termcn_cnputc,
489 	.cn_grab	= termcn_cngrab,
490 	.cn_ungrab	= termcn_cnungrab,
491 };
492 
493 void
494 termcn_cnregister(struct terminal *tm)
495 {
496 	struct consdev *cp;
497 
498 	cp = tm->consdev;
499 	if (cp == NULL) {
500 		cp = malloc(sizeof(struct consdev), M_TERMINAL,
501 		    M_WAITOK|M_ZERO);
502 		cp->cn_ops = &termcn_cnops;
503 		cp->cn_arg = tm;
504 		cp->cn_pri = CN_INTERNAL;
505 		sprintf(cp->cn_name, "ttyv0");
506 
507 		tm->tm_flags = TF_CONS;
508 		tm->consdev = cp;
509 
510 		terminal_init(tm);
511 	}
512 
513 	/* Attach terminal as console. */
514 	cnadd(cp);
515 }
516 
517 static void
518 termcn_cngrab(struct consdev *cp)
519 {
520 	struct terminal *tm = cp->cn_arg;
521 
522 	tm->tm_class->tc_cngrab(tm);
523 }
524 
525 static void
526 termcn_cnungrab(struct consdev *cp)
527 {
528 	struct terminal *tm = cp->cn_arg;
529 
530 	tm->tm_class->tc_cnungrab(tm);
531 }
532 
533 static void
534 termcn_cnprobe(struct consdev *cp)
535 {
536 	struct terminal *tm = cp->cn_arg;
537 
538 	if (tm == NULL) {
539 		cp->cn_pri = CN_DEAD;
540 		return;
541 	}
542 
543 	tm->consdev = cp;
544 	terminal_init(tm);
545 
546 	tm->tm_class->tc_cnprobe(tm, cp);
547 }
548 
549 static void
550 termcn_cninit(struct consdev *cp)
551 {
552 
553 }
554 
555 static void
556 termcn_cnterm(struct consdev *cp)
557 {
558 
559 }
560 
561 static int
562 termcn_cngetc(struct consdev *cp)
563 {
564 	struct terminal *tm = cp->cn_arg;
565 
566 	return (tm->tm_class->tc_cngetc(tm));
567 }
568 
569 static void
570 termcn_cnputc(struct consdev *cp, int c)
571 {
572 	struct terminal *tm = cp->cn_arg;
573 	teken_attr_t backup;
574 	char cv = c;
575 
576 	TERMINAL_LOCK_CONS(tm);
577 	if (!(tm->tm_flags & TF_MUTE)) {
578 		backup = *teken_get_curattr(&tm->tm_emulator);
579 		teken_set_curattr(&tm->tm_emulator, &kernel_message);
580 		teken_input(&tm->tm_emulator, &cv, 1);
581 		teken_set_curattr(&tm->tm_emulator, &backup);
582 		tm->tm_class->tc_done(tm);
583 	}
584 	TERMINAL_UNLOCK_CONS(tm);
585 }
586 
587 /*
588  * Binding with the terminal emulator.
589  */
590 
591 static void
592 termteken_bell(void *softc)
593 {
594 	struct terminal *tm = softc;
595 
596 	tm->tm_flags |= TF_BELL;
597 }
598 
599 static void
600 termteken_cursor(void *softc, const teken_pos_t *p)
601 {
602 	struct terminal *tm = softc;
603 
604 	tm->tm_class->tc_cursor(tm, p);
605 }
606 
607 static void
608 termteken_putchar(void *softc, const teken_pos_t *p, teken_char_t c,
609     const teken_attr_t *a)
610 {
611 	struct terminal *tm = softc;
612 
613 	tm->tm_class->tc_putchar(tm, p, TCHAR_CREATE(c, a));
614 }
615 
616 static void
617 termteken_fill(void *softc, const teken_rect_t *r, teken_char_t c,
618     const teken_attr_t *a)
619 {
620 	struct terminal *tm = softc;
621 
622 	tm->tm_class->tc_fill(tm, r, TCHAR_CREATE(c, a));
623 }
624 
625 static void
626 termteken_copy(void *softc, const teken_rect_t *r, const teken_pos_t *p)
627 {
628 	struct terminal *tm = softc;
629 
630 	tm->tm_class->tc_copy(tm, r, p);
631 }
632 
633 static void
634 termteken_pre_input(void *softc)
635 {
636 	struct terminal *tm = softc;
637 
638 	tm->tm_class->tc_pre_input(tm);
639 }
640 
641 static void
642 termteken_post_input(void *softc)
643 {
644 	struct terminal *tm = softc;
645 
646 	tm->tm_class->tc_post_input(tm);
647 }
648 
649 static void
650 termteken_param(void *softc, int cmd, unsigned int arg)
651 {
652 	struct terminal *tm = softc;
653 
654 	tm->tm_class->tc_param(tm, cmd, arg);
655 }
656 
657 static void
658 termteken_respond(void *softc, const void *buf, size_t len)
659 {
660 #if 0
661 	struct terminal *tm = softc;
662 	struct tty *tp;
663 
664 	/*
665 	 * Only inject a response into the TTY if the data actually
666 	 * originated from the TTY.
667 	 *
668 	 * XXX: This cannot be done right now.  The TTY could pick up
669 	 * other locks.  It could also in theory cause loops, when the
670 	 * TTY performs echoing of a command that generates even more
671 	 * input.
672 	 */
673 	tp = tm->tm_tty;
674 	if (tp == NULL)
675 		return;
676 
677 	ttydisc_rint_simple(tp, buf, len);
678 	ttydisc_rint_done(tp);
679 #endif
680 }
681