1 // SPDX-License-Identifier: GPL-2.0
3 * Provide access to virtual console memory.
4 * /dev/vcs: the screen as it is being viewed right now (possibly scrolled)
5 * /dev/vcsN: the screen of /dev/ttyN (1 <= N <= 63)
8 * /dev/vcsaN: idem, but including attributes, and prefixed with
9 * the 4 bytes lines,columns,x,y (as screendump used to give).
10 * Attribute/character pair is in native endianity.
13 * /dev/vcsuN: similar to /dev/vcsaN but using 4-byte unicode values
14 * instead of 1-byte screen glyph values.
17 * /dev/vcsuaN: same idea as /dev/vcsaN for unicode (not yet implemented).
19 * This replaces screendump and part of selection, so that the system
20 * administrator can control access using file system permissions.
22 * aeb@cwi.nl - efter Friedas begravelse - 950211
24 * machek@k332.feld.cvut.cz - modified not to send characters to wrong console
25 * - fixed some fatal off-by-one bugs (0-- no longer == -1 -> looping and looping and looping...)
26 * - making it shorter - scr_readw are macros which expand in PRETTY long code
29 #include <linux/kernel.h>
30 #include <linux/major.h>
31 #include <linux/errno.h>
32 #include <linux/export.h>
33 #include <linux/tty.h>
34 #include <linux/interrupt.h>
36 #include <linux/init.h>
37 #include <linux/vt_kern.h>
38 #include <linux/selection.h>
39 #include <linux/kbd_kern.h>
40 #include <linux/console.h>
41 #include <linux/device.h>
42 #include <linux/sched.h>
44 #include <linux/poll.h>
45 #include <linux/signal.h>
46 #include <linux/slab.h>
47 #include <linux/notifier.h>
49 #include <linux/uaccess.h>
50 #include <asm/byteorder.h>
51 #include <asm/unaligned.h>
58 #define CON_BUF_SIZE (CONFIG_BASE_SMALL ? 256 : PAGE_SIZE)
63 * 0 ... 63 glyph mode without attributes
64 * 64 ... 127 unicode mode without attributes
65 * 128 ... 191 glyph mode with attributes
66 * 192 ... 255 unused (reserved for unicode with attributes)
68 * This relies on MAX_NR_CONSOLES being <= 63, meaning 63 actual consoles
69 * with minors 0, 64, 128 and 192 being proxies for the foreground console.
71 #if MAX_NR_CONSOLES > 63
72 #warning "/dev/vcs* devices may not accommodate more than 63 consoles"
75 #define console(inode) (iminor(inode) & 63)
76 #define use_unicode(inode) (iminor(inode) & 64)
77 #define use_attributes(inode) (iminor(inode) & 128)
80 struct vcs_poll_data {
81 struct notifier_block notifier;
82 unsigned int cons_num;
84 wait_queue_head_t waitq;
85 struct fasync_struct *fasync;
89 vcs_notifier(struct notifier_block *nb, unsigned long code, void *_param)
91 struct vt_notifier_param *param = _param;
92 struct vc_data *vc = param->vc;
93 struct vcs_poll_data *poll =
94 container_of(nb, struct vcs_poll_data, notifier);
95 int currcons = poll->cons_num;
110 currcons = fg_console;
113 if (currcons != vc->vc_num)
117 wake_up_interruptible(&poll->waitq);
118 kill_fasync(&poll->fasync, SIGIO, fa_band);
123 vcs_poll_data_free(struct vcs_poll_data *poll)
125 unregister_vt_notifier(&poll->notifier);
129 static struct vcs_poll_data *
130 vcs_poll_data_get(struct file *file)
132 struct vcs_poll_data *poll = file->private_data, *kill = NULL;
137 poll = kzalloc(sizeof(*poll), GFP_KERNEL);
140 poll->cons_num = console(file_inode(file));
141 init_waitqueue_head(&poll->waitq);
142 poll->notifier.notifier_call = vcs_notifier;
144 * In order not to lose any update event, we must pretend one might
145 * have occurred before we have a chance to register our notifier.
146 * This is also how user space has come to detect which kernels
147 * support POLLPRI on /dev/vcs* devices i.e. using poll() with
148 * POLLPRI and a zero timeout.
150 poll->event = VT_UPDATE;
152 if (register_vt_notifier(&poll->notifier) != 0) {
158 * This code may be called either through ->poll() or ->fasync().
159 * If we have two threads using the same file descriptor, they could
160 * both enter this function, both notice that the structure hasn't
161 * been allocated yet and go ahead allocating it in parallel, but
162 * only one of them must survive and be shared otherwise we'd leak
163 * memory with a dangling notifier callback.
165 spin_lock(&file->f_lock);
166 if (!file->private_data) {
167 file->private_data = poll;
169 /* someone else raced ahead of us */
171 poll = file->private_data;
173 spin_unlock(&file->f_lock);
175 vcs_poll_data_free(kill);
181 * vcs_vc -- return VC for @inode
182 * @inode: inode for which to return a VC
183 * @viewed: returns whether this console is currently foreground (viewed)
185 * Must be called with console_lock.
187 static struct vc_data *vcs_vc(struct inode *inode, bool *viewed)
189 unsigned int currcons = console(inode);
191 WARN_CONSOLE_UNLOCKED();
194 currcons = fg_console;
202 return vc_cons[currcons].d;
206 * vcs_size -- return size for a VC in @vc
208 * @attr: does it use attributes?
209 * @unicode: is it unicode?
211 * Must be called with console_lock.
213 static int vcs_size(const struct vc_data *vc, bool attr, bool unicode)
217 WARN_CONSOLE_UNLOCKED();
219 size = vc->vc_rows * vc->vc_cols;
225 size = 2 * size + HEADER_SIZE;
232 static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
234 struct inode *inode = file_inode(file);
239 vc = vcs_vc(inode, NULL);
245 size = vcs_size(vc, use_attributes(inode), use_unicode(inode));
249 return fixed_size_llseek(file, offset, orig, size);
254 vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
256 struct inode *inode = file_inode(file);
258 struct vcs_poll_data *poll;
260 int attr, uni_mode, row, col, maxcol;
261 unsigned short *org = NULL;
266 con_buf = (char *) __get_free_page(GFP_KERNEL);
272 /* Select the proper current console and verify
273 * sanity of the situation under the console lock.
277 uni_mode = use_unicode(inode);
278 attr = use_attributes(inode);
280 vc = vcs_vc(inode, &viewed);
287 /* we enforce 32-bit alignment for pos and count in unicode mode */
288 if (uni_mode && (pos | count) & 3)
291 poll = file->private_data;
297 char *con_buf0, *con_buf_start;
298 long this_round, size;
302 /* Check whether we are above size each round,
303 * as copy_to_user at the end of this loop
306 size = vcs_size(vc, attr, uni_mode);
315 if (count > size - pos)
319 if (this_round > CON_BUF_SIZE)
320 this_round = CON_BUF_SIZE;
322 /* Perform the whole read into the local con_buf.
323 * Then we can drop the console spinlock and safely
324 * attempt to move it to userspace.
327 con_buf_start = con_buf0 = con_buf;
328 orig_count = this_round;
329 maxcol = vc->vc_cols;
333 ret = vc_uniscr_check(vc);
337 row = p / vc->vc_cols;
341 if (nr > this_round/4)
343 vc_uniscr_copy_line(vc, con_buf0, viewed,
346 this_round -= nr * 4;
350 } while (this_round);
352 org = screen_pos(vc, p, viewed);
355 while (this_round-- > 0) {
356 *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff);
357 if (++col == maxcol) {
358 org = screen_pos(vc, p, viewed);
364 if (p < HEADER_SIZE) {
367 /* clamp header values if they don't fit */
368 con_buf0[0] = min(vc->vc_rows, 0xFFu);
369 con_buf0[1] = min(vc->vc_cols, 0xFFu);
370 getconsxy(vc, con_buf0 + 2);
374 if (this_round > CON_BUF_SIZE) {
375 this_round = CON_BUF_SIZE;
376 orig_count = this_round - p;
379 tmp_count = HEADER_SIZE;
380 if (tmp_count > this_round)
381 tmp_count = this_round;
383 /* Advance state pointers and move on. */
384 this_round -= tmp_count;
386 con_buf0 = con_buf + HEADER_SIZE;
387 /* If this_round >= 0, then p is even... */
389 /* Skip first byte for output if start address is odd
390 * Update region sizes up/down depending on free
394 if (this_round < CON_BUF_SIZE)
399 if (this_round > 0) {
400 unsigned short *tmp_buf = (unsigned short *)con_buf0;
406 org = screen_pos(vc, p, viewed);
409 /* Buffer has even length, so we can always copy
410 * character + attribute. We do not copy last byte
411 * to userspace if this_round is odd.
413 this_round = (this_round + 1) >> 1;
416 *tmp_buf++ = vcs_scr_readw(vc, org++);
418 if (++col == maxcol) {
419 org = screen_pos(vc, p, viewed);
427 /* Finally, release the console semaphore while we push
428 * all the data to userspace from our temporary buffer.
430 * AKPM: Even though it's a semaphore, we should drop it because
431 * the pagefault handling code may want to call printk().
435 ret = copy_to_user(buf, con_buf_start, orig_count);
439 read += (orig_count - ret);
453 free_page((unsigned long) con_buf);
457 static u16 *vcs_write_buf_noattr(struct vc_data *vc, const char *con_buf,
458 unsigned int pos, unsigned int count, bool viewed, u16 **org0)
461 unsigned int col, maxcol = vc->vc_cols;
463 *org0 = org = screen_pos(vc, pos, viewed);
468 unsigned char c = *con_buf++;
472 (vcs_scr_readw(vc, org) & 0xff00) | c, org);
474 if (++col == maxcol) {
475 org = screen_pos(vc, pos, viewed);
484 static u16 *vcs_write_buf(struct vc_data *vc, const char *con_buf,
485 unsigned int pos, unsigned int count, bool viewed, u16 **org0)
488 unsigned int col, maxcol = vc->vc_cols;
492 if (pos < HEADER_SIZE) {
493 char header[HEADER_SIZE];
495 getconsxy(vc, header + 2);
496 while (pos < HEADER_SIZE && count > 0) {
498 header[pos++] = *con_buf++;
501 putconsxy(vc, header + 2);
508 col = (pos/2) % maxcol;
510 *org0 = org = screen_pos(vc, pos/2, viewed);
512 /* odd pos -- the first single character */
517 vcs_scr_writew(vc, c |
518 (vcs_scr_readw(vc, org) & 0xff00), org);
520 vcs_scr_writew(vc, (c << 8) |
521 (vcs_scr_readw(vc, org) & 0xff), org);
525 if (++col == maxcol) {
526 org = screen_pos(vc, pos/2, viewed);
534 /* even pos -- handle attr+character pairs */
538 w = get_unaligned(((unsigned short *)con_buf));
539 vcs_scr_writew(vc, w, org++);
542 if (++col == maxcol) {
543 org = screen_pos(vc, pos, viewed);
552 /* odd pos -- the remaining character */
555 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org);
557 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org);
564 vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
566 struct inode *inode = file_inode(file);
570 unsigned int written;
576 if (use_unicode(inode))
579 con_buf = (char *) __get_free_page(GFP_KERNEL);
585 /* Select the proper current console and verify
586 * sanity of the situation under the console lock.
590 attr = use_attributes(inode);
592 vc = vcs_vc(inode, &viewed);
596 size = vcs_size(vc, attr, false);
602 if (pos < 0 || pos > size)
604 if (count > size - pos)
608 unsigned int this_round = count;
610 if (this_round > CON_BUF_SIZE)
611 this_round = CON_BUF_SIZE;
613 /* Temporarily drop the console lock so that we can read
614 * in the write data from userspace safely.
617 ret = copy_from_user(con_buf, buf, this_round);
623 /* Abort loop if no data were copied. Otherwise
633 /* The vcs_size might have changed while we slept to grab
634 * the user buffer, so recheck.
635 * Return data written up to now on failure.
637 size = vcs_size(vc, attr, false);
646 if (this_round > size - pos)
647 this_round = size - pos;
649 /* OK, now actually push the write to the console
650 * under the lock using the local kernel buffer.
654 org = vcs_write_buf(vc, con_buf, pos, this_round,
657 org = vcs_write_buf_noattr(vc, con_buf, pos, this_round,
661 written += this_round;
665 update_region(vc, (unsigned long)(org0), org - org0);
674 free_page((unsigned long) con_buf);
679 vcs_poll(struct file *file, poll_table *wait)
681 struct vcs_poll_data *poll = vcs_poll_data_get(file);
682 __poll_t ret = DEFAULT_POLLMASK|EPOLLERR;
685 poll_wait(file, &poll->waitq, wait);
686 switch (poll->event) {
688 ret = DEFAULT_POLLMASK|EPOLLPRI;
691 ret = DEFAULT_POLLMASK|EPOLLHUP|EPOLLERR;
694 ret = DEFAULT_POLLMASK;
702 vcs_fasync(int fd, struct file *file, int on)
704 struct vcs_poll_data *poll = file->private_data;
707 /* don't allocate anything if all we want is disable fasync */
710 poll = vcs_poll_data_get(file);
715 return fasync_helper(fd, file, on, &poll->fasync);
719 vcs_open(struct inode *inode, struct file *filp)
721 unsigned int currcons = console(inode);
722 bool attr = use_attributes(inode);
723 bool uni_mode = use_unicode(inode);
726 /* we currently don't support attributes in unicode mode */
727 if (attr && uni_mode)
731 if(currcons && !vc_cons_allocated(currcons-1))
737 static int vcs_release(struct inode *inode, struct file *file)
739 struct vcs_poll_data *poll = file->private_data;
742 vcs_poll_data_free(poll);
746 static const struct file_operations vcs_fops = {
751 .fasync = vcs_fasync,
753 .release = vcs_release,
756 static struct class *vc_class;
758 void vcs_make_sysfs(int index)
760 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL,
762 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 65), NULL,
763 "vcsu%u", index + 1);
764 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL,
765 "vcsa%u", index + 1);
768 void vcs_remove_sysfs(int index)
770 device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 1));
771 device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 65));
772 device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 129));
775 int __init vcs_init(void)
779 if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
780 panic("unable to get major %d for vcs device", VCS_MAJOR);
781 vc_class = class_create(THIS_MODULE, "vc");
783 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
784 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 64), NULL, "vcsu");
785 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");
786 for (i = 0; i < MIN_NR_CONSOLES; i++)