GNU Linux-libre 4.19.281-gnu1
[releases.git] / drivers / tty / vt / vt_ioctl.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright (C) 1992 obz under the linux copyright
4  *
5  *  Dynamic diacritical handling - aeb@cwi.nl - Dec 1993
6  *  Dynamic keymap and string allocation - aeb@cwi.nl - May 1994
7  *  Restrict VT switching via ioctl() - grif@cs.ucr.edu - Dec 1995
8  *  Some code moved for less code duplication - Andi Kleen - Mar 1997
9  *  Check put/get_user, cleanups - acme@conectiva.com.br - Jun 2001
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
42 bool vt_dont_switch;
43
44 static inline bool vt_in_use(unsigned int i)
45 {
46         const struct vc_data *vc = vc_cons[i].d;
47
48         /*
49          * console_lock must be held to prevent the vc from being deallocated
50          * while we're checking whether it's in-use.
51          */
52         WARN_CONSOLE_UNLOCKED();
53
54         return vc && kref_read(&vc->port.kref) > 1;
55 }
56
57 static 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  * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
71  * experimentation and study of X386 SYSV handling.
72  *
73  * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
74  * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
75  * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
76  * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
77  * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
78  * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
79  * to the current console is done by the main ioctl code.
80  */
81
82 #ifdef CONFIG_X86
83 #include <asm/syscalls.h>
84 #endif
85
86 static void complete_change_console(struct vc_data *vc);
87
88 /*
89  *      User space VT_EVENT handlers
90  */
91
92 struct vt_event_wait {
93         struct list_head list;
94         struct vt_event event;
95         int done;
96 };
97
98 static LIST_HEAD(vt_events);
99 static DEFINE_SPINLOCK(vt_event_lock);
100 static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
101
102 /**
103  *      vt_event_post
104  *      @event: the event that occurred
105  *      @old: old console
106  *      @new: new console
107  *
108  *      Post an VT event to interested VT handlers
109  */
110
111 void 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                 /* kernel view is consoles 0..n-1, user space view is
127                    console 1..n with 0 meaning current, so we must bias */
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
138 static void __vt_event_queue(struct vt_event_wait *vw)
139 {
140         unsigned long flags;
141         /* Prepare the event */
142         INIT_LIST_HEAD(&vw->list);
143         vw->done = 0;
144         /* Queue our event */
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
150 static void __vt_event_wait(struct vt_event_wait *vw)
151 {
152         /* Wait for it to pass */
153         wait_event_interruptible(vt_event_waitqueue, vw->done);
154 }
155
156 static void __vt_event_dequeue(struct vt_event_wait *vw)
157 {
158         unsigned long flags;
159
160         /* Dequeue it */
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  *      vt_event_wait           -       wait for an event
168  *      @vw: our event
169  *
170  *      Waits for an event to occur which completes our vt_event_wait
171  *      structure. On return the structure has wv->done set to 1 for success
172  *      or 0 if some event such as a signal ended the wait.
173  */
174
175 static 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  *      vt_event_wait_ioctl     -       event ioctl handler
184  *      @arg: argument to ioctl
185  *
186  *      Implement the VT_WAITEVENT ioctl using the VT event interface
187  */
188
189 static 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         /* Highest supported event for now */
196         if (vw.event.event & ~VT_MAX_EVENT)
197                 return -EINVAL;
198
199         vt_event_wait(&vw);
200         /* If it occurred report it */
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  *      vt_waitactive   -       active console wait
211  *      @event: event code
212  *      @n: new console
213  *
214  *      Helper for event waits. Used to implement the legacy
215  *      event waiting ioctls in terms of events
216  */
217
218 int vt_waitactive(int n)
219 {
220         struct vt_event_wait vw;
221         do {
222                 vw.event.event = VT_EVENT_SWITCH;
223                 __vt_event_queue(&vw);
224                 if (n == fg_console + 1) {
225                         __vt_event_dequeue(&vw);
226                         break;
227                 }
228                 __vt_event_wait(&vw);
229                 __vt_event_dequeue(&vw);
230                 if (vw.done == 0)
231                         return -EINTR;
232         } while (vw.event.newev != n);
233         return 0;
234 }
235
236 /*
237  * these are the valid i/o ports we're allowed to change. they map all the
238  * video ports
239  */
240 #define GPFIRST 0x3b4
241 #define GPLAST 0x3df
242 #define GPNUM (GPLAST - GPFIRST + 1)
243
244 static inline int 
245 do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
246 {
247         struct unimapdesc tmp;
248
249         if (copy_from_user(&tmp, user_ud, sizeof tmp))
250                 return -EFAULT;
251         switch (cmd) {
252         case PIO_UNIMAP:
253                 if (!perm)
254                         return -EPERM;
255                 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
256         case GIO_UNIMAP:
257                 if (!perm && fg_console != vc->vc_num)
258                         return -EPERM;
259                 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
260         }
261         return 0;
262 }
263
264 /* deallocate a single console, if possible (leave 0) */
265 static int vt_disallocate(unsigned int vc_num)
266 {
267         struct vc_data *vc = NULL;
268         int ret = 0;
269
270         console_lock();
271         if (vt_busy(vc_num))
272                 ret = -EBUSY;
273         else if (vc_num)
274                 vc = vc_deallocate(vc_num);
275         console_unlock();
276
277         if (vc && vc_num >= MIN_NR_CONSOLES)
278                 tty_port_put(&vc->port);
279
280         return ret;
281 }
282
283 /* deallocate all unused consoles, but leave 0 */
284 static void vt_disallocate_all(void)
285 {
286         struct vc_data *vc[MAX_NR_CONSOLES];
287         int i;
288
289         console_lock();
290         for (i = 1; i < MAX_NR_CONSOLES; i++)
291                 if (!vt_busy(i))
292                         vc[i] = vc_deallocate(i);
293                 else
294                         vc[i] = NULL;
295         console_unlock();
296
297         for (i = 1; i < MAX_NR_CONSOLES; i++) {
298                 if (vc[i] && i >= MIN_NR_CONSOLES)
299                         tty_port_put(&vc[i]->port);
300         }
301 }
302
303
304 /*
305  * We handle the console-specific ioctl's here.  We allow the
306  * capability to modify any console, not just the fg_console. 
307  */
308 int vt_ioctl(struct tty_struct *tty,
309              unsigned int cmd, unsigned long arg)
310 {
311         struct vc_data *vc = tty->driver_data;
312         struct console_font_op op;      /* used in multiple places here */
313         unsigned int console = vc->vc_num;
314         unsigned char ucval;
315         unsigned int uival;
316         void __user *up = (void __user *)arg;
317         int i, perm;
318         int ret = 0;
319
320         /*
321          * To have permissions to do most of the vt ioctls, we either have
322          * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
323          */
324         perm = 0;
325         if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
326                 perm = 1;
327  
328         switch (cmd) {
329         case TIOCLINUX:
330                 ret = tioclinux(tty, arg);
331                 break;
332         case KIOCSOUND:
333                 if (!perm)
334                         return -EPERM;
335                 /*
336                  * The use of PIT_TICK_RATE is historic, it used to be
337                  * the platform-dependent CLOCK_TICK_RATE between 2.6.12
338                  * and 2.6.36, which was a minor but unfortunate ABI
339                  * change. kd_mksound is locked by the input layer.
340                  */
341                 if (arg)
342                         arg = PIT_TICK_RATE / arg;
343                 kd_mksound(arg, 0);
344                 break;
345
346         case KDMKTONE:
347                 if (!perm)
348                         return -EPERM;
349         {
350                 unsigned int ticks, count;
351                 
352                 /*
353                  * Generate the tone for the appropriate number of ticks.
354                  * If the time is zero, turn off sound ourselves.
355                  */
356                 ticks = msecs_to_jiffies((arg >> 16) & 0xffff);
357                 count = ticks ? (arg & 0xffff) : 0;
358                 if (count)
359                         count = PIT_TICK_RATE / count;
360                 kd_mksound(count, ticks);
361                 break;
362         }
363
364         case KDGKBTYPE:
365                 /*
366                  * this is naïve.
367                  */
368                 ucval = KB_101;
369                 ret = put_user(ucval, (char __user *)arg);
370                 break;
371
372                 /*
373                  * These cannot be implemented on any machine that implements
374                  * ioperm() in user level (such as Alpha PCs) or not at all.
375                  *
376                  * XXX: you should never use these, just call ioperm directly..
377                  */
378 #ifdef CONFIG_X86
379         case KDADDIO:
380         case KDDELIO:
381                 /*
382                  * KDADDIO and KDDELIO may be able to add ports beyond what
383                  * we reject here, but to be safe...
384                  *
385                  * These are locked internally via sys_ioperm
386                  */
387                 if (arg < GPFIRST || arg > GPLAST) {
388                         ret = -EINVAL;
389                         break;
390                 }
391                 ret = ksys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
392                 break;
393
394         case KDENABIO:
395         case KDDISABIO:
396                 ret = ksys_ioperm(GPFIRST, GPNUM,
397                                   (cmd == KDENABIO)) ? -ENXIO : 0;
398                 break;
399 #endif
400
401         /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
402                 
403         case KDKBDREP:
404         {
405                 struct kbd_repeat kbrep;
406                 
407                 if (!capable(CAP_SYS_TTY_CONFIG))
408                         return -EPERM;
409
410                 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
411                         ret =  -EFAULT;
412                         break;
413                 }
414                 ret = kbd_rate(&kbrep);
415                 if (ret)
416                         break;
417                 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
418                         ret = -EFAULT;
419                 break;
420         }
421
422         case KDSETMODE:
423                 /*
424                  * currently, setting the mode from KD_TEXT to KD_GRAPHICS
425                  * doesn't do a whole lot. i'm not sure if it should do any
426                  * restoration of modes or what...
427                  *
428                  * XXX It should at least call into the driver, fbdev's definitely
429                  * need to restore their engine state. --BenH
430                  */
431                 if (!perm)
432                         return -EPERM;
433                 switch (arg) {
434                 case KD_GRAPHICS:
435                         break;
436                 case KD_TEXT0:
437                 case KD_TEXT1:
438                         arg = KD_TEXT;
439                 case KD_TEXT:
440                         break;
441                 default:
442                         ret = -EINVAL;
443                         goto out;
444                 }
445                 console_lock();
446                 if (vc->vc_mode == (unsigned char) arg) {
447                         console_unlock();
448                         break;
449                 }
450                 vc->vc_mode = (unsigned char) arg;
451                 if (console != fg_console) {
452                         console_unlock();
453                         break;
454                 }
455                 /*
456                  * explicitly blank/unblank the screen if switching modes
457                  */
458                 if (arg == KD_TEXT)
459                         do_unblank_screen(1);
460                 else
461                         do_blank_screen(1);
462                 console_unlock();
463                 break;
464
465         case KDGETMODE:
466                 uival = vc->vc_mode;
467                 goto setint;
468
469         case KDMAPDISP:
470         case KDUNMAPDISP:
471                 /*
472                  * these work like a combination of mmap and KDENABIO.
473                  * this could be easily finished.
474                  */
475                 ret = -EINVAL;
476                 break;
477
478         case KDSKBMODE:
479                 if (!perm)
480                         return -EPERM;
481                 ret = vt_do_kdskbmode(console, arg);
482                 if (ret == 0)
483                         tty_ldisc_flush(tty);
484                 break;
485
486         case KDGKBMODE:
487                 uival = vt_do_kdgkbmode(console);
488                 ret = put_user(uival, (int __user *)arg);
489                 break;
490
491         /* this could be folded into KDSKBMODE, but for compatibility
492            reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
493         case KDSKBMETA:
494                 ret = vt_do_kdskbmeta(console, arg);
495                 break;
496
497         case KDGKBMETA:
498                 /* FIXME: should review whether this is worth locking */
499                 uival = vt_do_kdgkbmeta(console);
500         setint:
501                 ret = put_user(uival, (int __user *)arg);
502                 break;
503
504         case KDGETKEYCODE:
505         case KDSETKEYCODE:
506                 if(!capable(CAP_SYS_TTY_CONFIG))
507                         perm = 0;
508                 ret = vt_do_kbkeycode_ioctl(cmd, up, perm);
509                 break;
510
511         case KDGKBENT:
512         case KDSKBENT:
513                 ret = vt_do_kdsk_ioctl(cmd, up, perm, console);
514                 break;
515
516         case KDGKBSENT:
517         case KDSKBSENT:
518                 ret = vt_do_kdgkb_ioctl(cmd, up, perm);
519                 break;
520
521         /* Diacritical processing. Handled in keyboard.c as it has
522            to operate on the keyboard locks and structures */
523         case KDGKBDIACR:
524         case KDGKBDIACRUC:
525         case KDSKBDIACR:
526         case KDSKBDIACRUC:
527                 ret = vt_do_diacrit(cmd, up, perm);
528                 break;
529
530         /* the ioctls below read/set the flags usually shown in the leds */
531         /* don't use them - they will go away without warning */
532         case KDGKBLED:
533         case KDSKBLED:
534         case KDGETLED:
535         case KDSETLED:
536                 ret = vt_do_kdskled(console, cmd, arg, perm);
537                 break;
538
539         /*
540          * A process can indicate its willingness to accept signals
541          * generated by pressing an appropriate key combination.
542          * Thus, one can have a daemon that e.g. spawns a new console
543          * upon a keypress and then changes to it.
544          * See also the kbrequest field of inittab(5).
545          */
546         case KDSIGACCEPT:
547         {
548                 if (!perm || !capable(CAP_KILL))
549                         return -EPERM;
550                 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
551                         ret = -EINVAL;
552                 else {
553                         spin_lock_irq(&vt_spawn_con.lock);
554                         put_pid(vt_spawn_con.pid);
555                         vt_spawn_con.pid = get_pid(task_pid(current));
556                         vt_spawn_con.sig = arg;
557                         spin_unlock_irq(&vt_spawn_con.lock);
558                 }
559                 break;
560         }
561
562         case VT_SETMODE:
563         {
564                 struct vt_mode tmp;
565
566                 if (!perm)
567                         return -EPERM;
568                 if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
569                         ret = -EFAULT;
570                         goto out;
571                 }
572                 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
573                         ret = -EINVAL;
574                         goto out;
575                 }
576                 console_lock();
577                 vc->vt_mode = tmp;
578                 /* the frsig is ignored, so we set it to 0 */
579                 vc->vt_mode.frsig = 0;
580                 put_pid(vc->vt_pid);
581                 vc->vt_pid = get_pid(task_pid(current));
582                 /* no switch is required -- saw@shade.msu.ru */
583                 vc->vt_newvt = -1;
584                 console_unlock();
585                 break;
586         }
587
588         case VT_GETMODE:
589         {
590                 struct vt_mode tmp;
591                 int rc;
592
593                 console_lock();
594                 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
595                 console_unlock();
596
597                 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
598                 if (rc)
599                         ret = -EFAULT;
600                 break;
601         }
602
603         /*
604          * Returns global vt state. Note that VT 0 is always open, since
605          * it's an alias for the current VT, and people can't use it here.
606          * We cannot return state for more than 16 VTs, since v_state is short.
607          */
608         case VT_GETSTATE:
609         {
610                 struct vt_stat __user *vtstat = up;
611                 unsigned short state, mask;
612
613                 if (put_user(fg_console + 1, &vtstat->v_active))
614                         ret = -EFAULT;
615                 else {
616                         state = 1;      /* /dev/tty0 is always open */
617                         console_lock(); /* required by vt_in_use() */
618                         for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
619                                                         ++i, mask <<= 1)
620                                 if (vt_in_use(i))
621                                         state |= mask;
622                         console_unlock();
623                         ret = put_user(state, &vtstat->v_state);
624                 }
625                 break;
626         }
627
628         /*
629          * Returns the first available (non-opened) console.
630          */
631         case VT_OPENQRY:
632                 console_lock(); /* required by vt_in_use() */
633                 for (i = 0; i < MAX_NR_CONSOLES; ++i)
634                         if (!vt_in_use(i))
635                                 break;
636                 console_unlock();
637                 uival = i < MAX_NR_CONSOLES ? (i+1) : -1;
638                 goto setint;             
639
640         /*
641          * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
642          * with num >= 1 (switches to vt 0, our console, are not allowed, just
643          * to preserve sanity).
644          */
645         case VT_ACTIVATE:
646                 if (!perm)
647                         return -EPERM;
648                 if (arg == 0 || arg > MAX_NR_CONSOLES)
649                         ret =  -ENXIO;
650                 else {
651                         arg--;
652                         arg = array_index_nospec(arg, MAX_NR_CONSOLES);
653                         console_lock();
654                         ret = vc_allocate(arg);
655                         console_unlock();
656                         if (ret)
657                                 break;
658                         set_console(arg);
659                 }
660                 break;
661
662         case VT_SETACTIVATE:
663         {
664                 struct vt_setactivate vsa;
665
666                 if (!perm)
667                         return -EPERM;
668
669                 if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
670                                         sizeof(struct vt_setactivate))) {
671                         ret = -EFAULT;
672                         goto out;
673                 }
674                 if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
675                         ret = -ENXIO;
676                 else {
677                         vsa.console--;
678                         vsa.console = array_index_nospec(vsa.console,
679                                                          MAX_NR_CONSOLES);
680                         console_lock();
681                         ret = vc_allocate(vsa.console);
682                         if (ret == 0) {
683                                 struct vc_data *nvc;
684                                 /* This is safe providing we don't drop the
685                                    console sem between vc_allocate and
686                                    finishing referencing nvc */
687                                 nvc = vc_cons[vsa.console].d;
688                                 nvc->vt_mode = vsa.mode;
689                                 nvc->vt_mode.frsig = 0;
690                                 put_pid(nvc->vt_pid);
691                                 nvc->vt_pid = get_pid(task_pid(current));
692                         }
693                         console_unlock();
694                         if (ret)
695                                 break;
696                         /* Commence switch and lock */
697                         /* Review set_console locks */
698                         set_console(vsa.console);
699                 }
700                 break;
701         }
702
703         /*
704          * wait until the specified VT has been activated
705          */
706         case VT_WAITACTIVE:
707                 if (!perm)
708                         return -EPERM;
709                 if (arg == 0 || arg > MAX_NR_CONSOLES)
710                         ret = -ENXIO;
711                 else
712                         ret = vt_waitactive(arg);
713                 break;
714
715         /*
716          * If a vt is under process control, the kernel will not switch to it
717          * immediately, but postpone the operation until the process calls this
718          * ioctl, allowing the switch to complete.
719          *
720          * According to the X sources this is the behavior:
721          *      0:      pending switch-from not OK
722          *      1:      pending switch-from OK
723          *      2:      completed switch-to OK
724          */
725         case VT_RELDISP:
726                 if (!perm)
727                         return -EPERM;
728
729                 console_lock();
730                 if (vc->vt_mode.mode != VT_PROCESS) {
731                         console_unlock();
732                         ret = -EINVAL;
733                         break;
734                 }
735                 /*
736                  * Switching-from response
737                  */
738                 if (vc->vt_newvt >= 0) {
739                         if (arg == 0)
740                                 /*
741                                  * Switch disallowed, so forget we were trying
742                                  * to do it.
743                                  */
744                                 vc->vt_newvt = -1;
745
746                         else {
747                                 /*
748                                  * The current vt has been released, so
749                                  * complete the switch.
750                                  */
751                                 int newvt;
752                                 newvt = vc->vt_newvt;
753                                 vc->vt_newvt = -1;
754                                 ret = vc_allocate(newvt);
755                                 if (ret) {
756                                         console_unlock();
757                                         break;
758                                 }
759                                 /*
760                                  * When we actually do the console switch,
761                                  * make sure we are atomic with respect to
762                                  * other console switches..
763                                  */
764                                 complete_change_console(vc_cons[newvt].d);
765                         }
766                 } else {
767                         /*
768                          * Switched-to response
769                          */
770                         /*
771                          * If it's just an ACK, ignore it
772                          */
773                         if (arg != VT_ACKACQ)
774                                 ret = -EINVAL;
775                 }
776                 console_unlock();
777                 break;
778
779          /*
780           * Disallocate memory associated to VT (but leave VT1)
781           */
782          case VT_DISALLOCATE:
783                 if (arg > MAX_NR_CONSOLES) {
784                         ret = -ENXIO;
785                         break;
786                 }
787                 if (arg == 0)
788                         vt_disallocate_all();
789                 else
790                         ret = vt_disallocate(--arg);
791                 break;
792
793         case VT_RESIZE:
794         {
795                 struct vt_sizes __user *vtsizes = up;
796                 struct vc_data *vc;
797
798                 ushort ll,cc;
799                 if (!perm)
800                         return -EPERM;
801                 if (get_user(ll, &vtsizes->v_rows) ||
802                     get_user(cc, &vtsizes->v_cols))
803                         ret = -EFAULT;
804                 else {
805                         console_lock();
806                         for (i = 0; i < MAX_NR_CONSOLES; i++) {
807                                 vc = vc_cons[i].d;
808
809                                 if (vc) {
810                                         vc->vc_resize_user = 1;
811                                         /* FIXME: review v tty lock */
812                                         vc_resize(vc_cons[i].d, cc, ll);
813                                 }
814                         }
815                         console_unlock();
816                 }
817                 break;
818         }
819
820         case VT_RESIZEX:
821         {
822                 struct vt_consize v;
823                 if (!perm)
824                         return -EPERM;
825                 if (copy_from_user(&v, up, sizeof(struct vt_consize)))
826                         return -EFAULT;
827                 /* FIXME: Should check the copies properly */
828                 if (!v.v_vlin)
829                         v.v_vlin = vc->vc_scan_lines;
830                 if (v.v_clin) {
831                         int rows = v.v_vlin/v.v_clin;
832                         if (v.v_rows != rows) {
833                                 if (v.v_rows) /* Parameters don't add up */
834                                         return -EINVAL;
835                                 v.v_rows = rows;
836                         }
837                 }
838                 if (v.v_vcol && v.v_ccol) {
839                         int cols = v.v_vcol/v.v_ccol;
840                         if (v.v_cols != cols) {
841                                 if (v.v_cols)
842                                         return -EINVAL;
843                                 v.v_cols = cols;
844                         }
845                 }
846
847                 if (v.v_clin > 32)
848                         return -EINVAL;
849
850                 for (i = 0; i < MAX_NR_CONSOLES; i++) {
851                         struct vc_data *vcp;
852
853                         if (!vc_cons[i].d)
854                                 continue;
855                         console_lock();
856                         vcp = vc_cons[i].d;
857                         if (vcp) {
858                                 int ret;
859                                 int save_scan_lines = vcp->vc_scan_lines;
860                                 int save_cell_height = vcp->vc_cell_height;
861
862                                 if (v.v_vlin)
863                                         vcp->vc_scan_lines = v.v_vlin;
864                                 if (v.v_clin)
865                                         vcp->vc_cell_height = v.v_clin;
866                                 vcp->vc_resize_user = 1;
867                                 ret = vc_resize(vcp, v.v_cols, v.v_rows);
868                                 if (ret) {
869                                         vcp->vc_scan_lines = save_scan_lines;
870                                         vcp->vc_cell_height = save_cell_height;
871                                         console_unlock();
872                                         return ret;
873                                 }
874                         }
875                         console_unlock();
876                 }
877                 break;
878         }
879
880         case PIO_CMAP:
881                 if (!perm)
882                         ret = -EPERM;
883                 else
884                         ret = con_set_cmap(up);
885                 break;
886
887         case GIO_CMAP:
888                 ret = con_get_cmap(up);
889                 break;
890
891         case KDFONTOP: {
892                 if (copy_from_user(&op, up, sizeof(op))) {
893                         ret = -EFAULT;
894                         break;
895                 }
896                 if (!perm && op.op != KD_FONT_OP_GET)
897                         return -EPERM;
898                 ret = con_font_op(vc, &op);
899                 if (ret)
900                         break;
901                 if (copy_to_user(up, &op, sizeof(op)))
902                         ret = -EFAULT;
903                 break;
904         }
905
906         case PIO_SCRNMAP:
907                 if (!perm)
908                         ret = -EPERM;
909                 else
910                         ret = con_set_trans_old(up);
911                 break;
912
913         case GIO_SCRNMAP:
914                 ret = con_get_trans_old(up);
915                 break;
916
917         case PIO_UNISCRNMAP:
918                 if (!perm)
919                         ret = -EPERM;
920                 else
921                         ret = con_set_trans_new(up);
922                 break;
923
924         case GIO_UNISCRNMAP:
925                 ret = con_get_trans_new(up);
926                 break;
927
928         case PIO_UNIMAPCLR:
929                 if (!perm)
930                         return -EPERM;
931                 con_clear_unimap(vc);
932                 break;
933
934         case PIO_UNIMAP:
935         case GIO_UNIMAP:
936                 ret = do_unimap_ioctl(cmd, up, perm, vc);
937                 break;
938
939         case VT_LOCKSWITCH:
940                 if (!capable(CAP_SYS_TTY_CONFIG))
941                         return -EPERM;
942                 vt_dont_switch = true;
943                 break;
944         case VT_UNLOCKSWITCH:
945                 if (!capable(CAP_SYS_TTY_CONFIG))
946                         return -EPERM;
947                 vt_dont_switch = false;
948                 break;
949         case VT_GETHIFONTMASK:
950                 ret = put_user(vc->vc_hi_font_mask,
951                                         (unsigned short __user *)arg);
952                 break;
953         case VT_WAITEVENT:
954                 ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
955                 break;
956         default:
957                 ret = -ENOIOCTLCMD;
958         }
959 out:
960         return ret;
961 }
962
963 void reset_vc(struct vc_data *vc)
964 {
965         vc->vc_mode = KD_TEXT;
966         vt_reset_unicode(vc->vc_num);
967         vc->vt_mode.mode = VT_AUTO;
968         vc->vt_mode.waitv = 0;
969         vc->vt_mode.relsig = 0;
970         vc->vt_mode.acqsig = 0;
971         vc->vt_mode.frsig = 0;
972         put_pid(vc->vt_pid);
973         vc->vt_pid = NULL;
974         vc->vt_newvt = -1;
975         if (!in_interrupt())    /* Via keyboard.c:SAK() - akpm */
976                 reset_palette(vc);
977 }
978
979 void vc_SAK(struct work_struct *work)
980 {
981         struct vc *vc_con =
982                 container_of(work, struct vc, SAK_work);
983         struct vc_data *vc;
984         struct tty_struct *tty;
985
986         console_lock();
987         vc = vc_con->d;
988         if (vc) {
989                 /* FIXME: review tty ref counting */
990                 tty = vc->port.tty;
991                 /*
992                  * SAK should also work in all raw modes and reset
993                  * them properly.
994                  */
995                 if (tty)
996                         __do_SAK(tty);
997                 reset_vc(vc);
998         }
999         console_unlock();
1000 }
1001
1002 #ifdef CONFIG_COMPAT
1003
1004 struct compat_console_font_op {
1005         compat_uint_t op;        /* operation code KD_FONT_OP_* */
1006         compat_uint_t flags;     /* KD_FONT_FLAG_* */
1007         compat_uint_t width, height;     /* font size */
1008         compat_uint_t charcount;
1009         compat_caddr_t data;    /* font data with height fixed to 32 */
1010 };
1011
1012 static inline int
1013 compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
1014                          int perm, struct console_font_op *op, struct vc_data *vc)
1015 {
1016         int i;
1017
1018         if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
1019                 return -EFAULT;
1020         if (!perm && op->op != KD_FONT_OP_GET)
1021                 return -EPERM;
1022         op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
1023         i = con_font_op(vc, op);
1024         if (i)
1025                 return i;
1026         ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
1027         if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
1028                 return -EFAULT;
1029         return 0;
1030 }
1031
1032 struct compat_unimapdesc {
1033         unsigned short entry_ct;
1034         compat_caddr_t entries;
1035 };
1036
1037 static inline int
1038 compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
1039                          int perm, struct vc_data *vc)
1040 {
1041         struct compat_unimapdesc tmp;
1042         struct unipair __user *tmp_entries;
1043
1044         if (copy_from_user(&tmp, user_ud, sizeof tmp))
1045                 return -EFAULT;
1046         tmp_entries = compat_ptr(tmp.entries);
1047         switch (cmd) {
1048         case PIO_UNIMAP:
1049                 if (!perm)
1050                         return -EPERM;
1051                 return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
1052         case GIO_UNIMAP:
1053                 if (!perm && fg_console != vc->vc_num)
1054                         return -EPERM;
1055                 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
1056         }
1057         return 0;
1058 }
1059
1060 long vt_compat_ioctl(struct tty_struct *tty,
1061              unsigned int cmd, unsigned long arg)
1062 {
1063         struct vc_data *vc = tty->driver_data;
1064         struct console_font_op op;      /* used in multiple places here */
1065         void __user *up = (void __user *)arg;
1066         int perm;
1067         int ret = 0;
1068
1069         /*
1070          * To have permissions to do most of the vt ioctls, we either have
1071          * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
1072          */
1073         perm = 0;
1074         if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1075                 perm = 1;
1076
1077         switch (cmd) {
1078         /*
1079          * these need special handlers for incompatible data structures
1080          */
1081         case KDFONTOP:
1082                 ret = compat_kdfontop_ioctl(up, perm, &op, vc);
1083                 break;
1084
1085         case PIO_UNIMAP:
1086         case GIO_UNIMAP:
1087                 ret = compat_unimap_ioctl(cmd, up, perm, vc);
1088                 break;
1089
1090         /*
1091          * all these treat 'arg' as an integer
1092          */
1093         case KIOCSOUND:
1094         case KDMKTONE:
1095 #ifdef CONFIG_X86
1096         case KDADDIO:
1097         case KDDELIO:
1098 #endif
1099         case KDSETMODE:
1100         case KDMAPDISP:
1101         case KDUNMAPDISP:
1102         case KDSKBMODE:
1103         case KDSKBMETA:
1104         case KDSKBLED:
1105         case KDSETLED:
1106         case KDSIGACCEPT:
1107         case VT_ACTIVATE:
1108         case VT_WAITACTIVE:
1109         case VT_RELDISP:
1110         case VT_DISALLOCATE:
1111         case VT_RESIZE:
1112         case VT_RESIZEX:
1113                 goto fallback;
1114
1115         /*
1116          * the rest has a compatible data structure behind arg,
1117          * but we have to convert it to a proper 64 bit pointer.
1118          */
1119         default:
1120                 arg = (unsigned long)compat_ptr(arg);
1121                 goto fallback;
1122         }
1123
1124         return ret;
1125
1126 fallback:
1127         return vt_ioctl(tty, cmd, arg);
1128 }
1129
1130
1131 #endif /* CONFIG_COMPAT */
1132
1133
1134 /*
1135  * Performs the back end of a vt switch. Called under the console
1136  * semaphore.
1137  */
1138 static void complete_change_console(struct vc_data *vc)
1139 {
1140         unsigned char old_vc_mode;
1141         int old = fg_console;
1142
1143         last_console = fg_console;
1144
1145         /*
1146          * If we're switching, we could be going from KD_GRAPHICS to
1147          * KD_TEXT mode or vice versa, which means we need to blank or
1148          * unblank the screen later.
1149          */
1150         old_vc_mode = vc_cons[fg_console].d->vc_mode;
1151         switch_screen(vc);
1152
1153         /*
1154          * This can't appear below a successful kill_pid().  If it did,
1155          * then the *blank_screen operation could occur while X, having
1156          * received acqsig, is waking up on another processor.  This
1157          * condition can lead to overlapping accesses to the VGA range
1158          * and the framebuffer (causing system lockups).
1159          *
1160          * To account for this we duplicate this code below only if the
1161          * controlling process is gone and we've called reset_vc.
1162          */
1163         if (old_vc_mode != vc->vc_mode) {
1164                 if (vc->vc_mode == KD_TEXT)
1165                         do_unblank_screen(1);
1166                 else
1167                         do_blank_screen(1);
1168         }
1169
1170         /*
1171          * If this new console is under process control, send it a signal
1172          * telling it that it has acquired. Also check if it has died and
1173          * clean up (similar to logic employed in change_console())
1174          */
1175         if (vc->vt_mode.mode == VT_PROCESS) {
1176                 /*
1177                  * Send the signal as privileged - kill_pid() will
1178                  * tell us if the process has gone or something else
1179                  * is awry
1180                  */
1181                 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
1182                 /*
1183                  * The controlling process has died, so we revert back to
1184                  * normal operation. In this case, we'll also change back
1185                  * to KD_TEXT mode. I'm not sure if this is strictly correct
1186                  * but it saves the agony when the X server dies and the screen
1187                  * remains blanked due to KD_GRAPHICS! It would be nice to do
1188                  * this outside of VT_PROCESS but there is no single process
1189                  * to account for and tracking tty count may be undesirable.
1190                  */
1191                         reset_vc(vc);
1192
1193                         if (old_vc_mode != vc->vc_mode) {
1194                                 if (vc->vc_mode == KD_TEXT)
1195                                         do_unblank_screen(1);
1196                                 else
1197                                         do_blank_screen(1);
1198                         }
1199                 }
1200         }
1201
1202         /*
1203          * Wake anyone waiting for their VT to activate
1204          */
1205         vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
1206         return;
1207 }
1208
1209 /*
1210  * Performs the front-end of a vt switch
1211  */
1212 void change_console(struct vc_data *new_vc)
1213 {
1214         struct vc_data *vc;
1215
1216         if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1217                 return;
1218
1219         /*
1220          * If this vt is in process mode, then we need to handshake with
1221          * that process before switching. Essentially, we store where that
1222          * vt wants to switch to and wait for it to tell us when it's done
1223          * (via VT_RELDISP ioctl).
1224          *
1225          * We also check to see if the controlling process still exists.
1226          * If it doesn't, we reset this vt to auto mode and continue.
1227          * This is a cheap way to track process control. The worst thing
1228          * that can happen is: we send a signal to a process, it dies, and
1229          * the switch gets "lost" waiting for a response; hopefully, the
1230          * user will try again, we'll detect the process is gone (unless
1231          * the user waits just the right amount of time :-) and revert the
1232          * vt to auto control.
1233          */
1234         vc = vc_cons[fg_console].d;
1235         if (vc->vt_mode.mode == VT_PROCESS) {
1236                 /*
1237                  * Send the signal as privileged - kill_pid() will
1238                  * tell us if the process has gone or something else
1239                  * is awry.
1240                  *
1241                  * We need to set vt_newvt *before* sending the signal or we
1242                  * have a race.
1243                  */
1244                 vc->vt_newvt = new_vc->vc_num;
1245                 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
1246                         /*
1247                          * It worked. Mark the vt to switch to and
1248                          * return. The process needs to send us a
1249                          * VT_RELDISP ioctl to complete the switch.
1250                          */
1251                         return;
1252                 }
1253
1254                 /*
1255                  * The controlling process has died, so we revert back to
1256                  * normal operation. In this case, we'll also change back
1257                  * to KD_TEXT mode. I'm not sure if this is strictly correct
1258                  * but it saves the agony when the X server dies and the screen
1259                  * remains blanked due to KD_GRAPHICS! It would be nice to do
1260                  * this outside of VT_PROCESS but there is no single process
1261                  * to account for and tracking tty count may be undesirable.
1262                  */
1263                 reset_vc(vc);
1264
1265                 /*
1266                  * Fall through to normal (VT_AUTO) handling of the switch...
1267                  */
1268         }
1269
1270         /*
1271          * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1272          */
1273         if (vc->vc_mode == KD_GRAPHICS)
1274                 return;
1275
1276         complete_change_console(new_vc);
1277 }
1278
1279 /* Perform a kernel triggered VT switch for suspend/resume */
1280
1281 static int disable_vt_switch;
1282
1283 int vt_move_to_console(unsigned int vt, int alloc)
1284 {
1285         int prev;
1286
1287         console_lock();
1288         /* Graphics mode - up to X */
1289         if (disable_vt_switch) {
1290                 console_unlock();
1291                 return 0;
1292         }
1293         prev = fg_console;
1294
1295         if (alloc && vc_allocate(vt)) {
1296                 /* we can't have a free VC for now. Too bad,
1297                  * we don't want to mess the screen for now. */
1298                 console_unlock();
1299                 return -ENOSPC;
1300         }
1301
1302         if (set_console(vt)) {
1303                 /*
1304                  * We're unable to switch to the SUSPEND_CONSOLE.
1305                  * Let the calling function know so it can decide
1306                  * what to do.
1307                  */
1308                 console_unlock();
1309                 return -EIO;
1310         }
1311         console_unlock();
1312         if (vt_waitactive(vt + 1)) {
1313                 pr_debug("Suspend: Can't switch VCs.");
1314                 return -EINTR;
1315         }
1316         return prev;
1317 }
1318
1319 /*
1320  * Normally during a suspend, we allocate a new console and switch to it.
1321  * When we resume, we switch back to the original console.  This switch
1322  * can be slow, so on systems where the framebuffer can handle restoration
1323  * of video registers anyways, there's little point in doing the console
1324  * switch.  This function allows you to disable it by passing it '0'.
1325  */
1326 void pm_set_vt_switch(int do_switch)
1327 {
1328         console_lock();
1329         disable_vt_switch = !do_switch;
1330         console_unlock();
1331 }
1332 EXPORT_SYMBOL(pm_set_vt_switch);