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