blob: 28c5b246a72cbd89a41e2a403d084c73874a0f91 [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
Jiri Slaby2e1c84e2020-02-19 08:39:48 +010041bool vt_dont_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jiri Slaby8c3bc92d2020-02-19 08:39:44 +010043static inline bool vt_in_use(unsigned int i)
44{
45 extern struct tty_driver *console_driver;
46
47 return console_driver->ttys[i] && console_driver->ttys[i]->count;
48}
49
50static inline bool vt_busy(int i)
51{
52 if (vt_in_use(i))
53 return true;
54 if (i == fg_console)
55 return true;
56 if (vc_is_sel(vc_cons[i].d))
57 return true;
58
59 return false;
60}
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/*
63 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
64 * experimentation and study of X386 SYSV handling.
65 *
66 * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
67 * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
68 * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
69 * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
70 * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
71 * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
72 * to the current console is done by the main ioctl code.
73 */
74
75#ifdef CONFIG_X86
76#include <linux/syscalls.h>
77#endif
78
79static void complete_change_console(struct vc_data *vc);
80
81/*
Alan Cox8b92e872009-09-19 13:13:24 -070082 * User space VT_EVENT handlers
83 */
84
85struct vt_event_wait {
86 struct list_head list;
87 struct vt_event event;
88 int done;
89};
90
91static LIST_HEAD(vt_events);
92static DEFINE_SPINLOCK(vt_event_lock);
93static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
94
95/**
96 * vt_event_post
97 * @event: the event that occurred
98 * @old: old console
99 * @new: new console
100 *
101 * Post an VT event to interested VT handlers
102 */
103
104void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
105{
106 struct list_head *pos, *head;
107 unsigned long flags;
108 int wake = 0;
109
110 spin_lock_irqsave(&vt_event_lock, flags);
111 head = &vt_events;
112
113 list_for_each(pos, head) {
114 struct vt_event_wait *ve = list_entry(pos,
115 struct vt_event_wait, list);
116 if (!(ve->event.event & event))
117 continue;
118 ve->event.event = event;
119 /* kernel view is consoles 0..n-1, user space view is
120 console 1..n with 0 meaning current, so we must bias */
Alan Cox308efab2009-11-19 13:30:36 +0000121 ve->event.oldev = old + 1;
122 ve->event.newev = new + 1;
Alan Cox8b92e872009-09-19 13:13:24 -0700123 wake = 1;
124 ve->done = 1;
125 }
126 spin_unlock_irqrestore(&vt_event_lock, flags);
127 if (wake)
128 wake_up_interruptible(&vt_event_waitqueue);
129}
130
Rabin Vincenta7b12922012-05-21 13:38:42 +0530131static void __vt_event_queue(struct vt_event_wait *vw)
132{
133 unsigned long flags;
134 /* Prepare the event */
135 INIT_LIST_HEAD(&vw->list);
136 vw->done = 0;
137 /* Queue our event */
138 spin_lock_irqsave(&vt_event_lock, flags);
139 list_add(&vw->list, &vt_events);
140 spin_unlock_irqrestore(&vt_event_lock, flags);
141}
142
143static void __vt_event_wait(struct vt_event_wait *vw)
144{
145 /* Wait for it to pass */
146 wait_event_interruptible(vt_event_waitqueue, vw->done);
147}
148
149static void __vt_event_dequeue(struct vt_event_wait *vw)
150{
151 unsigned long flags;
152
153 /* Dequeue it */
154 spin_lock_irqsave(&vt_event_lock, flags);
155 list_del(&vw->list);
156 spin_unlock_irqrestore(&vt_event_lock, flags);
157}
158
Alan Cox8b92e872009-09-19 13:13:24 -0700159/**
160 * vt_event_wait - wait for an event
161 * @vw: our event
162 *
163 * Waits for an event to occur which completes our vt_event_wait
164 * structure. On return the structure has wv->done set to 1 for success
165 * or 0 if some event such as a signal ended the wait.
166 */
167
168static void vt_event_wait(struct vt_event_wait *vw)
169{
Rabin Vincenta7b12922012-05-21 13:38:42 +0530170 __vt_event_queue(vw);
171 __vt_event_wait(vw);
172 __vt_event_dequeue(vw);
Alan Cox8b92e872009-09-19 13:13:24 -0700173}
174
175/**
176 * vt_event_wait_ioctl - event ioctl handler
177 * @arg: argument to ioctl
178 *
179 * Implement the VT_WAITEVENT ioctl using the VT event interface
180 */
181
182static int vt_event_wait_ioctl(struct vt_event __user *event)
183{
184 struct vt_event_wait vw;
185
186 if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
187 return -EFAULT;
188 /* Highest supported event for now */
189 if (vw.event.event & ~VT_MAX_EVENT)
190 return -EINVAL;
191
192 vt_event_wait(&vw);
193 /* If it occurred report it */
194 if (vw.done) {
195 if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
196 return -EFAULT;
197 return 0;
198 }
199 return -EINTR;
200}
201
202/**
203 * vt_waitactive - active console wait
204 * @event: event code
205 * @n: new console
206 *
207 * Helper for event waits. Used to implement the legacy
208 * event waiting ioctls in terms of events
209 */
210
211int vt_waitactive(int n)
212{
213 struct vt_event_wait vw;
214 do {
Alan Cox8b92e872009-09-19 13:13:24 -0700215 vw.event.event = VT_EVENT_SWITCH;
Rabin Vincenta7b12922012-05-21 13:38:42 +0530216 __vt_event_queue(&vw);
217 if (n == fg_console + 1) {
218 __vt_event_dequeue(&vw);
219 break;
220 }
221 __vt_event_wait(&vw);
222 __vt_event_dequeue(&vw);
Alan Cox8b92e872009-09-19 13:13:24 -0700223 if (vw.done == 0)
224 return -EINTR;
Alan Cox308efab2009-11-19 13:30:36 +0000225 } while (vw.event.newev != n);
Alan Cox8b92e872009-09-19 13:13:24 -0700226 return 0;
227}
228
229/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * these are the valid i/o ports we're allowed to change. they map all the
231 * video ports
232 */
233#define GPFIRST 0x3b4
234#define GPLAST 0x3df
235#define GPNUM (GPLAST - GPFIRST + 1)
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239static inline int
240do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
241{
242 struct consolefontdesc cfdarg;
243 int i;
244
245 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
246 return -EFAULT;
247
248 switch (cmd) {
249 case PIO_FONTX:
250 if (!perm)
251 return -EPERM;
252 op->op = KD_FONT_OP_SET;
253 op->flags = KD_FONT_FLAG_OLD;
254 op->width = 8;
255 op->height = cfdarg.charheight;
256 op->charcount = cfdarg.charcount;
257 op->data = cfdarg.chardata;
258 return con_font_op(vc_cons[fg_console].d, op);
259 case GIO_FONTX: {
260 op->op = KD_FONT_OP_GET;
261 op->flags = KD_FONT_FLAG_OLD;
262 op->width = 8;
263 op->height = cfdarg.charheight;
264 op->charcount = cfdarg.charcount;
265 op->data = cfdarg.chardata;
266 i = con_font_op(vc_cons[fg_console].d, op);
267 if (i)
268 return i;
269 cfdarg.charheight = op->height;
270 cfdarg.charcount = op->charcount;
271 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
272 return -EFAULT;
273 return 0;
274 }
275 }
276 return -EINVAL;
277}
278
279static inline int
280do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
281{
282 struct unimapdesc tmp;
283
284 if (copy_from_user(&tmp, user_ud, sizeof tmp))
285 return -EFAULT;
286 if (tmp.entries)
287 if (!access_ok(VERIFY_WRITE, tmp.entries,
288 tmp.entry_ct*sizeof(struct unipair)))
289 return -EFAULT;
290 switch (cmd) {
291 case PIO_UNIMAP:
292 if (!perm)
293 return -EPERM;
294 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
295 case GIO_UNIMAP:
296 if (!perm && fg_console != vc->vc_num)
297 return -EPERM;
298 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
299 }
300 return 0;
301}
302
Peter Hurley421b40a2013-05-17 12:41:03 -0400303/* deallocate a single console, if possible (leave 0) */
304static int vt_disallocate(unsigned int vc_num)
305{
306 struct vc_data *vc = NULL;
307 int ret = 0;
308
Peter Hurley421b40a2013-05-17 12:41:03 -0400309 console_lock();
Jiri Slaby8c3bc92d2020-02-19 08:39:44 +0100310 if (vt_busy(vc_num))
Peter Hurley421b40a2013-05-17 12:41:03 -0400311 ret = -EBUSY;
Ross Lagerwallef223fb2013-06-14 23:24:25 +0100312 else if (vc_num)
Peter Hurley421b40a2013-05-17 12:41:03 -0400313 vc = vc_deallocate(vc_num);
314 console_unlock();
315
Eric Biggers6bc9bf72020-03-21 20:43:04 -0700316 if (vc && vc_num >= MIN_NR_CONSOLES)
317 tty_port_put(&vc->port);
Peter Hurley421b40a2013-05-17 12:41:03 -0400318
319 return ret;
320}
321
322/* deallocate all unused consoles, but leave 0 */
323static void vt_disallocate_all(void)
324{
325 struct vc_data *vc[MAX_NR_CONSOLES];
326 int i;
327
328 console_lock();
329 for (i = 1; i < MAX_NR_CONSOLES; i++)
Jiri Slaby8c3bc92d2020-02-19 08:39:44 +0100330 if (!vt_busy(i))
Peter Hurley421b40a2013-05-17 12:41:03 -0400331 vc[i] = vc_deallocate(i);
332 else
333 vc[i] = NULL;
334 console_unlock();
335
336 for (i = 1; i < MAX_NR_CONSOLES; i++) {
Eric Biggers6bc9bf72020-03-21 20:43:04 -0700337 if (vc[i] && i >= MIN_NR_CONSOLES)
338 tty_port_put(&vc[i]->port);
Peter Hurley421b40a2013-05-17 12:41:03 -0400339 }
340}
Alan Cox8b92e872009-09-19 13:13:24 -0700341
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343/*
344 * We handle the console-specific ioctl's here. We allow the
345 * capability to modify any console, not just the fg_console.
346 */
Alan Cox6caa76b2011-02-14 16:27:22 +0000347int vt_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unsigned int cmd, unsigned long arg)
349{
Alan Coxc9f19e92009-01-02 13:47:26 +0000350 struct vc_data *vc = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct console_font_op op; /* used in multiple places here */
Eric Biggers35d64802020-02-24 00:03:26 -0800352 unsigned int console = vc->vc_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 unsigned char ucval;
Graham Gower1e0ad282010-10-27 15:33:00 -0700354 unsigned int uival;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 void __user *up = (void __user *)arg;
356 int i, perm;
Alan Cox9cc3c222008-04-30 00:53:26 -0700357 int ret = 0;
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /*
360 * To have permissions to do most of the vt ioctls, we either have
361 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
362 */
363 perm = 0;
364 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
365 perm = 1;
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 switch (cmd) {
Alan Coxe6885102008-10-13 10:36:40 +0100368 case TIOCLINUX:
Jiri Slabya1159022009-06-22 18:42:18 +0100369 ret = tioclinux(tty, arg);
370 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 case KIOCSOUND:
372 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000373 return -EPERM;
Arnd Bergmann2c4e9672010-08-28 20:35:17 -0700374 /*
375 * The use of PIT_TICK_RATE is historic, it used to be
376 * the platform-dependent CLOCK_TICK_RATE between 2.6.12
377 * and 2.6.36, which was a minor but unfortunate ABI
Alan Cox4001d7b2012-03-02 14:59:20 +0000378 * change. kd_mksound is locked by the input layer.
Arnd Bergmann2c4e9672010-08-28 20:35:17 -0700379 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (arg)
Arnd Bergmann2c4e9672010-08-28 20:35:17 -0700381 arg = PIT_TICK_RATE / arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 kd_mksound(arg, 0);
Alan Cox9cc3c222008-04-30 00:53:26 -0700383 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 case KDMKTONE:
386 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000387 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 {
389 unsigned int ticks, count;
390
391 /*
392 * Generate the tone for the appropriate number of ticks.
393 * If the time is zero, turn off sound ourselves.
394 */
Nicholas Mc Guire4c0d9b12015-02-09 10:53:25 -0500395 ticks = msecs_to_jiffies((arg >> 16) & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 count = ticks ? (arg & 0xffff) : 0;
397 if (count)
Arnd Bergmann2c4e9672010-08-28 20:35:17 -0700398 count = PIT_TICK_RATE / count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 kd_mksound(count, ticks);
Alan Cox9cc3c222008-04-30 00:53:26 -0700400 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
403 case KDGKBTYPE:
404 /*
Alan Cox4001d7b2012-03-02 14:59:20 +0000405 * this is naïve.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 */
407 ucval = KB_101;
Alan Cox079c9532012-02-28 14:49:23 +0000408 ret = put_user(ucval, (char __user *)arg);
409 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /*
412 * These cannot be implemented on any machine that implements
413 * ioperm() in user level (such as Alpha PCs) or not at all.
414 *
415 * XXX: you should never use these, just call ioperm directly..
416 */
417#ifdef CONFIG_X86
418 case KDADDIO:
419 case KDDELIO:
420 /*
421 * KDADDIO and KDDELIO may be able to add ports beyond what
422 * we reject here, but to be safe...
Alan Cox4001d7b2012-03-02 14:59:20 +0000423 *
424 * These are locked internally via sys_ioperm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 */
Alan Cox9cc3c222008-04-30 00:53:26 -0700426 if (arg < GPFIRST || arg > GPLAST) {
427 ret = -EINVAL;
428 break;
429 }
430 ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
431 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 case KDENABIO:
434 case KDDISABIO:
Alan Cox9cc3c222008-04-30 00:53:26 -0700435 ret = sys_ioperm(GPFIRST, GPNUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 (cmd == KDENABIO)) ? -ENXIO : 0;
Alan Cox9cc3c222008-04-30 00:53:26 -0700437 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438#endif
439
440 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
441
442 case KDKBDREP:
443 {
444 struct kbd_repeat kbrep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox4001d7b2012-03-02 14:59:20 +0000447 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Alan Cox9cc3c222008-04-30 00:53:26 -0700449 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
450 ret = -EFAULT;
451 break;
452 }
453 ret = kbd_rate(&kbrep);
454 if (ret)
455 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
Alan Cox9cc3c222008-04-30 00:53:26 -0700457 ret = -EFAULT;
458 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
461 case KDSETMODE:
462 /*
463 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
464 * doesn't do a whole lot. i'm not sure if it should do any
465 * restoration of modes or what...
466 *
467 * XXX It should at least call into the driver, fbdev's definitely
468 * need to restore their engine state. --BenH
469 */
470 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000471 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 switch (arg) {
473 case KD_GRAPHICS:
474 break;
475 case KD_TEXT0:
476 case KD_TEXT1:
477 arg = KD_TEXT;
478 case KD_TEXT:
479 break;
480 default:
Alan Cox9cc3c222008-04-30 00:53:26 -0700481 ret = -EINVAL;
482 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Alan Cox4001d7b2012-03-02 14:59:20 +0000484 /* FIXME: this needs the console lock extending */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (vc->vc_mode == (unsigned char) arg)
Alan Cox9cc3c222008-04-30 00:53:26 -0700486 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 vc->vc_mode = (unsigned char) arg;
488 if (console != fg_console)
Alan Cox9cc3c222008-04-30 00:53:26 -0700489 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /*
491 * explicitly blank/unblank the screen if switching modes
492 */
Torben Hohnac751ef2011-01-25 15:07:35 -0800493 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (arg == KD_TEXT)
495 do_unblank_screen(1);
496 else
497 do_blank_screen(1);
Torben Hohnac751ef2011-01-25 15:07:35 -0800498 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700499 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 case KDGETMODE:
Graham Gower1e0ad282010-10-27 15:33:00 -0700502 uival = vc->vc_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 goto setint;
504
505 case KDMAPDISP:
506 case KDUNMAPDISP:
507 /*
508 * these work like a combination of mmap and KDENABIO.
509 * this could be easily finished.
510 */
Alan Cox9cc3c222008-04-30 00:53:26 -0700511 ret = -EINVAL;
512 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 case KDSKBMODE:
515 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000516 return -EPERM;
Alan Cox079c9532012-02-28 14:49:23 +0000517 ret = vt_do_kdskbmode(console, arg);
518 if (ret == 0)
519 tty_ldisc_flush(tty);
Alan Cox9cc3c222008-04-30 00:53:26 -0700520 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 case KDGKBMODE:
Alan Cox079c9532012-02-28 14:49:23 +0000523 uival = vt_do_kdgkbmode(console);
524 ret = put_user(uival, (int __user *)arg);
525 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 /* this could be folded into KDSKBMODE, but for compatibility
528 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
529 case KDSKBMETA:
Alan Cox079c9532012-02-28 14:49:23 +0000530 ret = vt_do_kdskbmeta(console, arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700531 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 case KDGKBMETA:
Alan Cox079c9532012-02-28 14:49:23 +0000534 /* FIXME: should review whether this is worth locking */
535 uival = vt_do_kdgkbmeta(console);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 setint:
Graham Gower1e0ad282010-10-27 15:33:00 -0700537 ret = put_user(uival, (int __user *)arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700538 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 case KDGETKEYCODE:
541 case KDSETKEYCODE:
542 if(!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -0700543 perm = 0;
Alan Cox079c9532012-02-28 14:49:23 +0000544 ret = vt_do_kbkeycode_ioctl(cmd, up, perm);
Alan Cox9cc3c222008-04-30 00:53:26 -0700545 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 case KDGKBENT:
548 case KDSKBENT:
Alan Cox079c9532012-02-28 14:49:23 +0000549 ret = vt_do_kdsk_ioctl(cmd, up, perm, console);
Alan Cox9cc3c222008-04-30 00:53:26 -0700550 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 case KDGKBSENT:
553 case KDSKBSENT:
Alan Cox079c9532012-02-28 14:49:23 +0000554 ret = vt_do_kdgkb_ioctl(cmd, up, perm);
Alan Cox9cc3c222008-04-30 00:53:26 -0700555 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Alan Cox247ff8e2012-02-24 12:47:11 +0000557 /* Diacritical processing. Handled in keyboard.c as it has
558 to operate on the keyboard locks and structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 case KDGKBDIACR:
Samuel Thibault04c71972007-10-16 23:27:04 -0700560 case KDGKBDIACRUC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 case KDSKBDIACR:
Samuel Thibault04c71972007-10-16 23:27:04 -0700562 case KDSKBDIACRUC:
Alan Cox247ff8e2012-02-24 12:47:11 +0000563 ret = vt_do_diacrit(cmd, up, perm);
Alan Cox9cc3c222008-04-30 00:53:26 -0700564 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* the ioctls below read/set the flags usually shown in the leds */
567 /* don't use them - they will go away without warning */
568 case KDGKBLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 case KDSKBLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 case KDGETLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 case KDSETLED:
Alan Cox079c9532012-02-28 14:49:23 +0000572 ret = vt_do_kdskled(console, cmd, arg, perm);
Alan Cox9cc3c222008-04-30 00:53:26 -0700573 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /*
576 * A process can indicate its willingness to accept signals
577 * generated by pressing an appropriate key combination.
578 * Thus, one can have a daemon that e.g. spawns a new console
579 * upon a keypress and then changes to it.
580 * See also the kbrequest field of inittab(5).
581 */
582 case KDSIGACCEPT:
583 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (!perm || !capable(CAP_KILL))
Alan Cox4001d7b2012-03-02 14:59:20 +0000585 return -EPERM;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700586 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
Alan Cox9cc3c222008-04-30 00:53:26 -0700587 ret = -EINVAL;
588 else {
589 spin_lock_irq(&vt_spawn_con.lock);
590 put_pid(vt_spawn_con.pid);
591 vt_spawn_con.pid = get_pid(task_pid(current));
592 vt_spawn_con.sig = arg;
593 spin_unlock_irq(&vt_spawn_con.lock);
594 }
595 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597
598 case VT_SETMODE:
599 {
600 struct vt_mode tmp;
601
602 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000603 return -EPERM;
Alan Cox9cc3c222008-04-30 00:53:26 -0700604 if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
605 ret = -EFAULT;
606 goto out;
607 }
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -0700608 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
Alan Cox9cc3c222008-04-30 00:53:26 -0700609 ret = -EINVAL;
610 goto out;
611 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800612 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 vc->vt_mode = tmp;
614 /* the frsig is ignored, so we set it to 0 */
615 vc->vt_mode.frsig = 0;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -0800616 put_pid(vc->vt_pid);
617 vc->vt_pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* no switch is required -- saw@shade.msu.ru */
619 vc->vt_newvt = -1;
Torben Hohnac751ef2011-01-25 15:07:35 -0800620 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700621 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
624 case VT_GETMODE:
625 {
626 struct vt_mode tmp;
627 int rc;
628
Torben Hohnac751ef2011-01-25 15:07:35 -0800629 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
Torben Hohnac751ef2011-01-25 15:07:35 -0800631 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
Alan Cox9cc3c222008-04-30 00:53:26 -0700634 if (rc)
635 ret = -EFAULT;
636 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638
639 /*
640 * Returns global vt state. Note that VT 0 is always open, since
641 * it's an alias for the current VT, and people can't use it here.
642 * We cannot return state for more than 16 VTs, since v_state is short.
643 */
644 case VT_GETSTATE:
645 {
646 struct vt_stat __user *vtstat = up;
647 unsigned short state, mask;
648
Alan Cox4001d7b2012-03-02 14:59:20 +0000649 /* Review: FIXME: Console lock ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (put_user(fg_console + 1, &vtstat->v_active))
Alan Cox9cc3c222008-04-30 00:53:26 -0700651 ret = -EFAULT;
652 else {
653 state = 1; /* /dev/tty0 is always open */
654 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
655 ++i, mask <<= 1)
Jiri Slaby8c3bc92d2020-02-19 08:39:44 +0100656 if (vt_in_use(i))
Alan Cox9cc3c222008-04-30 00:53:26 -0700657 state |= mask;
658 ret = put_user(state, &vtstat->v_state);
659 }
660 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
663 /*
664 * Returns the first available (non-opened) console.
665 */
666 case VT_OPENQRY:
Alan Cox4001d7b2012-03-02 14:59:20 +0000667 /* FIXME: locking ? - but then this is a stupid API */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 for (i = 0; i < MAX_NR_CONSOLES; ++i)
Jiri Slaby8c3bc92d2020-02-19 08:39:44 +0100669 if (!vt_in_use(i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 break;
Graham Gower1e0ad282010-10-27 15:33:00 -0700671 uival = i < MAX_NR_CONSOLES ? (i+1) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 goto setint;
673
674 /*
675 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
676 * with num >= 1 (switches to vt 0, our console, are not allowed, just
677 * to preserve sanity).
678 */
679 case VT_ACTIVATE:
680 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000681 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (arg == 0 || arg > MAX_NR_CONSOLES)
Alan Cox9cc3c222008-04-30 00:53:26 -0700683 ret = -ENXIO;
684 else {
685 arg--;
Torben Hohnac751ef2011-01-25 15:07:35 -0800686 console_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700687 ret = vc_allocate(arg);
Torben Hohnac751ef2011-01-25 15:07:35 -0800688 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700689 if (ret)
690 break;
691 set_console(arg);
692 }
693 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Alan Coxd3b5cff2009-09-19 13:13:26 -0700695 case VT_SETACTIVATE:
696 {
697 struct vt_setactivate vsa;
698
699 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000700 return -EPERM;
Alan Coxd3b5cff2009-09-19 13:13:26 -0700701
702 if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
Jiri Slabya09efb02009-10-01 15:43:59 -0700703 sizeof(struct vt_setactivate))) {
704 ret = -EFAULT;
705 goto out;
706 }
Alan Coxd3b5cff2009-09-19 13:13:26 -0700707 if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
708 ret = -ENXIO;
709 else {
Gustavo A. R. Silva0ec459e2018-08-16 15:30:38 -0500710 vsa.console = array_index_nospec(vsa.console,
711 MAX_NR_CONSOLES + 1);
Alan Coxd3b5cff2009-09-19 13:13:26 -0700712 vsa.console--;
Torben Hohnac751ef2011-01-25 15:07:35 -0800713 console_lock();
Alan Coxd3b5cff2009-09-19 13:13:26 -0700714 ret = vc_allocate(vsa.console);
715 if (ret == 0) {
716 struct vc_data *nvc;
717 /* This is safe providing we don't drop the
718 console sem between vc_allocate and
719 finishing referencing nvc */
720 nvc = vc_cons[vsa.console].d;
721 nvc->vt_mode = vsa.mode;
722 nvc->vt_mode.frsig = 0;
723 put_pid(nvc->vt_pid);
724 nvc->vt_pid = get_pid(task_pid(current));
725 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800726 console_unlock();
Alan Coxd3b5cff2009-09-19 13:13:26 -0700727 if (ret)
728 break;
729 /* Commence switch and lock */
Alan Cox4001d7b2012-03-02 14:59:20 +0000730 /* Review set_console locks */
Jiri Olsad6378372011-02-11 15:39:28 +0100731 set_console(vsa.console);
Alan Coxd3b5cff2009-09-19 13:13:26 -0700732 }
Jiri Olsad6378372011-02-11 15:39:28 +0100733 break;
Alan Coxd3b5cff2009-09-19 13:13:26 -0700734 }
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /*
737 * wait until the specified VT has been activated
738 */
739 case VT_WAITACTIVE:
740 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000741 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (arg == 0 || arg > MAX_NR_CONSOLES)
Alan Cox9cc3c222008-04-30 00:53:26 -0700743 ret = -ENXIO;
Alan Cox99cceb4e2012-03-02 14:59:49 +0000744 else
Alan Cox8b92e872009-09-19 13:13:24 -0700745 ret = vt_waitactive(arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700746 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 /*
749 * If a vt is under process control, the kernel will not switch to it
750 * immediately, but postpone the operation until the process calls this
751 * ioctl, allowing the switch to complete.
752 *
753 * According to the X sources this is the behavior:
754 * 0: pending switch-from not OK
755 * 1: pending switch-from OK
756 * 2: completed switch-to OK
757 */
758 case VT_RELDISP:
759 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000760 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Alan Cox4001d7b2012-03-02 14:59:20 +0000762 console_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700763 if (vc->vt_mode.mode != VT_PROCESS) {
Alan Cox4001d7b2012-03-02 14:59:20 +0000764 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700765 ret = -EINVAL;
766 break;
767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /*
769 * Switching-from response
770 */
771 if (vc->vt_newvt >= 0) {
772 if (arg == 0)
773 /*
774 * Switch disallowed, so forget we were trying
775 * to do it.
776 */
777 vc->vt_newvt = -1;
778
779 else {
780 /*
781 * The current vt has been released, so
782 * complete the switch.
783 */
784 int newvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 newvt = vc->vt_newvt;
786 vc->vt_newvt = -1;
Alan Cox9cc3c222008-04-30 00:53:26 -0700787 ret = vc_allocate(newvt);
788 if (ret) {
Torben Hohnac751ef2011-01-25 15:07:35 -0800789 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700790 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792 /*
793 * When we actually do the console switch,
794 * make sure we are atomic with respect to
795 * other console switches..
796 */
797 complete_change_console(vc_cons[newvt].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700799 } else {
800 /*
801 * Switched-to response
802 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /*
804 * If it's just an ACK, ignore it
805 */
Alan Cox9cc3c222008-04-30 00:53:26 -0700806 if (arg != VT_ACKACQ)
807 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800809 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700810 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /*
813 * Disallocate memory associated to VT (but leave VT1)
814 */
815 case VT_DISALLOCATE:
Alan Cox9cc3c222008-04-30 00:53:26 -0700816 if (arg > MAX_NR_CONSOLES) {
817 ret = -ENXIO;
818 break;
819 }
Peter Hurley421b40a2013-05-17 12:41:03 -0400820 if (arg == 0)
821 vt_disallocate_all();
822 else
823 ret = vt_disallocate(--arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700824 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 case VT_RESIZE:
827 {
828 struct vt_sizes __user *vtsizes = up;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700829 struct vc_data *vc;
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 ushort ll,cc;
832 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000833 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (get_user(ll, &vtsizes->v_rows) ||
835 get_user(cc, &vtsizes->v_cols))
Alan Cox9cc3c222008-04-30 00:53:26 -0700836 ret = -EFAULT;
837 else {
Torben Hohnac751ef2011-01-25 15:07:35 -0800838 console_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700839 for (i = 0; i < MAX_NR_CONSOLES; i++) {
840 vc = vc_cons[i].d;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700841
Alan Cox9cc3c222008-04-30 00:53:26 -0700842 if (vc) {
843 vc->vc_resize_user = 1;
Alan Cox4001d7b2012-03-02 14:59:20 +0000844 /* FIXME: review v tty lock */
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100845 vc_resize(vc_cons[i].d, cc, ll);
Alan Cox9cc3c222008-04-30 00:53:26 -0700846 }
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700847 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800848 console_unlock();
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700849 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700850 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
853 case VT_RESIZEX:
854 {
Al Viro97b58fa2017-09-29 12:34:13 -0400855 struct vt_consize v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000857 return -EPERM;
Al Viro97b58fa2017-09-29 12:34:13 -0400858 if (copy_from_user(&v, up, sizeof(struct vt_consize)))
859 return -EFAULT;
Alan Cox9cc3c222008-04-30 00:53:26 -0700860 /* FIXME: Should check the copies properly */
Al Viro97b58fa2017-09-29 12:34:13 -0400861 if (!v.v_vlin)
862 v.v_vlin = vc->vc_scan_lines;
863 if (v.v_clin) {
864 int rows = v.v_vlin/v.v_clin;
865 if (v.v_rows != rows) {
866 if (v.v_rows) /* Parameters don't add up */
867 return -EINVAL;
868 v.v_rows = rows;
869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
Al Viro97b58fa2017-09-29 12:34:13 -0400871 if (v.v_vcol && v.v_ccol) {
872 int cols = v.v_vcol/v.v_ccol;
873 if (v.v_cols != cols) {
874 if (v.v_cols)
875 return -EINVAL;
876 v.v_cols = cols;
877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
Al Viro97b58fa2017-09-29 12:34:13 -0400880 if (v.v_clin > 32)
881 return -EINVAL;
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 for (i = 0; i < MAX_NR_CONSOLES; i++) {
Eric Dumazet160fbca2020-02-10 11:07:21 -0800884 struct vc_data *vcp;
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (!vc_cons[i].d)
887 continue;
Torben Hohnac751ef2011-01-25 15:07:35 -0800888 console_lock();
Eric Dumazet160fbca2020-02-10 11:07:21 -0800889 vcp = vc_cons[i].d;
890 if (vcp) {
891 if (v.v_vlin)
892 vcp->vc_scan_lines = v.v_vlin;
893 if (v.v_clin)
894 vcp->vc_font.height = v.v_clin;
895 vcp->vc_resize_user = 1;
896 vc_resize(vcp, v.v_cols, v.v_rows);
897 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800898 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700900 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902
903 case PIO_FONT: {
904 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000905 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 op.op = KD_FONT_OP_SET;
907 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
908 op.width = 8;
909 op.height = 0;
910 op.charcount = 256;
911 op.data = up;
Alan Cox9cc3c222008-04-30 00:53:26 -0700912 ret = con_font_op(vc_cons[fg_console].d, &op);
913 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915
916 case GIO_FONT: {
917 op.op = KD_FONT_OP_GET;
918 op.flags = KD_FONT_FLAG_OLD;
919 op.width = 8;
920 op.height = 32;
921 op.charcount = 256;
922 op.data = up;
Alan Cox9cc3c222008-04-30 00:53:26 -0700923 ret = con_font_op(vc_cons[fg_console].d, &op);
924 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926
927 case PIO_CMAP:
928 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700929 ret = -EPERM;
930 else
931 ret = con_set_cmap(up);
932 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 case GIO_CMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -0700935 ret = con_get_cmap(up);
936 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 case PIO_FONTX:
939 case GIO_FONTX:
Alan Cox9cc3c222008-04-30 00:53:26 -0700940 ret = do_fontx_ioctl(cmd, up, perm, &op);
941 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 case PIO_FONTRESET:
944 {
945 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000946 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948#ifdef BROKEN_GRAPHICS_PROGRAMS
949 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
950 font is not saved. */
Alan Cox9cc3c222008-04-30 00:53:26 -0700951 ret = -ENOSYS;
952 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953#else
954 {
955 op.op = KD_FONT_OP_SET_DEFAULT;
956 op.data = NULL;
Alan Cox9cc3c222008-04-30 00:53:26 -0700957 ret = con_font_op(vc_cons[fg_console].d, &op);
958 if (ret)
959 break;
Alan Cox5d1a33f2012-04-24 11:06:06 +0100960 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 con_set_default_unimap(vc_cons[fg_console].d);
Alan Cox5d1a33f2012-04-24 11:06:06 +0100962 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700963 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965#endif
966 }
967
968 case KDFONTOP: {
Alan Cox9cc3c222008-04-30 00:53:26 -0700969 if (copy_from_user(&op, up, sizeof(op))) {
970 ret = -EFAULT;
971 break;
972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (!perm && op.op != KD_FONT_OP_GET)
Alan Cox4001d7b2012-03-02 14:59:20 +0000974 return -EPERM;
Alan Cox9cc3c222008-04-30 00:53:26 -0700975 ret = con_font_op(vc, &op);
976 if (ret)
977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (copy_to_user(up, &op, sizeof(op)))
Alan Cox9cc3c222008-04-30 00:53:26 -0700979 ret = -EFAULT;
980 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982
983 case PIO_SCRNMAP:
984 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700985 ret = -EPERM;
Alan Cox5d1a33f2012-04-24 11:06:06 +0100986 else
Alan Cox9cc3c222008-04-30 00:53:26 -0700987 ret = con_set_trans_old(up);
988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 case GIO_SCRNMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -0700991 ret = con_get_trans_old(up);
992 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 case PIO_UNISCRNMAP:
995 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700996 ret = -EPERM;
Alan Cox5d1a33f2012-04-24 11:06:06 +0100997 else
Alan Cox9cc3c222008-04-30 00:53:26 -0700998 ret = con_set_trans_new(up);
999 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 case GIO_UNISCRNMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -07001002 ret = con_get_trans_new(up);
1003 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 case PIO_UNIMAPCLR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +00001007 return -EPERM;
Jiri Slabyf8b0c2e2016-06-23 13:34:36 +02001008 con_clear_unimap(vc);
Alan Cox9cc3c222008-04-30 00:53:26 -07001009 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 case PIO_UNIMAP:
1012 case GIO_UNIMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -07001013 ret = do_unimap_ioctl(cmd, up, perm, vc);
1014 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 case VT_LOCKSWITCH:
1017 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox4001d7b2012-03-02 14:59:20 +00001018 return -EPERM;
Jiri Slaby2e1c84e2020-02-19 08:39:48 +01001019 vt_dont_switch = true;
Alan Cox9cc3c222008-04-30 00:53:26 -07001020 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 case VT_UNLOCKSWITCH:
1022 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox4001d7b2012-03-02 14:59:20 +00001023 return -EPERM;
Jiri Slaby2e1c84e2020-02-19 08:39:48 +01001024 vt_dont_switch = false;
Alan Cox9cc3c222008-04-30 00:53:26 -07001025 break;
Samuel Thibault533475d2006-08-27 01:23:39 -07001026 case VT_GETHIFONTMASK:
Alan Cox9cc3c222008-04-30 00:53:26 -07001027 ret = put_user(vc->vc_hi_font_mask,
1028 (unsigned short __user *)arg);
1029 break;
Alan Cox8b92e872009-09-19 13:13:24 -07001030 case VT_WAITEVENT:
1031 ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
1032 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 default:
Alan Cox9cc3c222008-04-30 00:53:26 -07001034 ret = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001036out:
Alan Cox9cc3c222008-04-30 00:53:26 -07001037 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040void reset_vc(struct vc_data *vc)
1041{
1042 vc->vc_mode = KD_TEXT;
Alan Cox079c9532012-02-28 14:49:23 +00001043 vt_reset_unicode(vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 vc->vt_mode.mode = VT_AUTO;
1045 vc->vt_mode.waitv = 0;
1046 vc->vt_mode.relsig = 0;
1047 vc->vt_mode.acqsig = 0;
1048 vc->vt_mode.frsig = 0;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001049 put_pid(vc->vt_pid);
1050 vc->vt_pid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 vc->vt_newvt = -1;
1052 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1053 reset_palette(vc);
1054}
1055
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001056void vc_SAK(struct work_struct *work)
1057{
1058 struct vc *vc_con =
1059 container_of(work, struct vc, SAK_work);
1060 struct vc_data *vc;
1061 struct tty_struct *tty;
1062
Torben Hohnac751ef2011-01-25 15:07:35 -08001063 console_lock();
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001064 vc = vc_con->d;
1065 if (vc) {
Alan Cox079c9532012-02-28 14:49:23 +00001066 /* FIXME: review tty ref counting */
Alan Cox8ce73262010-06-01 22:52:56 +02001067 tty = vc->port.tty;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001068 /*
1069 * SAK should also work in all raw modes and reset
1070 * them properly.
1071 */
1072 if (tty)
1073 __do_SAK(tty);
1074 reset_vc(vc);
1075 }
Torben Hohnac751ef2011-01-25 15:07:35 -08001076 console_unlock();
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001077}
1078
Arnd Bergmanne9216652009-08-06 15:09:28 +02001079#ifdef CONFIG_COMPAT
1080
1081struct compat_consolefontdesc {
1082 unsigned short charcount; /* characters in font (256 or 512) */
1083 unsigned short charheight; /* scan lines per character (1-32) */
1084 compat_caddr_t chardata; /* font data in expanded form */
1085};
1086
1087static inline int
1088compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd,
1089 int perm, struct console_font_op *op)
1090{
1091 struct compat_consolefontdesc cfdarg;
1092 int i;
1093
1094 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
1095 return -EFAULT;
1096
1097 switch (cmd) {
1098 case PIO_FONTX:
1099 if (!perm)
1100 return -EPERM;
1101 op->op = KD_FONT_OP_SET;
1102 op->flags = KD_FONT_FLAG_OLD;
1103 op->width = 8;
1104 op->height = cfdarg.charheight;
1105 op->charcount = cfdarg.charcount;
1106 op->data = compat_ptr(cfdarg.chardata);
1107 return con_font_op(vc_cons[fg_console].d, op);
1108 case GIO_FONTX:
1109 op->op = KD_FONT_OP_GET;
1110 op->flags = KD_FONT_FLAG_OLD;
1111 op->width = 8;
1112 op->height = cfdarg.charheight;
1113 op->charcount = cfdarg.charcount;
1114 op->data = compat_ptr(cfdarg.chardata);
1115 i = con_font_op(vc_cons[fg_console].d, op);
1116 if (i)
1117 return i;
1118 cfdarg.charheight = op->height;
1119 cfdarg.charcount = op->charcount;
1120 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
1121 return -EFAULT;
1122 return 0;
1123 }
1124 return -EINVAL;
1125}
1126
1127struct compat_console_font_op {
1128 compat_uint_t op; /* operation code KD_FONT_OP_* */
1129 compat_uint_t flags; /* KD_FONT_FLAG_* */
1130 compat_uint_t width, height; /* font size */
1131 compat_uint_t charcount;
1132 compat_caddr_t data; /* font data with height fixed to 32 */
1133};
1134
1135static inline int
1136compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
1137 int perm, struct console_font_op *op, struct vc_data *vc)
1138{
1139 int i;
1140
1141 if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
1142 return -EFAULT;
1143 if (!perm && op->op != KD_FONT_OP_GET)
1144 return -EPERM;
1145 op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
Arnd Bergmanne9216652009-08-06 15:09:28 +02001146 i = con_font_op(vc, op);
1147 if (i)
1148 return i;
1149 ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
1150 if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
1151 return -EFAULT;
1152 return 0;
1153}
1154
1155struct compat_unimapdesc {
1156 unsigned short entry_ct;
1157 compat_caddr_t entries;
1158};
1159
1160static inline int
1161compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
1162 int perm, struct vc_data *vc)
1163{
1164 struct compat_unimapdesc tmp;
1165 struct unipair __user *tmp_entries;
1166
1167 if (copy_from_user(&tmp, user_ud, sizeof tmp))
1168 return -EFAULT;
1169 tmp_entries = compat_ptr(tmp.entries);
1170 if (tmp_entries)
1171 if (!access_ok(VERIFY_WRITE, tmp_entries,
1172 tmp.entry_ct*sizeof(struct unipair)))
1173 return -EFAULT;
1174 switch (cmd) {
1175 case PIO_UNIMAP:
1176 if (!perm)
1177 return -EPERM;
1178 return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
1179 case GIO_UNIMAP:
1180 if (!perm && fg_console != vc->vc_num)
1181 return -EPERM;
1182 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
1183 }
1184 return 0;
1185}
1186
Alan Cox6caa76b2011-02-14 16:27:22 +00001187long vt_compat_ioctl(struct tty_struct *tty,
Arnd Bergmanne9216652009-08-06 15:09:28 +02001188 unsigned int cmd, unsigned long arg)
1189{
1190 struct vc_data *vc = tty->driver_data;
1191 struct console_font_op op; /* used in multiple places here */
Arnd Bergmanne9216652009-08-06 15:09:28 +02001192 void __user *up = (void __user *)arg;
1193 int perm;
1194 int ret = 0;
1195
Arnd Bergmanne9216652009-08-06 15:09:28 +02001196 /*
1197 * To have permissions to do most of the vt ioctls, we either have
1198 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
1199 */
1200 perm = 0;
1201 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1202 perm = 1;
1203
Arnd Bergmanne9216652009-08-06 15:09:28 +02001204 switch (cmd) {
1205 /*
1206 * these need special handlers for incompatible data structures
1207 */
1208 case PIO_FONTX:
1209 case GIO_FONTX:
1210 ret = compat_fontx_ioctl(cmd, up, perm, &op);
1211 break;
1212
1213 case KDFONTOP:
1214 ret = compat_kdfontop_ioctl(up, perm, &op, vc);
1215 break;
1216
1217 case PIO_UNIMAP:
1218 case GIO_UNIMAP:
Andreas Schwab4b1fe772009-09-28 20:10:02 +02001219 ret = compat_unimap_ioctl(cmd, up, perm, vc);
Arnd Bergmanne9216652009-08-06 15:09:28 +02001220 break;
1221
1222 /*
1223 * all these treat 'arg' as an integer
1224 */
1225 case KIOCSOUND:
1226 case KDMKTONE:
1227#ifdef CONFIG_X86
1228 case KDADDIO:
1229 case KDDELIO:
1230#endif
1231 case KDSETMODE:
1232 case KDMAPDISP:
1233 case KDUNMAPDISP:
1234 case KDSKBMODE:
1235 case KDSKBMETA:
1236 case KDSKBLED:
1237 case KDSETLED:
1238 case KDSIGACCEPT:
1239 case VT_ACTIVATE:
1240 case VT_WAITACTIVE:
1241 case VT_RELDISP:
1242 case VT_DISALLOCATE:
1243 case VT_RESIZE:
1244 case VT_RESIZEX:
1245 goto fallback;
1246
1247 /*
1248 * the rest has a compatible data structure behind arg,
1249 * but we have to convert it to a proper 64 bit pointer.
1250 */
1251 default:
1252 arg = (unsigned long)compat_ptr(arg);
1253 goto fallback;
1254 }
Eric Biggers35d64802020-02-24 00:03:26 -08001255
Arnd Bergmanne9216652009-08-06 15:09:28 +02001256 return ret;
1257
1258fallback:
Alan Cox6caa76b2011-02-14 16:27:22 +00001259 return vt_ioctl(tty, cmd, arg);
Arnd Bergmanne9216652009-08-06 15:09:28 +02001260}
1261
1262
1263#endif /* CONFIG_COMPAT */
1264
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266/*
Alan Coxd3b5cff2009-09-19 13:13:26 -07001267 * Performs the back end of a vt switch. Called under the console
1268 * semaphore.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 */
1270static void complete_change_console(struct vc_data *vc)
1271{
1272 unsigned char old_vc_mode;
Alan Cox8b92e872009-09-19 13:13:24 -07001273 int old = fg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 last_console = fg_console;
1276
1277 /*
1278 * If we're switching, we could be going from KD_GRAPHICS to
1279 * KD_TEXT mode or vice versa, which means we need to blank or
1280 * unblank the screen later.
1281 */
1282 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1283 switch_screen(vc);
1284
1285 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001286 * This can't appear below a successful kill_pid(). If it did,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 * then the *blank_screen operation could occur while X, having
1288 * received acqsig, is waking up on another processor. This
1289 * condition can lead to overlapping accesses to the VGA range
1290 * and the framebuffer (causing system lockups).
1291 *
1292 * To account for this we duplicate this code below only if the
1293 * controlling process is gone and we've called reset_vc.
1294 */
1295 if (old_vc_mode != vc->vc_mode) {
1296 if (vc->vc_mode == KD_TEXT)
1297 do_unblank_screen(1);
1298 else
1299 do_blank_screen(1);
1300 }
1301
1302 /*
1303 * If this new console is under process control, send it a signal
1304 * telling it that it has acquired. Also check if it has died and
1305 * clean up (similar to logic employed in change_console())
1306 */
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001307 if (vc->vt_mode.mode == VT_PROCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001309 * Send the signal as privileged - kill_pid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 * tell us if the process has gone or something else
1311 * is awry
1312 */
Eric W. Biedermanbde0d2c92006-10-02 02:17:14 -07001313 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 /*
1315 * The controlling process has died, so we revert back to
1316 * normal operation. In this case, we'll also change back
1317 * to KD_TEXT mode. I'm not sure if this is strictly correct
1318 * but it saves the agony when the X server dies and the screen
1319 * remains blanked due to KD_GRAPHICS! It would be nice to do
1320 * this outside of VT_PROCESS but there is no single process
1321 * to account for and tracking tty count may be undesirable.
1322 */
1323 reset_vc(vc);
1324
1325 if (old_vc_mode != vc->vc_mode) {
1326 if (vc->vc_mode == KD_TEXT)
1327 do_unblank_screen(1);
1328 else
1329 do_blank_screen(1);
1330 }
1331 }
1332 }
1333
1334 /*
1335 * Wake anyone waiting for their VT to activate
1336 */
Alan Cox8b92e872009-09-19 13:13:24 -07001337 vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return;
1339}
1340
1341/*
1342 * Performs the front-end of a vt switch
1343 */
1344void change_console(struct vc_data *new_vc)
1345{
1346 struct vc_data *vc;
1347
1348 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1349 return;
1350
1351 /*
1352 * If this vt is in process mode, then we need to handshake with
1353 * that process before switching. Essentially, we store where that
1354 * vt wants to switch to and wait for it to tell us when it's done
1355 * (via VT_RELDISP ioctl).
1356 *
1357 * We also check to see if the controlling process still exists.
1358 * If it doesn't, we reset this vt to auto mode and continue.
1359 * This is a cheap way to track process control. The worst thing
1360 * that can happen is: we send a signal to a process, it dies, and
1361 * the switch gets "lost" waiting for a response; hopefully, the
1362 * user will try again, we'll detect the process is gone (unless
1363 * the user waits just the right amount of time :-) and revert the
1364 * vt to auto control.
1365 */
1366 vc = vc_cons[fg_console].d;
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001367 if (vc->vt_mode.mode == VT_PROCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001369 * Send the signal as privileged - kill_pid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 * tell us if the process has gone or something else
Jan Lübbea64314e2007-09-29 18:47:51 +02001371 * is awry.
1372 *
1373 * We need to set vt_newvt *before* sending the signal or we
1374 * have a race.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 */
Jan Lübbea64314e2007-09-29 18:47:51 +02001376 vc->vt_newvt = new_vc->vc_num;
Eric W. Biedermanbde0d2c92006-10-02 02:17:14 -07001377 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /*
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001379 * It worked. Mark the vt to switch to and
1380 * return. The process needs to send us a
1381 * VT_RELDISP ioctl to complete the switch.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 */
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001383 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385
1386 /*
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001387 * The controlling process has died, so we revert back to
1388 * normal operation. In this case, we'll also change back
1389 * to KD_TEXT mode. I'm not sure if this is strictly correct
1390 * but it saves the agony when the X server dies and the screen
1391 * remains blanked due to KD_GRAPHICS! It would be nice to do
1392 * this outside of VT_PROCESS but there is no single process
1393 * to account for and tracking tty count may be undesirable.
1394 */
1395 reset_vc(vc);
1396
1397 /*
1398 * Fall through to normal (VT_AUTO) handling of the switch...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 */
1400 }
1401
1402 /*
1403 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1404 */
1405 if (vc->vc_mode == KD_GRAPHICS)
1406 return;
1407
1408 complete_change_console(new_vc);
1409}
Alan Cox8d233552009-09-19 13:13:25 -07001410
1411/* Perform a kernel triggered VT switch for suspend/resume */
1412
1413static int disable_vt_switch;
1414
1415int vt_move_to_console(unsigned int vt, int alloc)
1416{
1417 int prev;
1418
Torben Hohnac751ef2011-01-25 15:07:35 -08001419 console_lock();
Alan Cox8d233552009-09-19 13:13:25 -07001420 /* Graphics mode - up to X */
1421 if (disable_vt_switch) {
Torben Hohnac751ef2011-01-25 15:07:35 -08001422 console_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001423 return 0;
1424 }
1425 prev = fg_console;
1426
1427 if (alloc && vc_allocate(vt)) {
1428 /* we can't have a free VC for now. Too bad,
1429 * we don't want to mess the screen for now. */
Torben Hohnac751ef2011-01-25 15:07:35 -08001430 console_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001431 return -ENOSPC;
1432 }
1433
1434 if (set_console(vt)) {
1435 /*
1436 * We're unable to switch to the SUSPEND_CONSOLE.
1437 * Let the calling function know so it can decide
1438 * what to do.
1439 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001440 console_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001441 return -EIO;
1442 }
Torben Hohnac751ef2011-01-25 15:07:35 -08001443 console_unlock();
Jiri Slaby797938b2009-08-11 23:20:41 +02001444 if (vt_waitactive(vt + 1)) {
Alan Cox8d233552009-09-19 13:13:25 -07001445 pr_debug("Suspend: Can't switch VCs.");
1446 return -EINTR;
1447 }
1448 return prev;
1449}
1450
1451/*
1452 * Normally during a suspend, we allocate a new console and switch to it.
1453 * When we resume, we switch back to the original console. This switch
1454 * can be slow, so on systems where the framebuffer can handle restoration
1455 * of video registers anyways, there's little point in doing the console
1456 * switch. This function allows you to disable it by passing it '0'.
1457 */
1458void pm_set_vt_switch(int do_switch)
1459{
Torben Hohnac751ef2011-01-25 15:07:35 -08001460 console_lock();
Alan Cox8d233552009-09-19 13:13:25 -07001461 disable_vt_switch = !do_switch;
Torben Hohnac751ef2011-01-25 15:07:35 -08001462 console_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001463}
1464EXPORT_SYMBOL(pm_set_vt_switch);