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