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