blob: e8efb270dc8f960d8d694caa86a1ded60017bb5a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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>
Arnd Bergmanne9216652009-08-06 15:09:28 +020017#include <linux/compat.h>
Alan Cox8d233552009-09-19 13:13:25 -070018#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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>
Samuel Thibault04c71972007-10-16 23:27:04 -070026#include <linux/consolemap.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070027#include <linux/signal.h>
Josh Tripletta5e04b72012-11-18 21:27:41 -080028#include <linux/suspend.h>
Emmanuel Colbusbcc8ca02005-06-28 20:44:49 -070029#include <linux/timex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/io.h>
32#include <asm/uaccess.h>
33
Gustavo A. R. Silva0ec459e2018-08-16 15:30:38 -050034#include <linux/nospec.h>
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/kbd_kern.h>
37#include <linux/vt_kern.h>
38#include <linux/kbd_diacr.h>
39#include <linux/selection.h>
40
Andrew Johnsonb257bc02007-03-16 13:38:24 -080041char vt_dont_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042extern struct tty_driver *console_driver;
43
44#define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count)
45#define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons)
46
47/*
48 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
49 * experimentation and study of X386 SYSV handling.
50 *
51 * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
52 * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
53 * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
54 * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
55 * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
56 * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
57 * to the current console is done by the main ioctl code.
58 */
59
60#ifdef CONFIG_X86
61#include <linux/syscalls.h>
62#endif
63
64static void complete_change_console(struct vc_data *vc);
65
66/*
Alan Cox8b92e872009-09-19 13:13:24 -070067 * User space VT_EVENT handlers
68 */
69
70struct vt_event_wait {
71 struct list_head list;
72 struct vt_event event;
73 int done;
74};
75
76static LIST_HEAD(vt_events);
77static DEFINE_SPINLOCK(vt_event_lock);
78static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
79
80/**
81 * vt_event_post
82 * @event: the event that occurred
83 * @old: old console
84 * @new: new console
85 *
86 * Post an VT event to interested VT handlers
87 */
88
89void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
90{
91 struct list_head *pos, *head;
92 unsigned long flags;
93 int wake = 0;
94
95 spin_lock_irqsave(&vt_event_lock, flags);
96 head = &vt_events;
97
98 list_for_each(pos, head) {
99 struct vt_event_wait *ve = list_entry(pos,
100 struct vt_event_wait, list);
101 if (!(ve->event.event & event))
102 continue;
103 ve->event.event = event;
104 /* kernel view is consoles 0..n-1, user space view is
105 console 1..n with 0 meaning current, so we must bias */
Alan Cox308efab2009-11-19 13:30:36 +0000106 ve->event.oldev = old + 1;
107 ve->event.newev = new + 1;
Alan Cox8b92e872009-09-19 13:13:24 -0700108 wake = 1;
109 ve->done = 1;
110 }
111 spin_unlock_irqrestore(&vt_event_lock, flags);
112 if (wake)
113 wake_up_interruptible(&vt_event_waitqueue);
114}
115
Rabin Vincenta7b12922012-05-21 13:38:42 +0530116static void __vt_event_queue(struct vt_event_wait *vw)
117{
118 unsigned long flags;
119 /* Prepare the event */
120 INIT_LIST_HEAD(&vw->list);
121 vw->done = 0;
122 /* Queue our event */
123 spin_lock_irqsave(&vt_event_lock, flags);
124 list_add(&vw->list, &vt_events);
125 spin_unlock_irqrestore(&vt_event_lock, flags);
126}
127
128static void __vt_event_wait(struct vt_event_wait *vw)
129{
130 /* Wait for it to pass */
131 wait_event_interruptible(vt_event_waitqueue, vw->done);
132}
133
134static void __vt_event_dequeue(struct vt_event_wait *vw)
135{
136 unsigned long flags;
137
138 /* Dequeue it */
139 spin_lock_irqsave(&vt_event_lock, flags);
140 list_del(&vw->list);
141 spin_unlock_irqrestore(&vt_event_lock, flags);
142}
143
Alan Cox8b92e872009-09-19 13:13:24 -0700144/**
145 * vt_event_wait - wait for an event
146 * @vw: our event
147 *
148 * Waits for an event to occur which completes our vt_event_wait
149 * structure. On return the structure has wv->done set to 1 for success
150 * or 0 if some event such as a signal ended the wait.
151 */
152
153static void vt_event_wait(struct vt_event_wait *vw)
154{
Rabin Vincenta7b12922012-05-21 13:38:42 +0530155 __vt_event_queue(vw);
156 __vt_event_wait(vw);
157 __vt_event_dequeue(vw);
Alan Cox8b92e872009-09-19 13:13:24 -0700158}
159
160/**
161 * vt_event_wait_ioctl - event ioctl handler
162 * @arg: argument to ioctl
163 *
164 * Implement the VT_WAITEVENT ioctl using the VT event interface
165 */
166
167static int vt_event_wait_ioctl(struct vt_event __user *event)
168{
169 struct vt_event_wait vw;
170
171 if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
172 return -EFAULT;
173 /* Highest supported event for now */
174 if (vw.event.event & ~VT_MAX_EVENT)
175 return -EINVAL;
176
177 vt_event_wait(&vw);
178 /* If it occurred report it */
179 if (vw.done) {
180 if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
181 return -EFAULT;
182 return 0;
183 }
184 return -EINTR;
185}
186
187/**
188 * vt_waitactive - active console wait
189 * @event: event code
190 * @n: new console
191 *
192 * Helper for event waits. Used to implement the legacy
193 * event waiting ioctls in terms of events
194 */
195
196int vt_waitactive(int n)
197{
198 struct vt_event_wait vw;
199 do {
Alan Cox8b92e872009-09-19 13:13:24 -0700200 vw.event.event = VT_EVENT_SWITCH;
Rabin Vincenta7b12922012-05-21 13:38:42 +0530201 __vt_event_queue(&vw);
202 if (n == fg_console + 1) {
203 __vt_event_dequeue(&vw);
204 break;
205 }
206 __vt_event_wait(&vw);
207 __vt_event_dequeue(&vw);
Alan Cox8b92e872009-09-19 13:13:24 -0700208 if (vw.done == 0)
209 return -EINTR;
Alan Cox308efab2009-11-19 13:30:36 +0000210 } while (vw.event.newev != n);
Alan Cox8b92e872009-09-19 13:13:24 -0700211 return 0;
212}
213
214/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * these are the valid i/o ports we're allowed to change. they map all the
216 * video ports
217 */
218#define GPFIRST 0x3b4
219#define GPLAST 0x3df
220#define GPNUM (GPLAST - GPFIRST + 1)
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224static inline int
225do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
226{
227 struct consolefontdesc cfdarg;
228 int i;
229
230 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
231 return -EFAULT;
232
233 switch (cmd) {
234 case PIO_FONTX:
235 if (!perm)
236 return -EPERM;
237 op->op = KD_FONT_OP_SET;
238 op->flags = KD_FONT_FLAG_OLD;
239 op->width = 8;
240 op->height = cfdarg.charheight;
241 op->charcount = cfdarg.charcount;
242 op->data = cfdarg.chardata;
243 return con_font_op(vc_cons[fg_console].d, op);
244 case GIO_FONTX: {
245 op->op = KD_FONT_OP_GET;
246 op->flags = KD_FONT_FLAG_OLD;
247 op->width = 8;
248 op->height = cfdarg.charheight;
249 op->charcount = cfdarg.charcount;
250 op->data = cfdarg.chardata;
251 i = con_font_op(vc_cons[fg_console].d, op);
252 if (i)
253 return i;
254 cfdarg.charheight = op->height;
255 cfdarg.charcount = op->charcount;
256 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
257 return -EFAULT;
258 return 0;
259 }
260 }
261 return -EINVAL;
262}
263
264static inline int
265do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
266{
267 struct unimapdesc tmp;
268
269 if (copy_from_user(&tmp, user_ud, sizeof tmp))
270 return -EFAULT;
271 if (tmp.entries)
272 if (!access_ok(VERIFY_WRITE, tmp.entries,
273 tmp.entry_ct*sizeof(struct unipair)))
274 return -EFAULT;
275 switch (cmd) {
276 case PIO_UNIMAP:
277 if (!perm)
278 return -EPERM;
279 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
280 case GIO_UNIMAP:
281 if (!perm && fg_console != vc->vc_num)
282 return -EPERM;
283 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
284 }
285 return 0;
286}
287
Peter Hurley421b40a2013-05-17 12:41:03 -0400288/* deallocate a single console, if possible (leave 0) */
289static int vt_disallocate(unsigned int vc_num)
290{
291 struct vc_data *vc = NULL;
292 int ret = 0;
293
Peter Hurley421b40a2013-05-17 12:41:03 -0400294 console_lock();
295 if (VT_BUSY(vc_num))
296 ret = -EBUSY;
Ross Lagerwallef223fb2013-06-14 23:24:25 +0100297 else if (vc_num)
Peter Hurley421b40a2013-05-17 12:41:03 -0400298 vc = vc_deallocate(vc_num);
299 console_unlock();
300
301 if (vc && vc_num >= MIN_NR_CONSOLES) {
302 tty_port_destroy(&vc->port);
303 kfree(vc);
304 }
305
306 return ret;
307}
308
309/* deallocate all unused consoles, but leave 0 */
310static void vt_disallocate_all(void)
311{
312 struct vc_data *vc[MAX_NR_CONSOLES];
313 int i;
314
315 console_lock();
316 for (i = 1; i < MAX_NR_CONSOLES; i++)
317 if (!VT_BUSY(i))
318 vc[i] = vc_deallocate(i);
319 else
320 vc[i] = NULL;
321 console_unlock();
322
323 for (i = 1; i < MAX_NR_CONSOLES; i++) {
324 if (vc[i] && i >= MIN_NR_CONSOLES) {
325 tty_port_destroy(&vc[i]->port);
326 kfree(vc[i]);
327 }
328 }
329}
Alan Cox8b92e872009-09-19 13:13:24 -0700330
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/*
333 * We handle the console-specific ioctl's here. We allow the
334 * capability to modify any console, not just the fg_console.
335 */
Alan Cox6caa76b2011-02-14 16:27:22 +0000336int vt_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 unsigned int cmd, unsigned long arg)
338{
Alan Coxc9f19e92009-01-02 13:47:26 +0000339 struct vc_data *vc = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct console_font_op op; /* used in multiple places here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 unsigned int console;
342 unsigned char ucval;
Graham Gower1e0ad282010-10-27 15:33:00 -0700343 unsigned int uival;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 void __user *up = (void __user *)arg;
345 int i, perm;
Alan Cox9cc3c222008-04-30 00:53:26 -0700346 int ret = 0;
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 console = vc->vc_num;
349
Alan Cox9cc3c222008-04-30 00:53:26 -0700350
351 if (!vc_cons_allocated(console)) { /* impossible? */
352 ret = -ENOIOCTLCMD;
353 goto out;
354 }
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /*
358 * To have permissions to do most of the vt ioctls, we either have
359 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
360 */
361 perm = 0;
362 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
363 perm = 1;
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 switch (cmd) {
Alan Coxe6885102008-10-13 10:36:40 +0100366 case TIOCLINUX:
Jiri Slabya1159022009-06-22 18:42:18 +0100367 ret = tioclinux(tty, arg);
368 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 case KIOCSOUND:
370 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000371 return -EPERM;
Arnd Bergmann2c4e9672010-08-28 20:35:17 -0700372 /*
373 * The use of PIT_TICK_RATE is historic, it used to be
374 * the platform-dependent CLOCK_TICK_RATE between 2.6.12
375 * and 2.6.36, which was a minor but unfortunate ABI
Alan Cox4001d7b2012-03-02 14:59:20 +0000376 * change. kd_mksound is locked by the input layer.
Arnd Bergmann2c4e9672010-08-28 20:35:17 -0700377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (arg)
Arnd Bergmann2c4e9672010-08-28 20:35:17 -0700379 arg = PIT_TICK_RATE / arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 kd_mksound(arg, 0);
Alan Cox9cc3c222008-04-30 00:53:26 -0700381 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 case KDMKTONE:
384 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000385 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 {
387 unsigned int ticks, count;
388
389 /*
390 * Generate the tone for the appropriate number of ticks.
391 * If the time is zero, turn off sound ourselves.
392 */
Nicholas Mc Guire4c0d9b12015-02-09 10:53:25 -0500393 ticks = msecs_to_jiffies((arg >> 16) & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 count = ticks ? (arg & 0xffff) : 0;
395 if (count)
Arnd Bergmann2c4e9672010-08-28 20:35:17 -0700396 count = PIT_TICK_RATE / count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 kd_mksound(count, ticks);
Alan Cox9cc3c222008-04-30 00:53:26 -0700398 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
401 case KDGKBTYPE:
402 /*
Alan Cox4001d7b2012-03-02 14:59:20 +0000403 * this is naïve.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 */
405 ucval = KB_101;
Alan Cox079c9532012-02-28 14:49:23 +0000406 ret = put_user(ucval, (char __user *)arg);
407 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /*
410 * These cannot be implemented on any machine that implements
411 * ioperm() in user level (such as Alpha PCs) or not at all.
412 *
413 * XXX: you should never use these, just call ioperm directly..
414 */
415#ifdef CONFIG_X86
416 case KDADDIO:
417 case KDDELIO:
418 /*
419 * KDADDIO and KDDELIO may be able to add ports beyond what
420 * we reject here, but to be safe...
Alan Cox4001d7b2012-03-02 14:59:20 +0000421 *
422 * These are locked internally via sys_ioperm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 */
Alan Cox9cc3c222008-04-30 00:53:26 -0700424 if (arg < GPFIRST || arg > GPLAST) {
425 ret = -EINVAL;
426 break;
427 }
428 ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
429 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 case KDENABIO:
432 case KDDISABIO:
Alan Cox9cc3c222008-04-30 00:53:26 -0700433 ret = sys_ioperm(GPFIRST, GPNUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 (cmd == KDENABIO)) ? -ENXIO : 0;
Alan Cox9cc3c222008-04-30 00:53:26 -0700435 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436#endif
437
438 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
439
440 case KDKBDREP:
441 {
442 struct kbd_repeat kbrep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox4001d7b2012-03-02 14:59:20 +0000445 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Alan Cox9cc3c222008-04-30 00:53:26 -0700447 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
448 ret = -EFAULT;
449 break;
450 }
451 ret = kbd_rate(&kbrep);
452 if (ret)
453 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
Alan Cox9cc3c222008-04-30 00:53:26 -0700455 ret = -EFAULT;
456 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458
459 case KDSETMODE:
460 /*
461 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
462 * doesn't do a whole lot. i'm not sure if it should do any
463 * restoration of modes or what...
464 *
465 * XXX It should at least call into the driver, fbdev's definitely
466 * need to restore their engine state. --BenH
467 */
468 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000469 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 switch (arg) {
471 case KD_GRAPHICS:
472 break;
473 case KD_TEXT0:
474 case KD_TEXT1:
475 arg = KD_TEXT;
476 case KD_TEXT:
477 break;
478 default:
Alan Cox9cc3c222008-04-30 00:53:26 -0700479 ret = -EINVAL;
480 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Alan Cox4001d7b2012-03-02 14:59:20 +0000482 /* FIXME: this needs the console lock extending */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (vc->vc_mode == (unsigned char) arg)
Alan Cox9cc3c222008-04-30 00:53:26 -0700484 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 vc->vc_mode = (unsigned char) arg;
486 if (console != fg_console)
Alan Cox9cc3c222008-04-30 00:53:26 -0700487 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /*
489 * explicitly blank/unblank the screen if switching modes
490 */
Torben Hohnac751ef2011-01-25 15:07:35 -0800491 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (arg == KD_TEXT)
493 do_unblank_screen(1);
494 else
495 do_blank_screen(1);
Torben Hohnac751ef2011-01-25 15:07:35 -0800496 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700497 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 case KDGETMODE:
Graham Gower1e0ad282010-10-27 15:33:00 -0700500 uival = vc->vc_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 goto setint;
502
503 case KDMAPDISP:
504 case KDUNMAPDISP:
505 /*
506 * these work like a combination of mmap and KDENABIO.
507 * this could be easily finished.
508 */
Alan Cox9cc3c222008-04-30 00:53:26 -0700509 ret = -EINVAL;
510 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 case KDSKBMODE:
513 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000514 return -EPERM;
Alan Cox079c9532012-02-28 14:49:23 +0000515 ret = vt_do_kdskbmode(console, arg);
516 if (ret == 0)
517 tty_ldisc_flush(tty);
Alan Cox9cc3c222008-04-30 00:53:26 -0700518 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 case KDGKBMODE:
Alan Cox079c9532012-02-28 14:49:23 +0000521 uival = vt_do_kdgkbmode(console);
522 ret = put_user(uival, (int __user *)arg);
523 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 /* this could be folded into KDSKBMODE, but for compatibility
526 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
527 case KDSKBMETA:
Alan Cox079c9532012-02-28 14:49:23 +0000528 ret = vt_do_kdskbmeta(console, arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700529 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 case KDGKBMETA:
Alan Cox079c9532012-02-28 14:49:23 +0000532 /* FIXME: should review whether this is worth locking */
533 uival = vt_do_kdgkbmeta(console);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 setint:
Graham Gower1e0ad282010-10-27 15:33:00 -0700535 ret = put_user(uival, (int __user *)arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700536 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 case KDGETKEYCODE:
539 case KDSETKEYCODE:
540 if(!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -0700541 perm = 0;
Alan Cox079c9532012-02-28 14:49:23 +0000542 ret = vt_do_kbkeycode_ioctl(cmd, up, perm);
Alan Cox9cc3c222008-04-30 00:53:26 -0700543 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 case KDGKBENT:
546 case KDSKBENT:
Alan Cox079c9532012-02-28 14:49:23 +0000547 ret = vt_do_kdsk_ioctl(cmd, up, perm, console);
Alan Cox9cc3c222008-04-30 00:53:26 -0700548 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 case KDGKBSENT:
551 case KDSKBSENT:
Alan Cox079c9532012-02-28 14:49:23 +0000552 ret = vt_do_kdgkb_ioctl(cmd, up, perm);
Alan Cox9cc3c222008-04-30 00:53:26 -0700553 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Alan Cox247ff8e2012-02-24 12:47:11 +0000555 /* Diacritical processing. Handled in keyboard.c as it has
556 to operate on the keyboard locks and structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 case KDGKBDIACR:
Samuel Thibault04c71972007-10-16 23:27:04 -0700558 case KDGKBDIACRUC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 case KDSKBDIACR:
Samuel Thibault04c71972007-10-16 23:27:04 -0700560 case KDSKBDIACRUC:
Alan Cox247ff8e2012-02-24 12:47:11 +0000561 ret = vt_do_diacrit(cmd, up, perm);
Alan Cox9cc3c222008-04-30 00:53:26 -0700562 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* the ioctls below read/set the flags usually shown in the leds */
565 /* don't use them - they will go away without warning */
566 case KDGKBLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 case KDSKBLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 case KDGETLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 case KDSETLED:
Alan Cox079c9532012-02-28 14:49:23 +0000570 ret = vt_do_kdskled(console, cmd, arg, perm);
Alan Cox9cc3c222008-04-30 00:53:26 -0700571 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /*
574 * A process can indicate its willingness to accept signals
575 * generated by pressing an appropriate key combination.
576 * Thus, one can have a daemon that e.g. spawns a new console
577 * upon a keypress and then changes to it.
578 * See also the kbrequest field of inittab(5).
579 */
580 case KDSIGACCEPT:
581 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (!perm || !capable(CAP_KILL))
Alan Cox4001d7b2012-03-02 14:59:20 +0000583 return -EPERM;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700584 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
Alan Cox9cc3c222008-04-30 00:53:26 -0700585 ret = -EINVAL;
586 else {
587 spin_lock_irq(&vt_spawn_con.lock);
588 put_pid(vt_spawn_con.pid);
589 vt_spawn_con.pid = get_pid(task_pid(current));
590 vt_spawn_con.sig = arg;
591 spin_unlock_irq(&vt_spawn_con.lock);
592 }
593 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595
596 case VT_SETMODE:
597 {
598 struct vt_mode tmp;
599
600 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000601 return -EPERM;
Alan Cox9cc3c222008-04-30 00:53:26 -0700602 if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
603 ret = -EFAULT;
604 goto out;
605 }
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -0700606 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
Alan Cox9cc3c222008-04-30 00:53:26 -0700607 ret = -EINVAL;
608 goto out;
609 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800610 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 vc->vt_mode = tmp;
612 /* the frsig is ignored, so we set it to 0 */
613 vc->vt_mode.frsig = 0;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -0800614 put_pid(vc->vt_pid);
615 vc->vt_pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /* no switch is required -- saw@shade.msu.ru */
617 vc->vt_newvt = -1;
Torben Hohnac751ef2011-01-25 15:07:35 -0800618 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700619 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
622 case VT_GETMODE:
623 {
624 struct vt_mode tmp;
625 int rc;
626
Torben Hohnac751ef2011-01-25 15:07:35 -0800627 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
Torben Hohnac751ef2011-01-25 15:07:35 -0800629 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
Alan Cox9cc3c222008-04-30 00:53:26 -0700632 if (rc)
633 ret = -EFAULT;
634 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
637 /*
638 * Returns global vt state. Note that VT 0 is always open, since
639 * it's an alias for the current VT, and people can't use it here.
640 * We cannot return state for more than 16 VTs, since v_state is short.
641 */
642 case VT_GETSTATE:
643 {
644 struct vt_stat __user *vtstat = up;
645 unsigned short state, mask;
646
Alan Cox4001d7b2012-03-02 14:59:20 +0000647 /* Review: FIXME: Console lock ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (put_user(fg_console + 1, &vtstat->v_active))
Alan Cox9cc3c222008-04-30 00:53:26 -0700649 ret = -EFAULT;
650 else {
651 state = 1; /* /dev/tty0 is always open */
652 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
653 ++i, mask <<= 1)
654 if (VT_IS_IN_USE(i))
655 state |= mask;
656 ret = put_user(state, &vtstat->v_state);
657 }
658 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
661 /*
662 * Returns the first available (non-opened) console.
663 */
664 case VT_OPENQRY:
Alan Cox4001d7b2012-03-02 14:59:20 +0000665 /* FIXME: locking ? - but then this is a stupid API */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 for (i = 0; i < MAX_NR_CONSOLES; ++i)
667 if (! VT_IS_IN_USE(i))
668 break;
Graham Gower1e0ad282010-10-27 15:33:00 -0700669 uival = i < MAX_NR_CONSOLES ? (i+1) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 goto setint;
671
672 /*
673 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
674 * with num >= 1 (switches to vt 0, our console, are not allowed, just
675 * to preserve sanity).
676 */
677 case VT_ACTIVATE:
678 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000679 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (arg == 0 || arg > MAX_NR_CONSOLES)
Alan Cox9cc3c222008-04-30 00:53:26 -0700681 ret = -ENXIO;
682 else {
683 arg--;
Torben Hohnac751ef2011-01-25 15:07:35 -0800684 console_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700685 ret = vc_allocate(arg);
Torben Hohnac751ef2011-01-25 15:07:35 -0800686 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700687 if (ret)
688 break;
689 set_console(arg);
690 }
691 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Alan Coxd3b5cff2009-09-19 13:13:26 -0700693 case VT_SETACTIVATE:
694 {
695 struct vt_setactivate vsa;
696
697 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000698 return -EPERM;
Alan Coxd3b5cff2009-09-19 13:13:26 -0700699
700 if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
Jiri Slabya09efb02009-10-01 15:43:59 -0700701 sizeof(struct vt_setactivate))) {
702 ret = -EFAULT;
703 goto out;
704 }
Alan Coxd3b5cff2009-09-19 13:13:26 -0700705 if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
706 ret = -ENXIO;
707 else {
Gustavo A. R. Silva0ec459e2018-08-16 15:30:38 -0500708 vsa.console = array_index_nospec(vsa.console,
709 MAX_NR_CONSOLES + 1);
Alan Coxd3b5cff2009-09-19 13:13:26 -0700710 vsa.console--;
Torben Hohnac751ef2011-01-25 15:07:35 -0800711 console_lock();
Alan Coxd3b5cff2009-09-19 13:13:26 -0700712 ret = vc_allocate(vsa.console);
713 if (ret == 0) {
714 struct vc_data *nvc;
715 /* This is safe providing we don't drop the
716 console sem between vc_allocate and
717 finishing referencing nvc */
718 nvc = vc_cons[vsa.console].d;
719 nvc->vt_mode = vsa.mode;
720 nvc->vt_mode.frsig = 0;
721 put_pid(nvc->vt_pid);
722 nvc->vt_pid = get_pid(task_pid(current));
723 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800724 console_unlock();
Alan Coxd3b5cff2009-09-19 13:13:26 -0700725 if (ret)
726 break;
727 /* Commence switch and lock */
Alan Cox4001d7b2012-03-02 14:59:20 +0000728 /* Review set_console locks */
Jiri Olsad6378372011-02-11 15:39:28 +0100729 set_console(vsa.console);
Alan Coxd3b5cff2009-09-19 13:13:26 -0700730 }
Jiri Olsad6378372011-02-11 15:39:28 +0100731 break;
Alan Coxd3b5cff2009-09-19 13:13:26 -0700732 }
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /*
735 * wait until the specified VT has been activated
736 */
737 case VT_WAITACTIVE:
738 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000739 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (arg == 0 || arg > MAX_NR_CONSOLES)
Alan Cox9cc3c222008-04-30 00:53:26 -0700741 ret = -ENXIO;
Alan Cox99cceb4e2012-03-02 14:59:49 +0000742 else
Alan Cox8b92e872009-09-19 13:13:24 -0700743 ret = vt_waitactive(arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700744 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 /*
747 * If a vt is under process control, the kernel will not switch to it
748 * immediately, but postpone the operation until the process calls this
749 * ioctl, allowing the switch to complete.
750 *
751 * According to the X sources this is the behavior:
752 * 0: pending switch-from not OK
753 * 1: pending switch-from OK
754 * 2: completed switch-to OK
755 */
756 case VT_RELDISP:
757 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000758 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Alan Cox4001d7b2012-03-02 14:59:20 +0000760 console_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700761 if (vc->vt_mode.mode != VT_PROCESS) {
Alan Cox4001d7b2012-03-02 14:59:20 +0000762 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700763 ret = -EINVAL;
764 break;
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /*
767 * Switching-from response
768 */
769 if (vc->vt_newvt >= 0) {
770 if (arg == 0)
771 /*
772 * Switch disallowed, so forget we were trying
773 * to do it.
774 */
775 vc->vt_newvt = -1;
776
777 else {
778 /*
779 * The current vt has been released, so
780 * complete the switch.
781 */
782 int newvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 newvt = vc->vt_newvt;
784 vc->vt_newvt = -1;
Alan Cox9cc3c222008-04-30 00:53:26 -0700785 ret = vc_allocate(newvt);
786 if (ret) {
Torben Hohnac751ef2011-01-25 15:07:35 -0800787 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700788 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790 /*
791 * When we actually do the console switch,
792 * make sure we are atomic with respect to
793 * other console switches..
794 */
795 complete_change_console(vc_cons[newvt].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700797 } else {
798 /*
799 * Switched-to response
800 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /*
802 * If it's just an ACK, ignore it
803 */
Alan Cox9cc3c222008-04-30 00:53:26 -0700804 if (arg != VT_ACKACQ)
805 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800807 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700808 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /*
811 * Disallocate memory associated to VT (but leave VT1)
812 */
813 case VT_DISALLOCATE:
Alan Cox9cc3c222008-04-30 00:53:26 -0700814 if (arg > MAX_NR_CONSOLES) {
815 ret = -ENXIO;
816 break;
817 }
Peter Hurley421b40a2013-05-17 12:41:03 -0400818 if (arg == 0)
819 vt_disallocate_all();
820 else
821 ret = vt_disallocate(--arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700822 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 case VT_RESIZE:
825 {
826 struct vt_sizes __user *vtsizes = up;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700827 struct vc_data *vc;
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 ushort ll,cc;
830 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000831 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (get_user(ll, &vtsizes->v_rows) ||
833 get_user(cc, &vtsizes->v_cols))
Alan Cox9cc3c222008-04-30 00:53:26 -0700834 ret = -EFAULT;
835 else {
Torben Hohnac751ef2011-01-25 15:07:35 -0800836 console_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700837 for (i = 0; i < MAX_NR_CONSOLES; i++) {
838 vc = vc_cons[i].d;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700839
Alan Cox9cc3c222008-04-30 00:53:26 -0700840 if (vc) {
841 vc->vc_resize_user = 1;
Alan Cox4001d7b2012-03-02 14:59:20 +0000842 /* FIXME: review v tty lock */
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100843 vc_resize(vc_cons[i].d, cc, ll);
Alan Cox9cc3c222008-04-30 00:53:26 -0700844 }
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700845 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800846 console_unlock();
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700847 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700848 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850
851 case VT_RESIZEX:
852 {
Al Viro97b58fa2017-09-29 12:34:13 -0400853 struct vt_consize v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000855 return -EPERM;
Al Viro97b58fa2017-09-29 12:34:13 -0400856 if (copy_from_user(&v, up, sizeof(struct vt_consize)))
857 return -EFAULT;
Alan Cox9cc3c222008-04-30 00:53:26 -0700858 /* FIXME: Should check the copies properly */
Al Viro97b58fa2017-09-29 12:34:13 -0400859 if (!v.v_vlin)
860 v.v_vlin = vc->vc_scan_lines;
861 if (v.v_clin) {
862 int rows = v.v_vlin/v.v_clin;
863 if (v.v_rows != rows) {
864 if (v.v_rows) /* Parameters don't add up */
865 return -EINVAL;
866 v.v_rows = rows;
867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Al Viro97b58fa2017-09-29 12:34:13 -0400869 if (v.v_vcol && v.v_ccol) {
870 int cols = v.v_vcol/v.v_ccol;
871 if (v.v_cols != cols) {
872 if (v.v_cols)
873 return -EINVAL;
874 v.v_cols = cols;
875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
Al Viro97b58fa2017-09-29 12:34:13 -0400878 if (v.v_clin > 32)
879 return -EINVAL;
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 for (i = 0; i < MAX_NR_CONSOLES; i++) {
Eric Dumazet160fbca2020-02-10 11:07:21 -0800882 struct vc_data *vcp;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (!vc_cons[i].d)
885 continue;
Torben Hohnac751ef2011-01-25 15:07:35 -0800886 console_lock();
Eric Dumazet160fbca2020-02-10 11:07:21 -0800887 vcp = vc_cons[i].d;
888 if (vcp) {
889 if (v.v_vlin)
890 vcp->vc_scan_lines = v.v_vlin;
891 if (v.v_clin)
892 vcp->vc_font.height = v.v_clin;
893 vcp->vc_resize_user = 1;
894 vc_resize(vcp, v.v_cols, v.v_rows);
895 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800896 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700898 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900
901 case PIO_FONT: {
902 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000903 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 op.op = KD_FONT_OP_SET;
905 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
906 op.width = 8;
907 op.height = 0;
908 op.charcount = 256;
909 op.data = up;
Alan Cox9cc3c222008-04-30 00:53:26 -0700910 ret = con_font_op(vc_cons[fg_console].d, &op);
911 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913
914 case GIO_FONT: {
915 op.op = KD_FONT_OP_GET;
916 op.flags = KD_FONT_FLAG_OLD;
917 op.width = 8;
918 op.height = 32;
919 op.charcount = 256;
920 op.data = up;
Alan Cox9cc3c222008-04-30 00:53:26 -0700921 ret = con_font_op(vc_cons[fg_console].d, &op);
922 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
925 case PIO_CMAP:
926 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700927 ret = -EPERM;
928 else
929 ret = con_set_cmap(up);
930 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 case GIO_CMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -0700933 ret = con_get_cmap(up);
934 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 case PIO_FONTX:
937 case GIO_FONTX:
Alan Cox9cc3c222008-04-30 00:53:26 -0700938 ret = do_fontx_ioctl(cmd, up, perm, &op);
939 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 case PIO_FONTRESET:
942 {
943 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000944 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946#ifdef BROKEN_GRAPHICS_PROGRAMS
947 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
948 font is not saved. */
Alan Cox9cc3c222008-04-30 00:53:26 -0700949 ret = -ENOSYS;
950 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951#else
952 {
953 op.op = KD_FONT_OP_SET_DEFAULT;
954 op.data = NULL;
Alan Cox9cc3c222008-04-30 00:53:26 -0700955 ret = con_font_op(vc_cons[fg_console].d, &op);
956 if (ret)
957 break;
Alan Cox5d1a33f2012-04-24 11:06:06 +0100958 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 con_set_default_unimap(vc_cons[fg_console].d);
Alan Cox5d1a33f2012-04-24 11:06:06 +0100960 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700961 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963#endif
964 }
965
966 case KDFONTOP: {
Alan Cox9cc3c222008-04-30 00:53:26 -0700967 if (copy_from_user(&op, up, sizeof(op))) {
968 ret = -EFAULT;
969 break;
970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (!perm && op.op != KD_FONT_OP_GET)
Alan Cox4001d7b2012-03-02 14:59:20 +0000972 return -EPERM;
Alan Cox9cc3c222008-04-30 00:53:26 -0700973 ret = con_font_op(vc, &op);
974 if (ret)
975 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (copy_to_user(up, &op, sizeof(op)))
Alan Cox9cc3c222008-04-30 00:53:26 -0700977 ret = -EFAULT;
978 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980
981 case PIO_SCRNMAP:
982 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700983 ret = -EPERM;
Alan Cox5d1a33f2012-04-24 11:06:06 +0100984 else
Alan Cox9cc3c222008-04-30 00:53:26 -0700985 ret = con_set_trans_old(up);
986 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 case GIO_SCRNMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -0700989 ret = con_get_trans_old(up);
990 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 case PIO_UNISCRNMAP:
993 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700994 ret = -EPERM;
Alan Cox5d1a33f2012-04-24 11:06:06 +0100995 else
Alan Cox9cc3c222008-04-30 00:53:26 -0700996 ret = con_set_trans_new(up);
997 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 case GIO_UNISCRNMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -07001000 ret = con_get_trans_new(up);
1001 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 case PIO_UNIMAPCLR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +00001005 return -EPERM;
Jiri Slabyf8b0c2e2016-06-23 13:34:36 +02001006 con_clear_unimap(vc);
Alan Cox9cc3c222008-04-30 00:53:26 -07001007 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 case PIO_UNIMAP:
1010 case GIO_UNIMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -07001011 ret = do_unimap_ioctl(cmd, up, perm, vc);
1012 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 case VT_LOCKSWITCH:
1015 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox4001d7b2012-03-02 14:59:20 +00001016 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 vt_dont_switch = 1;
Alan Cox9cc3c222008-04-30 00:53:26 -07001018 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 case VT_UNLOCKSWITCH:
1020 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox4001d7b2012-03-02 14:59:20 +00001021 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 vt_dont_switch = 0;
Alan Cox9cc3c222008-04-30 00:53:26 -07001023 break;
Samuel Thibault533475d2006-08-27 01:23:39 -07001024 case VT_GETHIFONTMASK:
Alan Cox9cc3c222008-04-30 00:53:26 -07001025 ret = put_user(vc->vc_hi_font_mask,
1026 (unsigned short __user *)arg);
1027 break;
Alan Cox8b92e872009-09-19 13:13:24 -07001028 case VT_WAITEVENT:
1029 ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
1030 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 default:
Alan Cox9cc3c222008-04-30 00:53:26 -07001032 ret = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001034out:
Alan Cox9cc3c222008-04-30 00:53:26 -07001035 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038void reset_vc(struct vc_data *vc)
1039{
1040 vc->vc_mode = KD_TEXT;
Alan Cox079c9532012-02-28 14:49:23 +00001041 vt_reset_unicode(vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 vc->vt_mode.mode = VT_AUTO;
1043 vc->vt_mode.waitv = 0;
1044 vc->vt_mode.relsig = 0;
1045 vc->vt_mode.acqsig = 0;
1046 vc->vt_mode.frsig = 0;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001047 put_pid(vc->vt_pid);
1048 vc->vt_pid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 vc->vt_newvt = -1;
1050 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1051 reset_palette(vc);
1052}
1053
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001054void vc_SAK(struct work_struct *work)
1055{
1056 struct vc *vc_con =
1057 container_of(work, struct vc, SAK_work);
1058 struct vc_data *vc;
1059 struct tty_struct *tty;
1060
Torben Hohnac751ef2011-01-25 15:07:35 -08001061 console_lock();
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001062 vc = vc_con->d;
1063 if (vc) {
Alan Cox079c9532012-02-28 14:49:23 +00001064 /* FIXME: review tty ref counting */
Alan Cox8ce73262010-06-01 22:52:56 +02001065 tty = vc->port.tty;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001066 /*
1067 * SAK should also work in all raw modes and reset
1068 * them properly.
1069 */
1070 if (tty)
1071 __do_SAK(tty);
1072 reset_vc(vc);
1073 }
Torben Hohnac751ef2011-01-25 15:07:35 -08001074 console_unlock();
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001075}
1076
Arnd Bergmanne9216652009-08-06 15:09:28 +02001077#ifdef CONFIG_COMPAT
1078
1079struct compat_consolefontdesc {
1080 unsigned short charcount; /* characters in font (256 or 512) */
1081 unsigned short charheight; /* scan lines per character (1-32) */
1082 compat_caddr_t chardata; /* font data in expanded form */
1083};
1084
1085static inline int
1086compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd,
1087 int perm, struct console_font_op *op)
1088{
1089 struct compat_consolefontdesc cfdarg;
1090 int i;
1091
1092 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
1093 return -EFAULT;
1094
1095 switch (cmd) {
1096 case PIO_FONTX:
1097 if (!perm)
1098 return -EPERM;
1099 op->op = KD_FONT_OP_SET;
1100 op->flags = KD_FONT_FLAG_OLD;
1101 op->width = 8;
1102 op->height = cfdarg.charheight;
1103 op->charcount = cfdarg.charcount;
1104 op->data = compat_ptr(cfdarg.chardata);
1105 return con_font_op(vc_cons[fg_console].d, op);
1106 case GIO_FONTX:
1107 op->op = KD_FONT_OP_GET;
1108 op->flags = KD_FONT_FLAG_OLD;
1109 op->width = 8;
1110 op->height = cfdarg.charheight;
1111 op->charcount = cfdarg.charcount;
1112 op->data = compat_ptr(cfdarg.chardata);
1113 i = con_font_op(vc_cons[fg_console].d, op);
1114 if (i)
1115 return i;
1116 cfdarg.charheight = op->height;
1117 cfdarg.charcount = op->charcount;
1118 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
1119 return -EFAULT;
1120 return 0;
1121 }
1122 return -EINVAL;
1123}
1124
1125struct compat_console_font_op {
1126 compat_uint_t op; /* operation code KD_FONT_OP_* */
1127 compat_uint_t flags; /* KD_FONT_FLAG_* */
1128 compat_uint_t width, height; /* font size */
1129 compat_uint_t charcount;
1130 compat_caddr_t data; /* font data with height fixed to 32 */
1131};
1132
1133static inline int
1134compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
1135 int perm, struct console_font_op *op, struct vc_data *vc)
1136{
1137 int i;
1138
1139 if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
1140 return -EFAULT;
1141 if (!perm && op->op != KD_FONT_OP_GET)
1142 return -EPERM;
1143 op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
Arnd Bergmanne9216652009-08-06 15:09:28 +02001144 i = con_font_op(vc, op);
1145 if (i)
1146 return i;
1147 ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
1148 if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
1149 return -EFAULT;
1150 return 0;
1151}
1152
1153struct compat_unimapdesc {
1154 unsigned short entry_ct;
1155 compat_caddr_t entries;
1156};
1157
1158static inline int
1159compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
1160 int perm, struct vc_data *vc)
1161{
1162 struct compat_unimapdesc tmp;
1163 struct unipair __user *tmp_entries;
1164
1165 if (copy_from_user(&tmp, user_ud, sizeof tmp))
1166 return -EFAULT;
1167 tmp_entries = compat_ptr(tmp.entries);
1168 if (tmp_entries)
1169 if (!access_ok(VERIFY_WRITE, tmp_entries,
1170 tmp.entry_ct*sizeof(struct unipair)))
1171 return -EFAULT;
1172 switch (cmd) {
1173 case PIO_UNIMAP:
1174 if (!perm)
1175 return -EPERM;
1176 return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
1177 case GIO_UNIMAP:
1178 if (!perm && fg_console != vc->vc_num)
1179 return -EPERM;
1180 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
1181 }
1182 return 0;
1183}
1184
Alan Cox6caa76b2011-02-14 16:27:22 +00001185long vt_compat_ioctl(struct tty_struct *tty,
Arnd Bergmanne9216652009-08-06 15:09:28 +02001186 unsigned int cmd, unsigned long arg)
1187{
1188 struct vc_data *vc = tty->driver_data;
1189 struct console_font_op op; /* used in multiple places here */
Arnd Bergmanne9216652009-08-06 15:09:28 +02001190 unsigned int console;
1191 void __user *up = (void __user *)arg;
1192 int perm;
1193 int ret = 0;
1194
1195 console = vc->vc_num;
1196
Arnd Bergmanne9216652009-08-06 15:09:28 +02001197 if (!vc_cons_allocated(console)) { /* impossible? */
1198 ret = -ENOIOCTLCMD;
1199 goto out;
1200 }
1201
1202 /*
1203 * To have permissions to do most of the vt ioctls, we either have
1204 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
1205 */
1206 perm = 0;
1207 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1208 perm = 1;
1209
Arnd Bergmanne9216652009-08-06 15:09:28 +02001210 switch (cmd) {
1211 /*
1212 * these need special handlers for incompatible data structures
1213 */
1214 case PIO_FONTX:
1215 case GIO_FONTX:
1216 ret = compat_fontx_ioctl(cmd, up, perm, &op);
1217 break;
1218
1219 case KDFONTOP:
1220 ret = compat_kdfontop_ioctl(up, perm, &op, vc);
1221 break;
1222
1223 case PIO_UNIMAP:
1224 case GIO_UNIMAP:
Andreas Schwab4b1fe772009-09-28 20:10:02 +02001225 ret = compat_unimap_ioctl(cmd, up, perm, vc);
Arnd Bergmanne9216652009-08-06 15:09:28 +02001226 break;
1227
1228 /*
1229 * all these treat 'arg' as an integer
1230 */
1231 case KIOCSOUND:
1232 case KDMKTONE:
1233#ifdef CONFIG_X86
1234 case KDADDIO:
1235 case KDDELIO:
1236#endif
1237 case KDSETMODE:
1238 case KDMAPDISP:
1239 case KDUNMAPDISP:
1240 case KDSKBMODE:
1241 case KDSKBMETA:
1242 case KDSKBLED:
1243 case KDSETLED:
1244 case KDSIGACCEPT:
1245 case VT_ACTIVATE:
1246 case VT_WAITACTIVE:
1247 case VT_RELDISP:
1248 case VT_DISALLOCATE:
1249 case VT_RESIZE:
1250 case VT_RESIZEX:
1251 goto fallback;
1252
1253 /*
1254 * the rest has a compatible data structure behind arg,
1255 * but we have to convert it to a proper 64 bit pointer.
1256 */
1257 default:
1258 arg = (unsigned long)compat_ptr(arg);
1259 goto fallback;
1260 }
1261out:
Arnd Bergmanne9216652009-08-06 15:09:28 +02001262 return ret;
1263
1264fallback:
Alan Cox6caa76b2011-02-14 16:27:22 +00001265 return vt_ioctl(tty, cmd, arg);
Arnd Bergmanne9216652009-08-06 15:09:28 +02001266}
1267
1268
1269#endif /* CONFIG_COMPAT */
1270
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272/*
Alan Coxd3b5cff2009-09-19 13:13:26 -07001273 * Performs the back end of a vt switch. Called under the console
1274 * semaphore.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 */
1276static void complete_change_console(struct vc_data *vc)
1277{
1278 unsigned char old_vc_mode;
Alan Cox8b92e872009-09-19 13:13:24 -07001279 int old = fg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 last_console = fg_console;
1282
1283 /*
1284 * If we're switching, we could be going from KD_GRAPHICS to
1285 * KD_TEXT mode or vice versa, which means we need to blank or
1286 * unblank the screen later.
1287 */
1288 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1289 switch_screen(vc);
1290
1291 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001292 * This can't appear below a successful kill_pid(). If it did,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 * then the *blank_screen operation could occur while X, having
1294 * received acqsig, is waking up on another processor. This
1295 * condition can lead to overlapping accesses to the VGA range
1296 * and the framebuffer (causing system lockups).
1297 *
1298 * To account for this we duplicate this code below only if the
1299 * controlling process is gone and we've called reset_vc.
1300 */
1301 if (old_vc_mode != vc->vc_mode) {
1302 if (vc->vc_mode == KD_TEXT)
1303 do_unblank_screen(1);
1304 else
1305 do_blank_screen(1);
1306 }
1307
1308 /*
1309 * If this new console is under process control, send it a signal
1310 * telling it that it has acquired. Also check if it has died and
1311 * clean up (similar to logic employed in change_console())
1312 */
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001313 if (vc->vt_mode.mode == VT_PROCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001315 * Send the signal as privileged - kill_pid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 * tell us if the process has gone or something else
1317 * is awry
1318 */
Eric W. Biedermanbde0d2c92006-10-02 02:17:14 -07001319 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /*
1321 * The controlling process has died, so we revert back to
1322 * normal operation. In this case, we'll also change back
1323 * to KD_TEXT mode. I'm not sure if this is strictly correct
1324 * but it saves the agony when the X server dies and the screen
1325 * remains blanked due to KD_GRAPHICS! It would be nice to do
1326 * this outside of VT_PROCESS but there is no single process
1327 * to account for and tracking tty count may be undesirable.
1328 */
1329 reset_vc(vc);
1330
1331 if (old_vc_mode != vc->vc_mode) {
1332 if (vc->vc_mode == KD_TEXT)
1333 do_unblank_screen(1);
1334 else
1335 do_blank_screen(1);
1336 }
1337 }
1338 }
1339
1340 /*
1341 * Wake anyone waiting for their VT to activate
1342 */
Alan Cox8b92e872009-09-19 13:13:24 -07001343 vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return;
1345}
1346
1347/*
1348 * Performs the front-end of a vt switch
1349 */
1350void change_console(struct vc_data *new_vc)
1351{
1352 struct vc_data *vc;
1353
1354 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1355 return;
1356
1357 /*
1358 * If this vt is in process mode, then we need to handshake with
1359 * that process before switching. Essentially, we store where that
1360 * vt wants to switch to and wait for it to tell us when it's done
1361 * (via VT_RELDISP ioctl).
1362 *
1363 * We also check to see if the controlling process still exists.
1364 * If it doesn't, we reset this vt to auto mode and continue.
1365 * This is a cheap way to track process control. The worst thing
1366 * that can happen is: we send a signal to a process, it dies, and
1367 * the switch gets "lost" waiting for a response; hopefully, the
1368 * user will try again, we'll detect the process is gone (unless
1369 * the user waits just the right amount of time :-) and revert the
1370 * vt to auto control.
1371 */
1372 vc = vc_cons[fg_console].d;
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001373 if (vc->vt_mode.mode == VT_PROCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001375 * Send the signal as privileged - kill_pid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 * tell us if the process has gone or something else
Jan Lübbea64314e2007-09-29 18:47:51 +02001377 * is awry.
1378 *
1379 * We need to set vt_newvt *before* sending the signal or we
1380 * have a race.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 */
Jan Lübbea64314e2007-09-29 18:47:51 +02001382 vc->vt_newvt = new_vc->vc_num;
Eric W. Biedermanbde0d2c92006-10-02 02:17:14 -07001383 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /*
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001385 * It worked. Mark the vt to switch to and
1386 * return. The process needs to send us a
1387 * VT_RELDISP ioctl to complete the switch.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 */
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001389 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391
1392 /*
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001393 * The controlling process has died, so we revert back to
1394 * normal operation. In this case, we'll also change back
1395 * to KD_TEXT mode. I'm not sure if this is strictly correct
1396 * but it saves the agony when the X server dies and the screen
1397 * remains blanked due to KD_GRAPHICS! It would be nice to do
1398 * this outside of VT_PROCESS but there is no single process
1399 * to account for and tracking tty count may be undesirable.
1400 */
1401 reset_vc(vc);
1402
1403 /*
1404 * Fall through to normal (VT_AUTO) handling of the switch...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 */
1406 }
1407
1408 /*
1409 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1410 */
1411 if (vc->vc_mode == KD_GRAPHICS)
1412 return;
1413
1414 complete_change_console(new_vc);
1415}
Alan Cox8d233552009-09-19 13:13:25 -07001416
1417/* Perform a kernel triggered VT switch for suspend/resume */
1418
1419static int disable_vt_switch;
1420
1421int vt_move_to_console(unsigned int vt, int alloc)
1422{
1423 int prev;
1424
Torben Hohnac751ef2011-01-25 15:07:35 -08001425 console_lock();
Alan Cox8d233552009-09-19 13:13:25 -07001426 /* Graphics mode - up to X */
1427 if (disable_vt_switch) {
Torben Hohnac751ef2011-01-25 15:07:35 -08001428 console_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001429 return 0;
1430 }
1431 prev = fg_console;
1432
1433 if (alloc && vc_allocate(vt)) {
1434 /* we can't have a free VC for now. Too bad,
1435 * we don't want to mess the screen for now. */
Torben Hohnac751ef2011-01-25 15:07:35 -08001436 console_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001437 return -ENOSPC;
1438 }
1439
1440 if (set_console(vt)) {
1441 /*
1442 * We're unable to switch to the SUSPEND_CONSOLE.
1443 * Let the calling function know so it can decide
1444 * what to do.
1445 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001446 console_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001447 return -EIO;
1448 }
Torben Hohnac751ef2011-01-25 15:07:35 -08001449 console_unlock();
Jiri Slaby797938b2009-08-11 23:20:41 +02001450 if (vt_waitactive(vt + 1)) {
Alan Cox8d233552009-09-19 13:13:25 -07001451 pr_debug("Suspend: Can't switch VCs.");
1452 return -EINTR;
1453 }
1454 return prev;
1455}
1456
1457/*
1458 * Normally during a suspend, we allocate a new console and switch to it.
1459 * When we resume, we switch back to the original console. This switch
1460 * can be slow, so on systems where the framebuffer can handle restoration
1461 * of video registers anyways, there's little point in doing the console
1462 * switch. This function allows you to disable it by passing it '0'.
1463 */
1464void pm_set_vt_switch(int do_switch)
1465{
Torben Hohnac751ef2011-01-25 15:07:35 -08001466 console_lock();
Alan Cox8d233552009-09-19 13:13:25 -07001467 disable_vt_switch = !do_switch;
Torben Hohnac751ef2011-01-25 15:07:35 -08001468 console_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001469}
1470EXPORT_SYMBOL(pm_set_vt_switch);