1
2
3
4
5
6
7
8
9
10
11
12#include <linux/types.h>
13#include <linux/errno.h>
14#include <linux/sched/signal.h>
15#include <linux/tty.h>
16#include <linux/timer.h>
17#include <linux/kernel.h>
18#include <linux/compat.h>
19#include <linux/module.h>
20#include <linux/kd.h>
21#include <linux/vt.h>
22#include <linux/string.h>
23#include <linux/slab.h>
24#include <linux/major.h>
25#include <linux/fs.h>
26#include <linux/console.h>
27#include <linux/consolemap.h>
28#include <linux/signal.h>
29#include <linux/suspend.h>
30#include <linux/timex.h>
31
32#include <asm/io.h>
33#include <linux/uaccess.h>
34
35#include <linux/nospec.h>
36
37#include <linux/kbd_kern.h>
38#include <linux/vt_kern.h>
39#include <linux/kbd_diacr.h>
40#include <linux/selection.h>
41
42bool vt_dont_switch;
43
44static inline bool vt_in_use(unsigned int i)
45{
46 const struct vc_data *vc = vc_cons[i].d;
47
48
49
50
51
52 WARN_CONSOLE_UNLOCKED();
53
54 return vc && kref_read(&vc->port.kref) > 1;
55}
56
57static inline bool vt_busy(int i)
58{
59 if (vt_in_use(i))
60 return true;
61 if (i == fg_console)
62 return true;
63 if (vc_is_sel(vc_cons[i].d))
64 return true;
65
66 return false;
67}
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82#ifdef CONFIG_X86
83#include <asm/syscalls.h>
84#endif
85
86static void complete_change_console(struct vc_data *vc);
87
88
89
90
91
92struct vt_event_wait {
93 struct list_head list;
94 struct vt_event event;
95 int done;
96};
97
98static LIST_HEAD(vt_events);
99static DEFINE_SPINLOCK(vt_event_lock);
100static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
101
102
103
104
105
106
107
108
109
110
111void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
112{
113 struct list_head *pos, *head;
114 unsigned long flags;
115 int wake = 0;
116
117 spin_lock_irqsave(&vt_event_lock, flags);
118 head = &vt_events;
119
120 list_for_each(pos, head) {
121 struct vt_event_wait *ve = list_entry(pos,
122 struct vt_event_wait, list);
123 if (!(ve->event.event & event))
124 continue;
125 ve->event.event = event;
126
127
128 ve->event.oldev = old + 1;
129 ve->event.newev = new + 1;
130 wake = 1;
131 ve->done = 1;
132 }
133 spin_unlock_irqrestore(&vt_event_lock, flags);
134 if (wake)
135 wake_up_interruptible(&vt_event_waitqueue);
136}
137
138static void __vt_event_queue(struct vt_event_wait *vw)
139{
140 unsigned long flags;
141
142 INIT_LIST_HEAD(&vw->list);
143 vw->done = 0;
144
145 spin_lock_irqsave(&vt_event_lock, flags);
146 list_add(&vw->list, &vt_events);
147 spin_unlock_irqrestore(&vt_event_lock, flags);
148}
149
150static void __vt_event_wait(struct vt_event_wait *vw)
151{
152
153 wait_event_interruptible(vt_event_waitqueue, vw->done);
154}
155
156static void __vt_event_dequeue(struct vt_event_wait *vw)
157{
158 unsigned long flags;
159
160
161 spin_lock_irqsave(&vt_event_lock, flags);
162 list_del(&vw->list);
163 spin_unlock_irqrestore(&vt_event_lock, flags);
164}
165
166
167
168
169
170
171
172
173
174
175static void vt_event_wait(struct vt_event_wait *vw)
176{
177 __vt_event_queue(vw);
178 __vt_event_wait(vw);
179 __vt_event_dequeue(vw);
180}
181
182
183
184
185
186
187
188
189static int vt_event_wait_ioctl(struct vt_event __user *event)
190{
191 struct vt_event_wait vw;
192
193 if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
194 return -EFAULT;
195
196 if (vw.event.event & ~VT_MAX_EVENT)
197 return -EINVAL;
198
199 vt_event_wait(&vw);
200
201 if (vw.done) {
202 if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
203 return -EFAULT;
204 return 0;
205 }
206 return -EINTR;
207}
208
209
210
211
212
213
214
215
216
217int vt_waitactive(int n)
218{
219 struct vt_event_wait vw;
220 do {
221 vw.event.event = VT_EVENT_SWITCH;
222 __vt_event_queue(&vw);
223 if (n == fg_console + 1) {
224 __vt_event_dequeue(&vw);
225 break;
226 }
227 __vt_event_wait(&vw);
228 __vt_event_dequeue(&vw);
229 if (vw.done == 0)
230 return -EINTR;
231 } while (vw.event.newev != n);
232 return 0;
233}
234
235
236
237
238
239#define GPFIRST 0x3b4
240#define GPLAST 0x3df
241#define GPNUM (GPLAST - GPFIRST + 1)
242
243
244
245
246
247
248
249
250static int vt_kdsetmode(struct vc_data *vc, unsigned long mode)
251{
252 switch (mode) {
253 case KD_GRAPHICS:
254 break;
255 case KD_TEXT0:
256 case KD_TEXT1:
257 mode = KD_TEXT;
258 fallthrough;
259 case KD_TEXT:
260 break;
261 default:
262 return -EINVAL;
263 }
264
265
266 if (vc->vc_mode == mode)
267 return 0;
268
269 vc->vc_mode = mode;
270 if (vc->vc_num != fg_console)
271 return 0;
272
273
274 console_lock();
275 if (mode == KD_TEXT)
276 do_unblank_screen(1);
277 else
278 do_blank_screen(1);
279 console_unlock();
280
281 return 0;
282}
283
284static int vt_k_ioctl(struct tty_struct *tty, unsigned int cmd,
285 unsigned long arg, bool perm)
286{
287 struct vc_data *vc = tty->driver_data;
288 void __user *up = (void __user *)arg;
289 unsigned int console = vc->vc_num;
290 int ret;
291
292 switch (cmd) {
293 case KIOCSOUND:
294 if (!perm)
295 return -EPERM;
296
297
298
299
300
301
302 if (arg)
303 arg = PIT_TICK_RATE / arg;
304 kd_mksound(arg, 0);
305 break;
306
307 case KDMKTONE:
308 if (!perm)
309 return -EPERM;
310 {
311 unsigned int ticks, count;
312
313
314
315
316
317 ticks = msecs_to_jiffies((arg >> 16) & 0xffff);
318 count = ticks ? (arg & 0xffff) : 0;
319 if (count)
320 count = PIT_TICK_RATE / count;
321 kd_mksound(count, ticks);
322 break;
323 }
324
325 case KDGKBTYPE:
326
327
328
329 return put_user(KB_101, (char __user *)arg);
330
331
332
333
334
335
336
337#ifdef CONFIG_X86
338 case KDADDIO:
339 case KDDELIO:
340
341
342
343
344
345
346 if (arg < GPFIRST || arg > GPLAST)
347 return -EINVAL;
348
349 return ksys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
350
351 case KDENABIO:
352 case KDDISABIO:
353 return ksys_ioperm(GPFIRST, GPNUM,
354 (cmd == KDENABIO)) ? -ENXIO : 0;
355#endif
356
357
358
359 case KDKBDREP:
360 {
361 struct kbd_repeat kbrep;
362
363 if (!capable(CAP_SYS_TTY_CONFIG))
364 return -EPERM;
365
366 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat)))
367 return -EFAULT;
368
369 ret = kbd_rate(&kbrep);
370 if (ret)
371 return ret;
372 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
373 return -EFAULT;
374 break;
375 }
376
377 case KDSETMODE:
378 if (!perm)
379 return -EPERM;
380
381 return vt_kdsetmode(vc, arg);
382
383 case KDGETMODE:
384 return put_user(vc->vc_mode, (int __user *)arg);
385
386 case KDMAPDISP:
387 case KDUNMAPDISP:
388
389
390
391
392 return -EINVAL;
393
394 case KDSKBMODE:
395 if (!perm)
396 return -EPERM;
397 ret = vt_do_kdskbmode(console, arg);
398 if (ret)
399 return ret;
400 tty_ldisc_flush(tty);
401 break;
402
403 case KDGKBMODE:
404 return put_user(vt_do_kdgkbmode(console), (int __user *)arg);
405
406
407
408 case KDSKBMETA:
409 return vt_do_kdskbmeta(console, arg);
410
411 case KDGKBMETA:
412
413 return put_user(vt_do_kdgkbmeta(console), (int __user *)arg);
414
415 case KDGETKEYCODE:
416 case KDSETKEYCODE:
417 if(!capable(CAP_SYS_TTY_CONFIG))
418 perm = 0;
419 return vt_do_kbkeycode_ioctl(cmd, up, perm);
420
421 case KDGKBENT:
422 case KDSKBENT:
423 return vt_do_kdsk_ioctl(cmd, up, perm, console);
424
425 case KDGKBSENT:
426 case KDSKBSENT:
427 return vt_do_kdgkb_ioctl(cmd, up, perm);
428
429
430
431 case KDGKBDIACR:
432 case KDGKBDIACRUC:
433 case KDSKBDIACR:
434 case KDSKBDIACRUC:
435 return vt_do_diacrit(cmd, up, perm);
436
437
438
439 case KDGKBLED:
440 case KDSKBLED:
441 case KDGETLED:
442 case KDSETLED:
443 return vt_do_kdskled(console, cmd, arg, perm);
444
445
446
447
448
449
450
451
452 case KDSIGACCEPT:
453 if (!perm || !capable(CAP_KILL))
454 return -EPERM;
455 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
456 return -EINVAL;
457
458 spin_lock_irq(&vt_spawn_con.lock);
459 put_pid(vt_spawn_con.pid);
460 vt_spawn_con.pid = get_pid(task_pid(current));
461 vt_spawn_con.sig = arg;
462 spin_unlock_irq(&vt_spawn_con.lock);
463 break;
464
465 case KDFONTOP: {
466 struct console_font_op op;
467
468 if (copy_from_user(&op, up, sizeof(op)))
469 return -EFAULT;
470 if (!perm && op.op != KD_FONT_OP_GET)
471 return -EPERM;
472 ret = con_font_op(vc, &op);
473 if (ret)
474 return ret;
475 if (copy_to_user(up, &op, sizeof(op)))
476 return -EFAULT;
477 break;
478 }
479
480 default:
481 return -ENOIOCTLCMD;
482 }
483
484 return 0;
485}
486
487static inline int do_fontx_ioctl(struct vc_data *vc, int cmd,
488 struct consolefontdesc __user *user_cfd,
489 struct console_font_op *op)
490{
491 struct consolefontdesc cfdarg;
492 int i;
493
494 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
495 return -EFAULT;
496
497 switch (cmd) {
498 case PIO_FONTX:
499 op->op = KD_FONT_OP_SET;
500 op->flags = KD_FONT_FLAG_OLD;
501 op->width = 8;
502 op->height = cfdarg.charheight;
503 op->charcount = cfdarg.charcount;
504 op->data = cfdarg.chardata;
505 return con_font_op(vc, op);
506
507 case GIO_FONTX:
508 op->op = KD_FONT_OP_GET;
509 op->flags = KD_FONT_FLAG_OLD;
510 op->width = 8;
511 op->height = cfdarg.charheight;
512 op->charcount = cfdarg.charcount;
513 op->data = cfdarg.chardata;
514 i = con_font_op(vc, op);
515 if (i)
516 return i;
517 cfdarg.charheight = op->height;
518 cfdarg.charcount = op->charcount;
519 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
520 return -EFAULT;
521 return 0;
522 }
523 return -EINVAL;
524}
525
526static int vt_io_fontreset(struct vc_data *vc, struct console_font_op *op)
527{
528 int ret;
529
530 if (__is_defined(BROKEN_GRAPHICS_PROGRAMS)) {
531
532
533
534
535 return -ENOSYS;
536 }
537
538 op->op = KD_FONT_OP_SET_DEFAULT;
539 op->data = NULL;
540 ret = con_font_op(vc, op);
541 if (ret)
542 return ret;
543
544 console_lock();
545 con_set_default_unimap(vc);
546 console_unlock();
547
548 return 0;
549}
550
551static inline int do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud,
552 bool perm, struct vc_data *vc)
553{
554 struct unimapdesc tmp;
555
556 if (copy_from_user(&tmp, user_ud, sizeof tmp))
557 return -EFAULT;
558 switch (cmd) {
559 case PIO_UNIMAP:
560 if (!perm)
561 return -EPERM;
562 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
563 case GIO_UNIMAP:
564 if (!perm && fg_console != vc->vc_num)
565 return -EPERM;
566 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct),
567 tmp.entries);
568 }
569 return 0;
570}
571
572static int vt_io_ioctl(struct vc_data *vc, unsigned int cmd, void __user *up,
573 bool perm)
574{
575 struct console_font_op op;
576
577 switch (cmd) {
578 case PIO_FONT:
579 if (!perm)
580 return -EPERM;
581 op.op = KD_FONT_OP_SET;
582 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC;
583 op.width = 8;
584 op.height = 0;
585 op.charcount = 256;
586 op.data = up;
587 return con_font_op(vc, &op);
588
589 case GIO_FONT:
590 op.op = KD_FONT_OP_GET;
591 op.flags = KD_FONT_FLAG_OLD;
592 op.width = 8;
593 op.height = 32;
594 op.charcount = 256;
595 op.data = up;
596 return con_font_op(vc, &op);
597
598 case PIO_CMAP:
599 if (!perm)
600 return -EPERM;
601 return con_set_cmap(up);
602
603 case GIO_CMAP:
604 return con_get_cmap(up);
605
606 case PIO_FONTX:
607 if (!perm)
608 return -EPERM;
609
610 fallthrough;
611 case GIO_FONTX:
612 return do_fontx_ioctl(vc, cmd, up, &op);
613
614 case PIO_FONTRESET:
615 if (!perm)
616 return -EPERM;
617
618 return vt_io_fontreset(vc, &op);
619
620 case PIO_SCRNMAP:
621 if (!perm)
622 return -EPERM;
623 return con_set_trans_old(up);
624
625 case GIO_SCRNMAP:
626 return con_get_trans_old(up);
627
628 case PIO_UNISCRNMAP:
629 if (!perm)
630 return -EPERM;
631 return con_set_trans_new(up);
632
633 case GIO_UNISCRNMAP:
634 return con_get_trans_new(up);
635
636 case PIO_UNIMAPCLR:
637 if (!perm)
638 return -EPERM;
639 con_clear_unimap(vc);
640 break;
641
642 case PIO_UNIMAP:
643 case GIO_UNIMAP:
644 return do_unimap_ioctl(cmd, up, perm, vc);
645
646 default:
647 return -ENOIOCTLCMD;
648 }
649
650 return 0;
651}
652
653static int vt_reldisp(struct vc_data *vc, unsigned int swtch)
654{
655 int newvt, ret;
656
657 if (vc->vt_mode.mode != VT_PROCESS)
658 return -EINVAL;
659
660
661 if (vc->vt_newvt < 0) {
662
663 return swtch == VT_ACKACQ ? 0 : -EINVAL;
664 }
665
666
667 if (swtch == 0) {
668
669 vc->vt_newvt = -1;
670 return 0;
671 }
672
673
674 newvt = vc->vt_newvt;
675 vc->vt_newvt = -1;
676 ret = vc_allocate(newvt);
677 if (ret)
678 return ret;
679
680
681
682
683
684 complete_change_console(vc_cons[newvt].d);
685
686 return 0;
687}
688
689static int vt_setactivate(struct vt_setactivate __user *sa)
690{
691 struct vt_setactivate vsa;
692 struct vc_data *nvc;
693 int ret;
694
695 if (copy_from_user(&vsa, sa, sizeof(vsa)))
696 return -EFAULT;
697 if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
698 return -ENXIO;
699
700 vsa.console = array_index_nospec(vsa.console, MAX_NR_CONSOLES + 1);
701 vsa.console--;
702 console_lock();
703 ret = vc_allocate(vsa.console);
704 if (ret) {
705 console_unlock();
706 return ret;
707 }
708
709
710
711
712
713 nvc = vc_cons[vsa.console].d;
714 nvc->vt_mode = vsa.mode;
715 nvc->vt_mode.frsig = 0;
716 put_pid(nvc->vt_pid);
717 nvc->vt_pid = get_pid(task_pid(current));
718 console_unlock();
719
720
721
722 set_console(vsa.console);
723
724 return 0;
725}
726
727
728static int vt_disallocate(unsigned int vc_num)
729{
730 struct vc_data *vc = NULL;
731 int ret = 0;
732
733 console_lock();
734 if (vt_busy(vc_num))
735 ret = -EBUSY;
736 else if (vc_num)
737 vc = vc_deallocate(vc_num);
738 console_unlock();
739
740 if (vc && vc_num >= MIN_NR_CONSOLES)
741 tty_port_put(&vc->port);
742
743 return ret;
744}
745
746
747static void vt_disallocate_all(void)
748{
749 struct vc_data *vc[MAX_NR_CONSOLES];
750 int i;
751
752 console_lock();
753 for (i = 1; i < MAX_NR_CONSOLES; i++)
754 if (!vt_busy(i))
755 vc[i] = vc_deallocate(i);
756 else
757 vc[i] = NULL;
758 console_unlock();
759
760 for (i = 1; i < MAX_NR_CONSOLES; i++) {
761 if (vc[i] && i >= MIN_NR_CONSOLES)
762 tty_port_put(&vc[i]->port);
763 }
764}
765
766static int vt_resizex(struct vc_data *vc, struct vt_consize __user *cs)
767{
768 struct vt_consize v;
769 int i;
770
771 if (copy_from_user(&v, cs, sizeof(struct vt_consize)))
772 return -EFAULT;
773
774 if (v.v_vlin)
775 pr_info_once("\"struct vt_consize\"->v_vlin is ignored. Please report if you need this.\n");
776 if (v.v_clin)
777 pr_info_once("\"struct vt_consize\"->v_clin is ignored. Please report if you need this.\n");
778
779 console_lock();
780 for (i = 0; i < MAX_NR_CONSOLES; i++) {
781 vc = vc_cons[i].d;
782
783 if (vc) {
784 vc->vc_resize_user = 1;
785 vc_resize(vc, v.v_cols, v.v_rows);
786 }
787 }
788 console_unlock();
789
790 return 0;
791}
792
793
794
795
796
797int vt_ioctl(struct tty_struct *tty,
798 unsigned int cmd, unsigned long arg)
799{
800 struct vc_data *vc = tty->driver_data;
801 void __user *up = (void __user *)arg;
802 int i, perm;
803 int ret;
804
805
806
807
808
809 perm = 0;
810 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
811 perm = 1;
812
813 ret = vt_k_ioctl(tty, cmd, arg, perm);
814 if (ret != -ENOIOCTLCMD)
815 return ret;
816
817 ret = vt_io_ioctl(vc, cmd, up, perm);
818 if (ret != -ENOIOCTLCMD)
819 return ret;
820
821 switch (cmd) {
822 case TIOCLINUX:
823 return tioclinux(tty, arg);
824 case VT_SETMODE:
825 {
826 struct vt_mode tmp;
827
828 if (!perm)
829 return -EPERM;
830 if (copy_from_user(&tmp, up, sizeof(struct vt_mode)))
831 return -EFAULT;
832 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS)
833 return -EINVAL;
834
835 console_lock();
836 vc->vt_mode = tmp;
837
838 vc->vt_mode.frsig = 0;
839 put_pid(vc->vt_pid);
840 vc->vt_pid = get_pid(task_pid(current));
841
842 vc->vt_newvt = -1;
843 console_unlock();
844 break;
845 }
846
847 case VT_GETMODE:
848 {
849 struct vt_mode tmp;
850 int rc;
851
852 console_lock();
853 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
854 console_unlock();
855
856 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
857 if (rc)
858 return -EFAULT;
859 break;
860 }
861
862
863
864
865
866
867 case VT_GETSTATE:
868 {
869 struct vt_stat __user *vtstat = up;
870 unsigned short state, mask;
871
872 if (put_user(fg_console + 1, &vtstat->v_active))
873 return -EFAULT;
874
875 state = 1;
876 console_lock();
877 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
878 ++i, mask <<= 1)
879 if (vt_in_use(i))
880 state |= mask;
881 console_unlock();
882 return put_user(state, &vtstat->v_state);
883 }
884
885
886
887
888 case VT_OPENQRY:
889 console_lock();
890 for (i = 0; i < MAX_NR_CONSOLES; ++i)
891 if (!vt_in_use(i))
892 break;
893 console_unlock();
894 i = i < MAX_NR_CONSOLES ? (i+1) : -1;
895 return put_user(i, (int __user *)arg);
896
897
898
899
900
901
902 case VT_ACTIVATE:
903 if (!perm)
904 return -EPERM;
905 if (arg == 0 || arg > MAX_NR_CONSOLES)
906 return -ENXIO;
907
908 arg--;
909 console_lock();
910 ret = vc_allocate(arg);
911 console_unlock();
912 if (ret)
913 return ret;
914 set_console(arg);
915 break;
916
917 case VT_SETACTIVATE:
918 if (!perm)
919 return -EPERM;
920
921 return vt_setactivate(up);
922
923
924
925
926 case VT_WAITACTIVE:
927 if (!perm)
928 return -EPERM;
929 if (arg == 0 || arg > MAX_NR_CONSOLES)
930 return -ENXIO;
931 return vt_waitactive(arg);
932
933
934
935
936
937
938
939
940
941
942
943 case VT_RELDISP:
944 if (!perm)
945 return -EPERM;
946
947 console_lock();
948 ret = vt_reldisp(vc, arg);
949 console_unlock();
950
951 return ret;
952
953
954
955
956
957 case VT_DISALLOCATE:
958 if (arg > MAX_NR_CONSOLES)
959 return -ENXIO;
960
961 if (arg == 0)
962 vt_disallocate_all();
963 else
964 return vt_disallocate(--arg);
965 break;
966
967 case VT_RESIZE:
968 {
969 struct vt_sizes __user *vtsizes = up;
970 struct vc_data *vc;
971 ushort ll,cc;
972
973 if (!perm)
974 return -EPERM;
975 if (get_user(ll, &vtsizes->v_rows) ||
976 get_user(cc, &vtsizes->v_cols))
977 return -EFAULT;
978
979 console_lock();
980 for (i = 0; i < MAX_NR_CONSOLES; i++) {
981 vc = vc_cons[i].d;
982
983 if (vc) {
984 vc->vc_resize_user = 1;
985
986 vc_resize(vc_cons[i].d, cc, ll);
987 }
988 }
989 console_unlock();
990 break;
991 }
992
993 case VT_RESIZEX:
994 if (!perm)
995 return -EPERM;
996
997 return vt_resizex(vc, up);
998
999 case VT_LOCKSWITCH:
1000 if (!capable(CAP_SYS_TTY_CONFIG))
1001 return -EPERM;
1002 vt_dont_switch = true;
1003 break;
1004 case VT_UNLOCKSWITCH:
1005 if (!capable(CAP_SYS_TTY_CONFIG))
1006 return -EPERM;
1007 vt_dont_switch = false;
1008 break;
1009 case VT_GETHIFONTMASK:
1010 return put_user(vc->vc_hi_font_mask,
1011 (unsigned short __user *)arg);
1012 case VT_WAITEVENT:
1013 return vt_event_wait_ioctl((struct vt_event __user *)arg);
1014 default:
1015 return -ENOIOCTLCMD;
1016 }
1017
1018 return 0;
1019}
1020
1021void reset_vc(struct vc_data *vc)
1022{
1023 vc->vc_mode = KD_TEXT;
1024 vt_reset_unicode(vc->vc_num);
1025 vc->vt_mode.mode = VT_AUTO;
1026 vc->vt_mode.waitv = 0;
1027 vc->vt_mode.relsig = 0;
1028 vc->vt_mode.acqsig = 0;
1029 vc->vt_mode.frsig = 0;
1030 put_pid(vc->vt_pid);
1031 vc->vt_pid = NULL;
1032 vc->vt_newvt = -1;
1033 if (!in_interrupt())
1034 reset_palette(vc);
1035}
1036
1037void vc_SAK(struct work_struct *work)
1038{
1039 struct vc *vc_con =
1040 container_of(work, struct vc, SAK_work);
1041 struct vc_data *vc;
1042 struct tty_struct *tty;
1043
1044 console_lock();
1045 vc = vc_con->d;
1046 if (vc) {
1047
1048 tty = vc->port.tty;
1049
1050
1051
1052
1053 if (tty)
1054 __do_SAK(tty);
1055 reset_vc(vc);
1056 }
1057 console_unlock();
1058}
1059
1060#ifdef CONFIG_COMPAT
1061
1062struct compat_consolefontdesc {
1063 unsigned short charcount;
1064 unsigned short charheight;
1065 compat_caddr_t chardata;
1066};
1067
1068static inline int
1069compat_fontx_ioctl(struct vc_data *vc, int cmd,
1070 struct compat_consolefontdesc __user *user_cfd,
1071 int perm, struct console_font_op *op)
1072{
1073 struct compat_consolefontdesc cfdarg;
1074 int i;
1075
1076 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
1077 return -EFAULT;
1078
1079 switch (cmd) {
1080 case PIO_FONTX:
1081 if (!perm)
1082 return -EPERM;
1083 op->op = KD_FONT_OP_SET;
1084 op->flags = KD_FONT_FLAG_OLD;
1085 op->width = 8;
1086 op->height = cfdarg.charheight;
1087 op->charcount = cfdarg.charcount;
1088 op->data = compat_ptr(cfdarg.chardata);
1089 return con_font_op(vc, op);
1090
1091 case GIO_FONTX:
1092 op->op = KD_FONT_OP_GET;
1093 op->flags = KD_FONT_FLAG_OLD;
1094 op->width = 8;
1095 op->height = cfdarg.charheight;
1096 op->charcount = cfdarg.charcount;
1097 op->data = compat_ptr(cfdarg.chardata);
1098 i = con_font_op(vc, op);
1099 if (i)
1100 return i;
1101 cfdarg.charheight = op->height;
1102 cfdarg.charcount = op->charcount;
1103 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
1104 return -EFAULT;
1105 return 0;
1106 }
1107 return -EINVAL;
1108}
1109
1110struct compat_console_font_op {
1111 compat_uint_t op;
1112 compat_uint_t flags;
1113 compat_uint_t width, height;
1114 compat_uint_t charcount;
1115 compat_caddr_t data;
1116};
1117
1118static inline int
1119compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
1120 int perm, struct console_font_op *op, struct vc_data *vc)
1121{
1122 int i;
1123
1124 if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
1125 return -EFAULT;
1126 if (!perm && op->op != KD_FONT_OP_GET)
1127 return -EPERM;
1128 op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
1129 i = con_font_op(vc, op);
1130 if (i)
1131 return i;
1132 ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
1133 if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
1134 return -EFAULT;
1135 return 0;
1136}
1137
1138struct compat_unimapdesc {
1139 unsigned short entry_ct;
1140 compat_caddr_t entries;
1141};
1142
1143static inline int
1144compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
1145 int perm, struct vc_data *vc)
1146{
1147 struct compat_unimapdesc tmp;
1148 struct unipair __user *tmp_entries;
1149
1150 if (copy_from_user(&tmp, user_ud, sizeof tmp))
1151 return -EFAULT;
1152 tmp_entries = compat_ptr(tmp.entries);
1153 switch (cmd) {
1154 case PIO_UNIMAP:
1155 if (!perm)
1156 return -EPERM;
1157 return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
1158 case GIO_UNIMAP:
1159 if (!perm && fg_console != vc->vc_num)
1160 return -EPERM;
1161 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
1162 }
1163 return 0;
1164}
1165
1166long vt_compat_ioctl(struct tty_struct *tty,
1167 unsigned int cmd, unsigned long arg)
1168{
1169 struct vc_data *vc = tty->driver_data;
1170 struct console_font_op op;
1171 void __user *up = compat_ptr(arg);
1172 int perm;
1173
1174
1175
1176
1177
1178 perm = 0;
1179 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1180 perm = 1;
1181
1182 switch (cmd) {
1183
1184
1185
1186 case PIO_FONTX:
1187 case GIO_FONTX:
1188 return compat_fontx_ioctl(vc, cmd, up, perm, &op);
1189
1190 case KDFONTOP:
1191 return compat_kdfontop_ioctl(up, perm, &op, vc);
1192
1193 case PIO_UNIMAP:
1194 case GIO_UNIMAP:
1195 return compat_unimap_ioctl(cmd, up, perm, vc);
1196
1197
1198
1199
1200 case KIOCSOUND:
1201 case KDMKTONE:
1202#ifdef CONFIG_X86
1203 case KDADDIO:
1204 case KDDELIO:
1205#endif
1206 case KDSETMODE:
1207 case KDMAPDISP:
1208 case KDUNMAPDISP:
1209 case KDSKBMODE:
1210 case KDSKBMETA:
1211 case KDSKBLED:
1212 case KDSETLED:
1213 case KDSIGACCEPT:
1214 case VT_ACTIVATE:
1215 case VT_WAITACTIVE:
1216 case VT_RELDISP:
1217 case VT_DISALLOCATE:
1218 case VT_RESIZE:
1219 case VT_RESIZEX:
1220 return vt_ioctl(tty, cmd, arg);
1221
1222
1223
1224
1225
1226 default:
1227 return vt_ioctl(tty, cmd, (unsigned long)up);
1228 }
1229}
1230
1231
1232#endif
1233
1234
1235
1236
1237
1238
1239static void complete_change_console(struct vc_data *vc)
1240{
1241 unsigned char old_vc_mode;
1242 int old = fg_console;
1243
1244 last_console = fg_console;
1245
1246
1247
1248
1249
1250
1251 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1252 switch_screen(vc);
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264 if (old_vc_mode != vc->vc_mode) {
1265 if (vc->vc_mode == KD_TEXT)
1266 do_unblank_screen(1);
1267 else
1268 do_blank_screen(1);
1269 }
1270
1271
1272
1273
1274
1275
1276 if (vc->vt_mode.mode == VT_PROCESS) {
1277
1278
1279
1280
1281
1282 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292 reset_vc(vc);
1293
1294 if (old_vc_mode != vc->vc_mode) {
1295 if (vc->vc_mode == KD_TEXT)
1296 do_unblank_screen(1);
1297 else
1298 do_blank_screen(1);
1299 }
1300 }
1301 }
1302
1303
1304
1305
1306 vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
1307 return;
1308}
1309
1310
1311
1312
1313void change_console(struct vc_data *new_vc)
1314{
1315 struct vc_data *vc;
1316
1317 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1318 return;
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335 vc = vc_cons[fg_console].d;
1336 if (vc->vt_mode.mode == VT_PROCESS) {
1337
1338
1339
1340
1341
1342
1343
1344
1345 vc->vt_newvt = new_vc->vc_num;
1346 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
1347
1348
1349
1350
1351
1352 return;
1353 }
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364 reset_vc(vc);
1365
1366
1367
1368
1369 }
1370
1371
1372
1373
1374 if (vc->vc_mode == KD_GRAPHICS)
1375 return;
1376
1377 complete_change_console(new_vc);
1378}
1379
1380
1381
1382static int disable_vt_switch;
1383
1384int vt_move_to_console(unsigned int vt, int alloc)
1385{
1386 int prev;
1387
1388 console_lock();
1389
1390 if (disable_vt_switch) {
1391 console_unlock();
1392 return 0;
1393 }
1394 prev = fg_console;
1395
1396 if (alloc && vc_allocate(vt)) {
1397
1398
1399 console_unlock();
1400 return -ENOSPC;
1401 }
1402
1403 if (set_console(vt)) {
1404
1405
1406
1407
1408
1409 console_unlock();
1410 return -EIO;
1411 }
1412 console_unlock();
1413 if (vt_waitactive(vt + 1)) {
1414 pr_debug("Suspend: Can't switch VCs.");
1415 return -EINTR;
1416 }
1417 return prev;
1418}
1419
1420
1421
1422
1423
1424
1425
1426
1427void pm_set_vt_switch(int do_switch)
1428{
1429 console_lock();
1430 disable_vt_switch = !do_switch;
1431 console_unlock();
1432}
1433EXPORT_SYMBOL(pm_set_vt_switch);
1434