xref: /illumos-gate/usr/src/uts/common/io/vcons.c (revision 57c40785)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/signal.h>
30 #include <sys/cred.h>
31 #include <sys/vnode.h>
32 #include <sys/termios.h>
33 #include <sys/termio.h>
34 #include <sys/ttold.h>
35 #include <sys/stropts.h>
36 #include <sys/stream.h>
37 #include <sys/strsun.h>
38 #include <sys/tty.h>
39 #include <sys/buf.h>
40 #include <sys/uio.h>
41 #include <sys/stat.h>
42 #include <sys/sysmacros.h>
43 #include <sys/errno.h>
44 #include <sys/proc.h>
45 #include <sys/procset.h>
46 #include <sys/fault.h>
47 #include <sys/siginfo.h>
48 #include <sys/debug.h>
49 #include <sys/kd.h>
50 #include <sys/vt.h>
51 #include <sys/vtdaemon.h>
52 #include <sys/session.h>
53 #include <sys/door.h>
54 #include <sys/kmem.h>
55 #include <sys/cpuvar.h>
56 #include <sys/kbio.h>
57 #include <sys/strredir.h>
58 #include <sys/fs/snode.h>
59 #include <sys/consdev.h>
60 #include <sys/conf.h>
61 #include <sys/cmn_err.h>
62 #include <sys/console.h>
63 #include <sys/promif.h>
64 #include <sys/note.h>
65 #include <sys/polled_io.h>
66 #include <sys/systm.h>
67 #include <sys/ddi.h>
68 #include <sys/sunddi.h>
69 #include <sys/sunndi.h>
70 #include <sys/esunddi.h>
71 #include <sys/sunldi.h>
72 #include <sys/debug.h>
73 #include <sys/console.h>
74 #include <sys/ddi_impldefs.h>
75 #include <sys/policy.h>
76 #include <sys/tem.h>
77 #include <sys/wscons.h>
78 #include <sys/systm.h>
79 #include <sys/modctl.h>
80 #include <sys/vt_impl.h>
81 #include <sys/consconfig_dacf.h>
82 
83 /*
84  * This file belongs to wc STREAMS module which has a D_MTPERMODE
85  * inner perimeter. See "Locking Policy" comment in wscons.c for
86  * more information.
87  */
88 
89 /*
90  * Minor	name		device file		Hotkeys
91  *
92  * 0	the system console	/dev/console		Alt + F1
93  * 0:	virtual console #1	/dev/vt/0		Alt + F1
94  *
95  * 2:   virtual console #2	/dev/vt/2		Alt + F2
96  * 3:	virtual console #3	/dev/vt/3		Alt + F3
97  * ......
98  * n:	virtual console #n	/dev/vt/n		Alt + Fn
99  *
100  * Note that vtdaemon is running on /dev/vt/1 (minor=1),
101  * which is not available to end users.
102  *
103  */
104 
105 #define	VT_DAEMON_MINOR	1
106 #define	VT_IS_DAEMON(minor)	((minor) == VT_DAEMON_MINOR)
107 
108 extern void	wc_get_size(vc_state_t *pvc);
109 extern boolean_t consconfig_console_is_tipline(void);
110 
111 
112 minor_t vc_last_console = VT_MINOR_INVALID;	/* the last used console */
113 volatile uint_t	vc_target_console;		/* arg (1..n) */
114 
115 static volatile minor_t vc_inuse_max_minor = 0;
116 static list_t vc_waitactive_list;
117 _NOTE(SCHEME_PROTECTS_DATA("D_MTPERMOD protected data", vc_target_console))
118 _NOTE(SCHEME_PROTECTS_DATA("D_MTPERMOD protected data", vc_last_console))
119 _NOTE(SCHEME_PROTECTS_DATA("D_MTPERMOD protected data", vc_inuse_max_minor))
120 _NOTE(SCHEME_PROTECTS_DATA("D_MTPERMOD protected data", vc_waitactive_list))
121 
122 static int vt_pending_vtno = -1;
123 kmutex_t vt_pending_vtno_lock;
124 _NOTE(MUTEX_PROTECTS_DATA(vt_pending_vtno_lock, vt_pending_vtno))
125 
126 static int vt_activate(uint_t vt_no, cred_t *credp);
127 static void vt_copyout(queue_t *qp, mblk_t *mp, mblk_t *tmp, uint_t size);
128 static void vt_copyin(queue_t *qp, mblk_t *mp, uint_t size);
129 static void vt_iocnak(queue_t *qp, mblk_t *mp, int error);
130 static void vt_iocack(queue_t *qp, mblk_t *mp);
131 
132 static uint_t vt_minor2arg(minor_t minor);
133 static minor_t vt_arg2minor(uint_t arg);
134 
135 /*
136  * If the system console is directed to tipline, consider /dev/vt/0 as
137  * not being used.
138  * For other VT, if it is opened and tty is initialized, consider it
139  * as being used.
140  */
141 #define	VT_IS_INUSE(id)						\
142 	(((vt_minor2vc(id))->vc_flags & WCS_ISOPEN) &&		\
143 	((vt_minor2vc(id))->vc_flags & WCS_INIT) &&		\
144 	(id != 0 || !consconfig_console_is_tipline()))
145 
146 /*
147  * the vt switching message is encoded as:
148  *
149  *   -------------------------------------------------------------
150  *   |  \033  |  'Q'  |  vtno + 'A'  |  opcode  |  'z'  |  '\0'  |
151  *   -------------------------------------------------------------
152  */
153 #define	VT_MSG_SWITCH(mp)					\
154 	((int)((mp)->b_wptr - (mp)->b_rptr) >= 5 &&		\
155 	*((mp)->b_rptr) == '\033' &&				\
156 	*((mp)->b_rptr + 1) == 'Q' &&				\
157 	*((mp)->b_rptr + 4) == 'z')
158 
159 #define	VT_MSG_VTNO(mp)		(*((mp)->b_rptr + 2) - 'A')
160 #define	VT_MSG_OPCODE(mp)	(*((mp)->b_rptr + 3))
161 
162 #define	VT_DOORCALL_MAX_RETRY	3
163 
164 static void
165 vt_init_ttycommon(tty_common_t *pcommon)
166 {
167 	struct termios *termiosp;
168 	int len;
169 
170 	mutex_init(&pcommon->t_excl, NULL, MUTEX_DEFAULT, NULL);
171 	pcommon->t_iflag = 0;
172 
173 	/*
174 	 * Get the default termios settings (cflag).
175 	 * These are stored as a property in the
176 	 * "options" node.
177 	 */
178 	if (ddi_getlongprop(DDI_DEV_T_ANY,
179 	    ddi_root_node(), 0, "ttymodes",
180 	    (caddr_t)&termiosp, &len) == DDI_PROP_SUCCESS) {
181 
182 		if (len == sizeof (struct termios))
183 			pcommon->t_cflag = termiosp->c_cflag;
184 		else
185 			cmn_err(CE_WARN,
186 			    "wc: Couldn't get ttymodes property!");
187 
188 		kmem_free(termiosp, len);
189 	} else {
190 		/*
191 		 * Gack!  Whine about it.
192 		 */
193 		cmn_err(CE_WARN,
194 		    "wc: Couldn't get ttymodes property!");
195 	}
196 
197 	pcommon->t_iocpending = NULL;
198 }
199 
200 static int
201 vt_config(uint_t count)
202 {
203 	if (consmode != CONS_KFB)
204 		return (ENOTSUP);
205 
206 	/* one for system console, one for vtdaemon */
207 	if (count < 2)
208 		return (ENXIO);
209 
210 	/*
211 	 * Shouldn't allow to shrink the max vt minor to be smaller than
212 	 * the max in used minor.
213 	 */
214 	if (count <= vc_inuse_max_minor)
215 		return (EBUSY);
216 
217 	mutex_enter(&vc_lock);
218 	vt_resize(count);
219 	mutex_exit(&vc_lock);
220 
221 	return (0);
222 }
223 
224 void
225 vt_clean(queue_t *q, vc_state_t *pvc)
226 {
227 	ASSERT(MUTEX_HELD(&pvc->vc_state_lock));
228 
229 	if (pvc->vc_bufcallid != 0) {
230 		qunbufcall(q, pvc->vc_bufcallid);
231 		pvc->vc_bufcallid = 0;
232 	}
233 	if (pvc->vc_timeoutid != 0) {
234 		(void) quntimeout(q, pvc->vc_timeoutid);
235 		pvc->vc_timeoutid = 0;
236 	}
237 	ttycommon_close(&pvc->vc_ttycommon);
238 
239 	pvc->vc_flags &= ~WCS_INIT;
240 }
241 
242 /*
243  * Reply the VT_WAITACTIVE ioctl.
244  * Argument 'close' usage:
245  * B_TRUE:  the vt designated by argument 'minor' is being closed.
246  * B_FALSE: the vt designated by argument 'minor' has been activated just now.
247  */
248 static void
249 vc_waitactive_reply(int minor, boolean_t close)
250 {
251 	vc_waitactive_msg_t *index, *tmp;
252 	vc_state_t *pvc;
253 
254 	index = list_head(&vc_waitactive_list);
255 
256 	while (index != NULL) {
257 		tmp = index;
258 		index = list_next(&vc_waitactive_list, index);
259 
260 		if ((close && tmp->wa_msg_minor == minor) ||
261 		    (!close && tmp->wa_wait_minor == minor)) {
262 			list_remove(&vc_waitactive_list, tmp);
263 			pvc = vt_minor2vc(tmp->wa_msg_minor);
264 
265 			if (close)
266 				vt_iocnak(pvc->vc_wq, tmp->wa_mp, ENXIO);
267 			else
268 				vt_iocack(pvc->vc_wq, tmp->wa_mp);
269 
270 			kmem_free(tmp, sizeof (vc_waitactive_msg_t));
271 		}
272 	}
273 }
274 
275 void
276 vt_close(queue_t *q, vc_state_t *pvc, cred_t *credp)
277 {
278 	minor_t index;
279 
280 	mutex_enter(&pvc->vc_state_lock);
281 	vt_clean(q, pvc);
282 	pvc->vc_flags &= ~WCS_ISOPEN;
283 	mutex_exit(&pvc->vc_state_lock);
284 
285 	tem_destroy(pvc->vc_tem, credp);
286 	pvc->vc_tem = NULL;
287 
288 	index = pvc->vc_minor;
289 	if (index == vc_inuse_max_minor) {
290 		while ((--index > 0) && !VT_IS_INUSE(index))
291 			;
292 		vc_inuse_max_minor = index;
293 	}
294 
295 	vc_waitactive_reply(pvc->vc_minor, B_TRUE);
296 }
297 
298 static void
299 vt_init_tty(vc_state_t *pvc)
300 {
301 	ASSERT(MUTEX_HELD(&pvc->vc_state_lock));
302 
303 	pvc->vc_flags |= WCS_INIT;
304 	vt_init_ttycommon(&pvc->vc_ttycommon);
305 	wc_get_size(pvc);
306 }
307 
308 /*
309  * minor 0:	/dev/vt/0	(index = 0, indicating the system console)
310  * minor 1:	/dev/vt/1	(index = 1, vtdaemon special console)
311  * minor 2:	/dev/vt/2	(index = 2, virtual consoles)
312  * ......
313  * minor n:	/dev/vt/n	(index = n)
314  *
315  *
316  * The system console (minor 0), is opened firstly and used during console
317  * configuration.  It also acts as the system hard console even when all
318  * virtual consoles go off.
319  *
320  * In tipline case, minor 0 (/dev/vt/0) is reserved, and cannot be switched to.
321  * And the system console is redirected to the tipline. During normal cases,
322  * we can switch from virtual consoles to it by pressing 'Alt + F1'.
323  *
324  * minor 1 (/dev/vt/1) is reserved for vtdaemon special console, and it's
325  * not available to end users.
326  *
327  * During early console configuration, consconfig_dacf opens wscons and then
328  * issue a WC_OPEN_FB ioctl to kick off terminal init process. So during
329  * consconfig_dacf first opening of wscons, tems (of type tem_state_t) is
330  * not initialized. We do not initialize the tem_vt_state_t instance returned
331  * by tem_init() for this open, since we do not have enough info to handle
332  * normal terminal operation at this moment. This tem_vt_state_t instance
333  * will get initialized when handling WC_OPEN_FB.
334  */
335 int
336 vt_open(minor_t minor, queue_t *rq, cred_t *crp)
337 {
338 	vc_state_t *pvc;
339 
340 	if (!vt_minor_valid(minor))
341 		return (ENXIO);
342 
343 	pvc = vt_minor2vc(minor);
344 	if (pvc == NULL)
345 		return (ENXIO);
346 
347 	mutex_enter(&vc_lock);
348 	mutex_enter(&pvc->vc_state_lock);
349 
350 	if (!(pvc->vc_flags & WCS_ISOPEN)) {
351 		/*
352 		 * vc_tem might not be intialized if !tems.ts_initialized,
353 		 * and this only happens during console configuration.
354 		 */
355 		pvc->vc_tem = tem_init(crp);
356 	}
357 
358 	if (!(pvc->vc_flags & WCS_INIT))
359 		vt_init_tty(pvc);
360 
361 	/*
362 	 * In normal case, the first screen is the system console;
363 	 * In tipline case, the first screen is the first VT that gets started.
364 	 */
365 	if (vc_active_console == VT_MINOR_INVALID && minor != VT_DAEMON_MINOR)
366 		if (minor == 0 || consmode == CONS_KFB) {
367 			boolean_t unblank = B_FALSE;
368 
369 			vc_active_console = minor;
370 			vc_last_console = minor;
371 			if (minor != 0) {
372 				/*
373 				 * If we are not opening the system console
374 				 * as the first console, clear the phyical
375 				 * screen.
376 				 */
377 				unblank = B_TRUE;
378 			}
379 
380 			tem_activate(pvc->vc_tem, unblank, crp);
381 		}
382 
383 	if ((pvc->vc_ttycommon.t_flags & TS_XCLUDE) &&
384 	    (secpolicy_excl_open(crp) != 0)) {
385 		mutex_exit(&pvc->vc_state_lock);
386 		mutex_exit(&vc_lock);
387 		return (EBUSY);
388 	}
389 
390 	if (minor > vc_inuse_max_minor)
391 		vc_inuse_max_minor = minor;
392 
393 	pvc->vc_flags |= WCS_ISOPEN;
394 	pvc->vc_ttycommon.t_readq = rq;
395 	pvc->vc_ttycommon.t_writeq = WR(rq);
396 
397 	mutex_exit(&pvc->vc_state_lock);
398 	mutex_exit(&vc_lock);
399 
400 	rq->q_ptr = pvc;
401 	WR(rq)->q_ptr = pvc;
402 	pvc->vc_wq = WR(rq);
403 
404 	qprocson(rq);
405 	return (0);
406 }
407 
408 static minor_t
409 vt_find_prev(minor_t cur)
410 {
411 	minor_t i, t, max;
412 
413 	ASSERT(vc_active_console != VT_MINOR_INVALID);
414 
415 	max = VC_INSTANCES_COUNT;
416 
417 	for (i = cur - 1; (t = (i + max) % max) != cur; i--)
418 		if (!VT_IS_DAEMON(t) && VT_IS_INUSE(t))
419 			return (t);
420 
421 	return (VT_MINOR_INVALID);
422 }
423 
424 static minor_t
425 vt_find_next(minor_t cur)
426 {
427 	minor_t i, t, max;
428 
429 	ASSERT(vc_active_console != VT_MINOR_INVALID);
430 
431 	max = VC_INSTANCES_COUNT;
432 
433 	for (i = cur + 1; (t = (i + max) % max) != cur; i++)
434 		if (!VT_IS_DAEMON(t) && VT_IS_INUSE(t))
435 			return (t);
436 
437 	return (VT_MINOR_INVALID);
438 }
439 
440 /* ARGSUSED */
441 void
442 vt_send_hotkeys(void *timeout_arg)
443 {
444 	door_handle_t door;
445 	vt_cmd_arg_t arg;
446 	int error = 0;
447 	int retries = 0;
448 	door_arg_t door_arg;
449 
450 	mutex_enter(&vt_pending_vtno_lock);
451 
452 	arg.vt_ev = VT_EV_HOTKEYS;
453 	arg.vt_num = vt_pending_vtno;
454 
455 	/* only available in kernel context or user context */
456 	if (door_ki_open(VT_DAEMON_DOOR_FILE, &door) != 0) {
457 		vt_pending_vtno = -1;
458 		mutex_exit(&vt_pending_vtno_lock);
459 		return;
460 	}
461 
462 	door_arg.rbuf = NULL;
463 	door_arg.rsize = 0;
464 	door_arg.data_ptr = (void *)&arg;
465 	door_arg.data_size = sizeof (arg);
466 	door_arg.desc_ptr = NULL;
467 	door_arg.desc_num = 0;
468 
469 	/*
470 	 * Make door upcall
471 	 */
472 	while ((error = door_ki_upcall(door, &door_arg)) != 0 &&
473 	    retries < VT_DOORCALL_MAX_RETRY)
474 		if (error == EAGAIN || error == EINTR)
475 			retries++;
476 		else
477 			break;
478 
479 	door_ki_rele(door);
480 
481 	vt_pending_vtno = -1;
482 
483 	mutex_exit(&vt_pending_vtno_lock);
484 }
485 
486 static boolean_t
487 vt_validate_hotkeys(int minor)
488 {
489 	/*
490 	 * minor should not succeed the existing minor numbers range.
491 	 */
492 	if (!vt_minor_valid(minor))
493 		return (B_FALSE);
494 
495 	/*
496 	 * Shouldn't switch to /dev/vt/1 or an unused vt.
497 	 */
498 	if (!VT_IS_DAEMON(minor) && VT_IS_INUSE(minor))
499 		return (B_TRUE);
500 
501 	return (B_FALSE);
502 }
503 
504 static void
505 vt_trigger_hotkeys(int vtno)
506 {
507 	mutex_enter(&vt_pending_vtno_lock);
508 
509 	if (vt_pending_vtno != -1) {
510 		mutex_exit(&vt_pending_vtno_lock);
511 		return;
512 	}
513 
514 	vt_pending_vtno = vtno;
515 	mutex_exit(&vt_pending_vtno_lock);
516 	(void) timeout(vt_send_hotkeys, NULL, 1);
517 }
518 
519 /*
520  * return value:
521  *    0:    non msg of vt hotkeys
522  *    1:    msg of vt hotkeys
523  */
524 int
525 vt_check_hotkeys(mblk_t *mp)
526 {
527 	int vtno = 0;
528 	minor_t minor = 0;
529 
530 	/* LINTED E_PTRDIFF_OVERFLOW */
531 	if (!VT_MSG_SWITCH(mp))
532 		return (0);
533 
534 	switch (VT_MSG_OPCODE(mp)) {
535 	case 'B':
536 		/* find out the previous vt */
537 		if (vc_active_console == VT_MINOR_INVALID)
538 			return (1);
539 
540 		if (VT_IS_DAEMON(vc_active_console)) {
541 			minor = vt_find_prev(vt_arg2minor(vc_target_console));
542 			break;
543 		}
544 
545 		minor = vt_find_prev(vc_active_console);
546 		break;
547 	case 'F':
548 		/* find out the next vt */
549 		if (vc_active_console == VT_MINOR_INVALID)
550 			return (1);
551 
552 		if (VT_IS_DAEMON(vc_active_console)) {
553 			minor = vt_find_next(vt_arg2minor(vc_target_console));
554 			break;
555 		}
556 
557 		minor = vt_find_next(vc_active_console);
558 		break;
559 	case 'H':
560 		/* find out the specified vt */
561 		minor = VT_MSG_VTNO(mp);
562 
563 		/* check for system console, Alt + F1 */
564 		if (minor == 1)
565 			minor = 0;
566 		break;
567 	case 'L':
568 		/* find out the last vt */
569 		if ((minor = vc_last_console) == VT_MINOR_INVALID)
570 			return (1);
571 		break;
572 	default:
573 		return (1);
574 	}
575 
576 	if (!vt_validate_hotkeys(minor))
577 		return (1);
578 
579 	/*
580 	 * for system console, the argument of vtno for
581 	 * vt_activate is 1, though its minor is 0
582 	 */
583 	if (minor == 0)
584 		vtno = 1;	/* for system console */
585 	else
586 		vtno = minor;
587 
588 	vt_trigger_hotkeys(vtno);
589 	return (1);
590 }
591 
592 static void
593 vt_proc_sendsig(pid_t pid, int sig)
594 {
595 	register proc_t *p;
596 
597 	if (pid <= 0)
598 		return;
599 
600 	mutex_enter(&pidlock);
601 	if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) {
602 		mutex_exit(&pidlock);
603 		return;
604 	}
605 
606 	psignal(p, sig);
607 	mutex_exit(&pidlock);
608 }
609 
610 static int
611 vt_proc_exists(pid_t pid)
612 {
613 	register proc_t *p;
614 
615 	if (pid <= 0)
616 		return (EINVAL);
617 
618 	mutex_enter(&pidlock);
619 	if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) {
620 		mutex_exit(&pidlock);
621 		return (ESRCH);
622 	}
623 	mutex_exit(&pidlock);
624 
625 	return (0);
626 }
627 
628 #define	SIG_VALID(x)	(((x) > 0) && ((x) < _SIGRTMAX) && \
629 			((x) != SIGKILL) && ((x) != SIGSTOP))
630 
631 static int
632 vt_setmode(vc_state_t *pvc, struct vt_mode *pmode)
633 {
634 	if ((pmode->mode != VT_PROCESS) && (pmode->mode != VT_AUTO))
635 		return (EINVAL);
636 
637 	if (!SIG_VALID(pmode->relsig) || !SIG_VALID(pmode->acqsig))
638 		return (EINVAL);
639 
640 	if (pmode->mode == VT_PROCESS) {
641 		pvc->vc_pid = curproc->p_pid;
642 	} else {
643 		pvc->vc_dispnum = 0;
644 		pvc->vc_login = 0;
645 	}
646 
647 	pvc->vc_switch_mode = pmode->mode;
648 	pvc->vc_waitv = pmode->waitv;
649 	pvc->vc_relsig = pmode->relsig;
650 	pvc->vc_acqsig = pmode->acqsig;
651 
652 	return (0);
653 }
654 
655 static void
656 vt_reset(vc_state_t *pvc)
657 {
658 	pvc->vc_switch_mode = VT_AUTO;
659 	pvc->vc_pid = -1;
660 	pvc->vc_dispnum = 0;
661 	pvc->vc_login = 0;
662 	pvc->vc_switchto = VT_MINOR_INVALID;
663 }
664 
665 /*
666  * switch to vt_no from vc_active_console
667  */
668 static void
669 vt_switch(uint_t vt_no, cred_t *credp)
670 {
671 	vc_state_t *pvc_active = vt_minor2vc(vc_active_console);
672 	vc_state_t *pvc = vt_minor2vc(vt_no);
673 	minor_t index;
674 
675 	ASSERT(pvc_active && pvc);
676 
677 	mutex_enter(&vc_lock);
678 
679 	tem_switch(pvc_active->vc_tem, pvc->vc_tem, credp);
680 
681 	if (!VT_IS_DAEMON(vc_active_console))
682 		vc_last_console = vc_active_console;
683 	else
684 		vc_last_console = vt_arg2minor(vc_target_console);
685 
686 	vc_active_console = pvc->vc_minor;
687 
688 	if (pvc->vc_switch_mode == VT_PROCESS) {
689 		pvc->vc_switchto = pvc->vc_minor;
690 
691 		/* send it an acquired signal */
692 		vt_proc_sendsig(pvc->vc_pid, pvc->vc_acqsig);
693 	}
694 
695 	vc_waitactive_reply(vc_active_console, B_FALSE);
696 
697 	mutex_exit(&vc_lock);
698 
699 	if (!VT_IS_DAEMON(vt_no)) {
700 		/*
701 		 * Applications that open the virtual console device may request
702 		 * asynchronous notification of VT switching from a previous VT
703 		 * to another one by setting the S_MSG flag in an I_SETSIG
704 		 * STREAMS ioctl. Such processes receive a SIGPOLL signal when
705 		 * a VT switching succeeds.
706 		 */
707 		for (index = 0; index < VC_INSTANCES_COUNT; index++) {
708 			vc_state_t *tmp_pvc = vt_minor2vc(index);
709 			mblk_t *mp;
710 
711 			if ((tmp_pvc->vc_flags & WCS_ISOPEN) &&
712 			    (tmp_pvc->vc_flags & WCS_INIT) &&
713 			    (mp = allocb(sizeof (unsigned char), BPRI_HI))) {
714 				mp->b_datap->db_type = M_PCSIG;
715 				*mp->b_wptr = SIGPOLL;
716 				mp->b_wptr += sizeof (unsigned char);
717 				putnext(RD(tmp_pvc->vc_wq), mp);
718 			}
719 		}
720 	}
721 
722 }
723 
724 /*
725  * vt_no	from 0 to n
726  *
727  * 0	for the vtdaemon sepcial console (only vtdaemon will use it)
728  * 1    for the system console (Alt + F1, or Alt + Ctrl + F1),
729  *      aka Virtual Console #1
730  *
731  * 2    for Virtual Console #2
732  * n    for Virtual Console #n
733  */
734 static minor_t
735 vt_arg2minor(uint_t arg)
736 {
737 	if (arg == 0)
738 		return (1);
739 
740 	if (arg == 1)
741 		return (0);
742 
743 	return (arg);
744 }
745 
746 static uint_t
747 vt_minor2arg(minor_t minor)
748 {
749 	if (minor == 0)
750 		return (1);
751 
752 	if (VT_IS_DAEMON(minor)) {
753 		/* here it should be the real console */
754 		return (vc_target_console);
755 	}
756 
757 	return (minor);
758 }
759 
760 static int
761 vt_activate(uint_t vt_no, cred_t *credp)
762 {
763 	vc_state_t *pvc;
764 	minor_t minor;
765 
766 	minor = vt_arg2minor(vt_no);
767 	if (!vt_minor_valid(minor))
768 		return (ENXIO);
769 	if (minor == vc_active_console) {
770 		if (VT_IS_DAEMON(minor)) {
771 			/*
772 			 * vtdaemon is reactivating itself to do locking
773 			 * on behalf of another console, so record current
774 			 * target console as the last console.
775 			 */
776 			vc_last_console = vt_arg2minor(vc_target_console);
777 		}
778 
779 		return (0);
780 	}
781 
782 	/*
783 	 * In tipline case, the system console is redirected to tipline
784 	 * and thus is always available.
785 	 */
786 	if (minor == 0 && consconfig_console_is_tipline())
787 		return (0);
788 
789 	if (!VT_IS_INUSE(minor))
790 		return (ENXIO);
791 
792 	pvc = vt_minor2vc(minor);
793 	if (pvc == NULL)
794 		return (ENXIO);
795 	if (pvc->vc_tem == NULL)
796 		return (ENXIO);
797 
798 	pvc = vt_minor2vc(vc_active_console);
799 	if (pvc == NULL)
800 		return (ENXIO);
801 	if (pvc->vc_switch_mode != VT_PROCESS) {
802 		vt_switch(minor, credp);
803 		return (0);
804 	}
805 
806 	/*
807 	 * Validate the process, reset the
808 	 * vt to auto mode if failed.
809 	 */
810 	if (pvc->vc_pid == -1 || vt_proc_exists(pvc->vc_pid) != 0) {
811 		/*
812 		 * Xserver has not started up yet,
813 		 * or it dose not exist.
814 		 */
815 		vt_reset(pvc);
816 		return (0);
817 	}
818 
819 	/*
820 	 * Send the release signal to the process,
821 	 * and wait VT_RELDISP ioctl from Xserver
822 	 * after its leaving VT.
823 	 */
824 	vt_proc_sendsig(pvc->vc_pid, pvc->vc_relsig);
825 	pvc->vc_switchto = minor;
826 
827 	/*
828 	 * We don't need a timeout here, for if Xserver refuses
829 	 * or fails to respond to release signal using VT_RELDISP,
830 	 * we cannot successfully switch to our text mode. Actually
831 	 * users can try again. At present we don't support force
832 	 * switch.
833 	 */
834 	return (0);
835 }
836 
837 static int
838 vt_reldisp(vc_state_t *pvc, int arg, cred_t *credp)
839 {
840 	minor_t target_vtno = pvc->vc_switchto;
841 
842 	if ((pvc->vc_switch_mode != VT_PROCESS) ||
843 	    (pvc->vc_minor != vc_active_console))
844 		return (EACCES);
845 
846 	if (target_vtno == VT_MINOR_INVALID)
847 		return (EINVAL);
848 
849 	pvc->vc_switchto = VT_MINOR_INVALID;
850 
851 	if (arg == VT_ACKACQ)
852 		return (0);
853 
854 	if (arg == 0)
855 		return (0); /* refuse to release */
856 
857 	/* Xserver has left VT */
858 	vt_switch(target_vtno, credp);
859 	return (0);
860 }
861 
862 void
863 vt_ioctl(queue_t *q, mblk_t *mp)
864 {
865 	vc_state_t *pvc = (vc_state_t *)q->q_ptr;
866 	struct iocblk	*iocp;
867 	struct vt_mode vtmode;
868 	struct vt_stat vtinfo;
869 	struct vt_dispinfo vtdisp;
870 	mblk_t *tmp;
871 	int minor;
872 	int arg;
873 	int error = 0;
874 	vc_waitactive_msg_t *wait_msg;
875 
876 	iocp = (struct iocblk *)(void *)mp->b_rptr;
877 	if (consmode != CONS_KFB && iocp->ioc_cmd != VT_ENABLED) {
878 		vt_iocnak(q, mp, EINVAL);
879 		return;
880 	}
881 
882 	switch (iocp->ioc_cmd) {
883 	case VT_ENABLED:
884 		if (!(tmp = allocb(sizeof (int), BPRI_MED))) {
885 			error = ENOMEM;
886 			break;
887 		}
888 		*(int *)(void *)tmp->b_rptr = consmode;
889 		tmp->b_wptr += sizeof (int);
890 		vt_copyout(q, mp, tmp, sizeof (int));
891 		return;
892 
893 	case KDSETMODE:
894 		arg = *(intptr_t *)(void *)mp->b_cont->b_rptr;
895 		if (arg != KD_TEXT && arg != KD_GRAPHICS) {
896 			error = EINVAL;
897 			break;
898 		}
899 		if (tem_get_fbmode(pvc->vc_tem) == arg)
900 			break;
901 
902 		tem_set_fbmode(pvc->vc_tem, (uchar_t)arg, iocp->ioc_cr);
903 
904 		break;
905 
906 	case KDGETMODE:
907 		if (!(tmp = allocb(sizeof (int), BPRI_MED))) {
908 			error = ENOMEM;
909 			break;
910 		}
911 		*(int *)(void *)tmp->b_rptr = tem_get_fbmode(pvc->vc_tem);
912 		tmp->b_wptr += sizeof (int);
913 		vt_copyout(q, mp, tmp, sizeof (int));
914 		return;
915 
916 	case VT_OPENQRY: /* return number of first free VT */
917 		if (!(tmp = allocb(sizeof (int), BPRI_MED))) {
918 			error = ENOMEM;
919 			break;
920 		}
921 
922 		/* minors of 0 and 1 are not available to end users */
923 		for (minor = 2; vt_minor_valid(minor); minor++)
924 			if (!VT_IS_INUSE(minor))
925 				break;
926 
927 		if (!vt_minor_valid(minor))
928 			minor = -1;
929 		*(int *)(void *)tmp->b_rptr = minor; /* /dev/vt/minor */
930 		tmp->b_wptr += sizeof (int);
931 		vt_copyout(q, mp, tmp, sizeof (int));
932 		return;
933 
934 	case VT_GETMODE:
935 		vtmode.mode = pvc->vc_switch_mode;
936 		vtmode.waitv = pvc->vc_waitv;
937 		vtmode.relsig = pvc->vc_relsig;
938 		vtmode.acqsig = pvc->vc_acqsig;
939 		vtmode.frsig = 0;
940 		if (!(tmp = allocb(sizeof (struct vt_mode), BPRI_MED))) {
941 			error = ENOMEM;
942 			break;
943 		}
944 		*(struct vt_mode *)(void *)tmp->b_rptr = vtmode;
945 		tmp->b_wptr += sizeof (struct vt_mode);
946 		vt_copyout(q, mp, tmp, sizeof (struct vt_mode));
947 		return;
948 
949 	case VT_SETMODE:
950 		vt_copyin(q, mp, sizeof (struct vt_mode));
951 		return;
952 
953 	case VT_SETDISPINFO:
954 		/* always enforce sys_devices privilege for setdispinfo */
955 		if ((error = secpolicy_console(iocp->ioc_cr)) != 0)
956 			break;
957 
958 		pvc->vc_dispnum = *(intptr_t *)(void *)mp->b_cont->b_rptr;
959 		break;
960 
961 	case VT_SETDISPLOGIN:
962 		pvc->vc_login = *(intptr_t *)(void *)mp->b_cont->b_rptr;
963 		break;
964 
965 	case VT_GETDISPINFO:
966 		vtdisp.v_pid = pvc->vc_pid;
967 		vtdisp.v_dispnum = pvc->vc_dispnum;
968 		vtdisp.v_login = pvc->vc_login;
969 		if (!(tmp = allocb(sizeof (struct vt_dispinfo), BPRI_MED))) {
970 			error = ENOMEM;
971 			break;
972 		}
973 		*(struct vt_dispinfo *)(void *)tmp->b_rptr = vtdisp;
974 		tmp->b_wptr += sizeof (struct vt_dispinfo);
975 		vt_copyout(q, mp, tmp, sizeof (struct vt_dispinfo));
976 		return;
977 
978 	case VT_RELDISP:
979 		arg = *(intptr_t *)(void *)mp->b_cont->b_rptr;
980 		error = vt_reldisp(pvc, arg, iocp->ioc_cr);
981 		break;
982 
983 	case VT_CONFIG:
984 		/* always enforce sys_devices privilege for config */
985 		if ((error = secpolicy_console(iocp->ioc_cr)) != 0)
986 			break;
987 
988 		arg = *(intptr_t *)(void *)mp->b_cont->b_rptr;
989 		error = vt_config(arg);
990 		break;
991 
992 	case VT_ACTIVATE:
993 		/* always enforce sys_devices privilege for secure switch */
994 		if ((error = secpolicy_console(iocp->ioc_cr)) != 0)
995 			break;
996 
997 		arg = *(intptr_t *)(void *)mp->b_cont->b_rptr;
998 		error = vt_activate(arg, iocp->ioc_cr);
999 		break;
1000 
1001 	case VT_WAITACTIVE:
1002 		arg = *(intptr_t *)(void *)mp->b_cont->b_rptr;
1003 		arg = vt_arg2minor(arg);
1004 		if (!vt_minor_valid(arg)) {
1005 			error = ENXIO;
1006 			break;
1007 		}
1008 		if (arg == vc_active_console)
1009 			break;
1010 
1011 		wait_msg = kmem_zalloc(sizeof (vc_waitactive_msg_t),
1012 		    KM_NOSLEEP);
1013 		if (wait_msg == NULL) {
1014 			error = ENXIO;
1015 			break;
1016 		}
1017 
1018 		wait_msg->wa_mp = mp;
1019 		wait_msg->wa_msg_minor = pvc->vc_minor;
1020 		wait_msg->wa_wait_minor = arg;
1021 		list_insert_head(&vc_waitactive_list, wait_msg);
1022 
1023 		return;
1024 
1025 	case VT_GETSTATE:
1026 		/*
1027 		 * Here v_active is the argument for vt_activate,
1028 		 * not minor.
1029 		 */
1030 		vtinfo.v_active = vt_minor2arg(vc_active_console);
1031 		vtinfo.v_state = 3;	/* system console and vtdaemon */
1032 
1033 		/* we only support 16 vt states since the v_state is short */
1034 		for (minor = 2; minor < 16; minor++) {
1035 			pvc = vt_minor2vc(minor);
1036 			if (pvc == NULL)
1037 				break;
1038 			if (VT_IS_INUSE(minor))
1039 				vtinfo.v_state |= (1 << pvc->vc_minor);
1040 		}
1041 
1042 		if (!(tmp = allocb(sizeof (struct vt_stat), BPRI_MED))) {
1043 			error = ENOMEM;
1044 			break;
1045 		}
1046 		*(struct vt_stat *)(void *)tmp->b_rptr = vtinfo;
1047 		tmp->b_wptr += sizeof (struct vt_stat);
1048 		vt_copyout(q, mp, tmp, sizeof (struct vt_stat));
1049 		return;
1050 
1051 	case VT_SET_TARGET:
1052 		/* always enforce sys_devices privilege */
1053 		if ((error = secpolicy_console(iocp->ioc_cr)) != 0)
1054 			break;
1055 
1056 		arg = *(intptr_t *)(void *)mp->b_cont->b_rptr;
1057 
1058 		/* vtdaemon is doing authentication for this target console */
1059 		vc_target_console = arg;
1060 		break;
1061 
1062 	case VT_GETACTIVE:	/* get real active console (minor) */
1063 		if (!(tmp = allocb(sizeof (int), BPRI_MED))) {
1064 			error = ENOMEM;
1065 			break;
1066 		}
1067 		*(int *)(void *)tmp->b_rptr = vc_active_console;
1068 		tmp->b_wptr += sizeof (int);
1069 		vt_copyout(q, mp, tmp, sizeof (int));
1070 		return;
1071 
1072 	default:
1073 		error = ENXIO;
1074 		break;
1075 	}
1076 
1077 	if (error != 0)
1078 		vt_iocnak(q, mp, error);
1079 	else
1080 		vt_iocack(q, mp);
1081 }
1082 
1083 void
1084 vt_miocdata(queue_t *qp, mblk_t *mp)
1085 {
1086 	vc_state_t *pvc = (vc_state_t *)qp->q_ptr;
1087 	struct copyresp *copyresp;
1088 	struct vt_mode *pmode;
1089 	int error = 0;
1090 
1091 	copyresp = (struct copyresp *)(void *)mp->b_rptr;
1092 	if (copyresp->cp_rval) {
1093 		vt_iocnak(qp, mp, EAGAIN);
1094 		return;
1095 	}
1096 
1097 	switch (copyresp->cp_cmd) {
1098 	case VT_SETMODE:
1099 		pmode = (struct vt_mode *)(void *)mp->b_cont->b_rptr;
1100 		error = vt_setmode(pvc, pmode);
1101 		break;
1102 
1103 	case KDGETMODE:
1104 	case VT_OPENQRY:
1105 	case VT_GETMODE:
1106 	case VT_GETDISPINFO:
1107 	case VT_GETSTATE:
1108 	case VT_ENABLED:
1109 	case VT_GETACTIVE:
1110 		break;
1111 
1112 	default:
1113 		error = ENXIO;
1114 		break;
1115 	}
1116 
1117 	if (error != 0)
1118 		vt_iocnak(qp, mp, error);
1119 	else
1120 		vt_iocack(qp, mp);
1121 }
1122 
1123 static void
1124 vt_iocack(queue_t *qp, mblk_t *mp)
1125 {
1126 	struct iocblk	*iocbp = (struct iocblk *)(void *)mp->b_rptr;
1127 
1128 	mp->b_datap->db_type = M_IOCACK;
1129 	mp->b_wptr = mp->b_rptr + sizeof (struct iocblk);
1130 	iocbp->ioc_error = 0;
1131 	iocbp->ioc_count = 0;
1132 	iocbp->ioc_rval = 0;
1133 	if (mp->b_cont != NULL) {
1134 		freemsg(mp->b_cont);
1135 		mp->b_cont = NULL;
1136 	}
1137 	qreply(qp, mp);
1138 }
1139 
1140 static void
1141 vt_iocnak(queue_t *qp, mblk_t *mp, int error)
1142 {
1143 	struct iocblk *iocp = (struct iocblk *)(void *)mp->b_rptr;
1144 
1145 	mp->b_datap->db_type = M_IOCNAK;
1146 	iocp->ioc_rval = 0;
1147 	iocp->ioc_count = 0;
1148 	iocp->ioc_error = error;
1149 	if (mp->b_cont != NULL) {
1150 		freemsg(mp->b_cont);
1151 		mp->b_cont = NULL;
1152 	}
1153 	qreply(qp, mp);
1154 }
1155 
1156 static void
1157 vt_copyin(queue_t *qp, mblk_t *mp, uint_t size)
1158 {
1159 	struct copyreq  *cqp;
1160 
1161 	cqp = (struct copyreq *)(void *)mp->b_rptr;
1162 	cqp->cq_addr = *((caddr_t *)(void *)mp->b_cont->b_rptr);
1163 	cqp->cq_size = size;
1164 	cqp->cq_flag = 0;
1165 	cqp->cq_private = (mblk_t *)NULL;
1166 	mp->b_wptr = mp->b_rptr + sizeof (struct copyreq);
1167 	mp->b_datap->db_type = M_COPYIN;
1168 	if (mp->b_cont)
1169 		freemsg(mp->b_cont);
1170 	mp->b_cont = (mblk_t *)NULL;
1171 	qreply(qp, mp);
1172 }
1173 
1174 static void
1175 vt_copyout(queue_t *qp, mblk_t *mp, mblk_t *tmp, uint_t size)
1176 {
1177 	struct copyreq  *cqp;
1178 
1179 	cqp = (struct copyreq *)(void *)mp->b_rptr;
1180 	cqp->cq_size = size;
1181 	cqp->cq_addr = *((caddr_t *)(void *)mp->b_cont->b_rptr);
1182 	cqp->cq_flag = 0;
1183 	cqp->cq_private = (mblk_t *)NULL;
1184 	mp->b_wptr = mp->b_rptr + sizeof (struct copyreq);
1185 	mp->b_datap->db_type = M_COPYOUT;
1186 	if (mp->b_cont)
1187 		freemsg(mp->b_cont);
1188 	mp->b_cont = tmp;
1189 	qreply(qp, mp);
1190 }
1191 
1192 /*
1193  * Get vc state from minor.
1194  * Once a caller gets a vc_state_t from this function,
1195  * the vc_state_t is guaranteed not being freed before
1196  * the caller leaves this STREAMS module by the D_MTPERMOD
1197  * perimeter.
1198  */
1199 vc_state_t *
1200 vt_minor2vc(minor_t minor)
1201 {
1202 	avl_index_t where;
1203 	vc_state_t target;
1204 
1205 	if (minor != VT_ACTIVE) {
1206 		target.vc_minor = minor;
1207 		return (avl_find(&vc_avl_root, &target, &where));
1208 	}
1209 
1210 	if (vc_active_console == VT_MINOR_INVALID)
1211 		target.vc_minor = 0;
1212 	else
1213 		target.vc_minor = vc_active_console;
1214 
1215 	return (avl_find(&vc_avl_root, &target, &where));
1216 }
1217 
1218 static void
1219 vt_state_init(vc_state_t *vcptr, minor_t minor)
1220 {
1221 	mutex_init(&vcptr->vc_state_lock, NULL, MUTEX_DRIVER, NULL);
1222 
1223 	mutex_enter(&vcptr->vc_state_lock);
1224 	vcptr->vc_flags = 0;
1225 	mutex_exit(&vcptr->vc_state_lock);
1226 
1227 	vcptr->vc_pid = -1;
1228 	vcptr->vc_dispnum = 0;
1229 	vcptr->vc_login = 0;
1230 	vcptr->vc_switchto = VT_MINOR_INVALID;
1231 	vcptr->vc_switch_mode = VT_AUTO;
1232 	vcptr->vc_relsig = SIGUSR1;
1233 	vcptr->vc_acqsig = SIGUSR1;
1234 	vcptr->vc_tem = NULL;
1235 	vcptr->vc_bufcallid = 0;
1236 	vcptr->vc_timeoutid = 0;
1237 	vcptr->vc_wq = NULL;
1238 	vcptr->vc_minor = minor;
1239 }
1240 
1241 void
1242 vt_resize(uint_t count)
1243 {
1244 	uint_t vc_num, i;
1245 
1246 	ASSERT(MUTEX_HELD(&vc_lock));
1247 
1248 	vc_num = VC_INSTANCES_COUNT;
1249 
1250 	if (count == vc_num)
1251 		return;
1252 
1253 	if (count > vc_num) {
1254 		for (i = vc_num; i < count; i++) {
1255 			vc_state_t *vcptr = kmem_zalloc(sizeof (vc_state_t),
1256 			    KM_SLEEP);
1257 			vt_state_init(vcptr, i);
1258 			avl_add(&vc_avl_root, vcptr);
1259 		}
1260 		return;
1261 	}
1262 
1263 	for (i = vc_num; i > count; i--) {
1264 		avl_index_t where;
1265 		vc_state_t target, *found;
1266 
1267 		target.vc_minor = i - 1;
1268 		found = avl_find(&vc_avl_root, &target, &where);
1269 		ASSERT(found != NULL && found->vc_flags == 0);
1270 		avl_remove(&vc_avl_root, found);
1271 		kmem_free(found, sizeof (vc_state_t));
1272 	}
1273 }
1274 
1275 static int
1276 vc_avl_compare(const void *first, const void *second)
1277 {
1278 	const vc_state_t *vcptr1 = first;
1279 	const vc_state_t *vcptr2 = second;
1280 
1281 	if (vcptr1->vc_minor < vcptr2->vc_minor)
1282 		return (-1);
1283 
1284 	if (vcptr1->vc_minor == vcptr2->vc_minor)
1285 		return (0);
1286 
1287 	return (1);
1288 }
1289 
1290 /*
1291  * Only called from wc init().
1292  */
1293 void
1294 vt_init(void)
1295 {
1296 #ifdef	__lock_lint
1297 	ASSERT(NO_COMPETING_THREADS);
1298 #endif
1299 
1300 	avl_create(&vc_avl_root, vc_avl_compare, sizeof (vc_state_t),
1301 	    offsetof(vc_state_t, vc_avl_node));
1302 
1303 	list_create(&vc_waitactive_list, sizeof (vc_waitactive_msg_t),
1304 	    offsetof(vc_waitactive_msg_t, wa_list_node));
1305 
1306 	mutex_init(&vc_lock, NULL, MUTEX_DRIVER, NULL);
1307 	mutex_init(&vt_pending_vtno_lock, NULL, MUTEX_DRIVER, NULL);
1308 }
1309