vs_screen: kill tmp_count from vcs_read
[linux-2.6-microblaze.git] / drivers / tty / vt / vc_screen.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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)
6  *            [minor: N]
7  *
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.
11  *            [minor: N+128]
12  *
13  * /dev/vcsuN: similar to /dev/vcsaN but using 4-byte unicode values
14  *      instead of 1-byte screen glyph values.
15  *            [minor: N+64]
16  *
17  * /dev/vcsuaN: same idea as /dev/vcsaN for unicode (not yet implemented).
18  *
19  * This replaces screendump and part of selection, so that the system
20  * administrator can control access using file system permissions.
21  *
22  * aeb@cwi.nl - efter Friedas begravelse - 950211
23  *
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
27  */
28
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>
35 #include <linux/mm.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>
43 #include <linux/fs.h>
44 #include <linux/poll.h>
45 #include <linux/signal.h>
46 #include <linux/slab.h>
47 #include <linux/notifier.h>
48
49 #include <linux/uaccess.h>
50 #include <asm/byteorder.h>
51 #include <asm/unaligned.h>
52
53 #undef attr
54 #undef org
55 #undef addr
56 #define HEADER_SIZE     4u
57
58 #define CON_BUF_SIZE (CONFIG_BASE_SMALL ? 256 : PAGE_SIZE)
59
60 /*
61  * Our minor space:
62  *
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)
67  *
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.
70  */
71 #if MAX_NR_CONSOLES > 63
72 #warning "/dev/vcs* devices may not accommodate more than 63 consoles"
73 #endif
74
75 #define console(inode)          (iminor(inode) & 63)
76 #define use_unicode(inode)      (iminor(inode) & 64)
77 #define use_attributes(inode)   (iminor(inode) & 128)
78
79
80 struct vcs_poll_data {
81         struct notifier_block notifier;
82         unsigned int cons_num;
83         int event;
84         wait_queue_head_t waitq;
85         struct fasync_struct *fasync;
86 };
87
88 static int
89 vcs_notifier(struct notifier_block *nb, unsigned long code, void *_param)
90 {
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;
96         int fa_band;
97
98         switch (code) {
99         case VT_UPDATE:
100                 fa_band = POLL_PRI;
101                 break;
102         case VT_DEALLOCATE:
103                 fa_band = POLL_HUP;
104                 break;
105         default:
106                 return NOTIFY_DONE;
107         }
108
109         if (currcons == 0)
110                 currcons = fg_console;
111         else
112                 currcons--;
113         if (currcons != vc->vc_num)
114                 return NOTIFY_DONE;
115
116         poll->event = code;
117         wake_up_interruptible(&poll->waitq);
118         kill_fasync(&poll->fasync, SIGIO, fa_band);
119         return NOTIFY_OK;
120 }
121
122 static void
123 vcs_poll_data_free(struct vcs_poll_data *poll)
124 {
125         unregister_vt_notifier(&poll->notifier);
126         kfree(poll);
127 }
128
129 static struct vcs_poll_data *
130 vcs_poll_data_get(struct file *file)
131 {
132         struct vcs_poll_data *poll = file->private_data, *kill = NULL;
133
134         if (poll)
135                 return poll;
136
137         poll = kzalloc(sizeof(*poll), GFP_KERNEL);
138         if (!poll)
139                 return NULL;
140         poll->cons_num = console(file_inode(file));
141         init_waitqueue_head(&poll->waitq);
142         poll->notifier.notifier_call = vcs_notifier;
143         /*
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.
149          */
150         poll->event = VT_UPDATE;
151
152         if (register_vt_notifier(&poll->notifier) != 0) {
153                 kfree(poll);
154                 return NULL;
155         }
156
157         /*
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.
164          */
165         spin_lock(&file->f_lock);
166         if (!file->private_data) {
167                 file->private_data = poll;
168         } else {
169                 /* someone else raced ahead of us */
170                 kill = poll;
171                 poll = file->private_data;
172         }
173         spin_unlock(&file->f_lock);
174         if (kill)
175                 vcs_poll_data_free(kill);
176
177         return poll;
178 }
179
180 /**
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)
184  *
185  * Must be called with console_lock.
186  */
187 static struct vc_data *vcs_vc(struct inode *inode, bool *viewed)
188 {
189         unsigned int currcons = console(inode);
190
191         WARN_CONSOLE_UNLOCKED();
192
193         if (currcons == 0) {
194                 currcons = fg_console;
195                 if (viewed)
196                         *viewed = true;
197         } else {
198                 currcons--;
199                 if (viewed)
200                         *viewed = false;
201         }
202         return vc_cons[currcons].d;
203 }
204
205 /**
206  * vcs_size -- return size for a VC in @vc
207  * @vc: which VC
208  * @attr: does it use attributes?
209  * @unicode: is it unicode?
210  *
211  * Must be called with console_lock.
212  */
213 static int vcs_size(const struct vc_data *vc, bool attr, bool unicode)
214 {
215         int size;
216
217         WARN_CONSOLE_UNLOCKED();
218
219         size = vc->vc_rows * vc->vc_cols;
220
221         if (attr) {
222                 if (unicode)
223                         return -EOPNOTSUPP;
224
225                 size = 2 * size + HEADER_SIZE;
226         } else if (unicode)
227                 size *= 4;
228
229         return size;
230 }
231
232 static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
233 {
234         struct inode *inode = file_inode(file);
235         struct vc_data *vc;
236         int size;
237
238         console_lock();
239         vc = vcs_vc(inode, NULL);
240         if (!vc) {
241                 console_unlock();
242                 return -ENXIO;
243         }
244
245         size = vcs_size(vc, use_attributes(inode), use_unicode(inode));
246         console_unlock();
247         if (size < 0)
248                 return size;
249         return fixed_size_llseek(file, offset, orig, size);
250 }
251
252
253 static ssize_t
254 vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
255 {
256         struct inode *inode = file_inode(file);
257         struct vc_data *vc;
258         struct vcs_poll_data *poll;
259         u16 *org;
260         unsigned int read, row, col, maxcol;
261         ssize_t ret;
262         char *con_buf;
263         loff_t pos;
264         bool viewed, attr, uni_mode;
265
266         con_buf = (char *) __get_free_page(GFP_KERNEL);
267         if (!con_buf)
268                 return -ENOMEM;
269
270         pos = *ppos;
271
272         /* Select the proper current console and verify
273          * sanity of the situation under the console lock.
274          */
275         console_lock();
276
277         uni_mode = use_unicode(inode);
278         attr = use_attributes(inode);
279         ret = -ENXIO;
280         vc = vcs_vc(inode, &viewed);
281         if (!vc)
282                 goto unlock_out;
283
284         ret = -EINVAL;
285         if (pos < 0)
286                 goto unlock_out;
287         /* we enforce 32-bit alignment for pos and count in unicode mode */
288         if (uni_mode && (pos | count) & 3)
289                 goto unlock_out;
290
291         poll = file->private_data;
292         if (count && poll)
293                 poll->event = 0;
294         read = 0;
295         ret = 0;
296         while (count) {
297                 char *con_buf0, *con_buf_start;
298                 unsigned int this_round, orig_count, p = pos;
299                 int size;
300
301                 /* Check whether we are above size each round,
302                  * as copy_to_user at the end of this loop
303                  * could sleep.
304                  */
305                 size = vcs_size(vc, attr, uni_mode);
306                 if (size < 0) {
307                         if (read)
308                                 break;
309                         ret = size;
310                         goto unlock_out;
311                 }
312                 if (pos >= size)
313                         break;
314                 if (count > size - pos)
315                         count = size - pos;
316
317                 this_round = count;
318                 if (this_round > CON_BUF_SIZE)
319                         this_round = CON_BUF_SIZE;
320
321                 /* Perform the whole read into the local con_buf.
322                  * Then we can drop the console spinlock and safely
323                  * attempt to move it to userspace.
324                  */
325
326                 con_buf_start = con_buf0 = con_buf;
327                 orig_count = this_round;
328                 maxcol = vc->vc_cols;
329                 if (uni_mode) {
330                         unsigned int nr;
331
332                         ret = vc_uniscr_check(vc);
333                         if (ret)
334                                 break;
335                         p /= 4;
336                         row = p / vc->vc_cols;
337                         col = p % maxcol;
338                         nr = maxcol - col;
339                         do {
340                                 if (nr > this_round/4)
341                                         nr = this_round/4;
342                                 vc_uniscr_copy_line(vc, con_buf0, viewed,
343                                                     row, col, nr);
344                                 con_buf0 += nr * 4;
345                                 this_round -= nr * 4;
346                                 row++;
347                                 col = 0;
348                                 nr = maxcol;
349                         } while (this_round);
350                 } else if (!attr) {
351                         org = screen_pos(vc, p, viewed);
352                         col = p % maxcol;
353                         p += maxcol - col;
354                         while (this_round-- > 0) {
355                                 *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff);
356                                 if (++col == maxcol) {
357                                         org = screen_pos(vc, p, viewed);
358                                         col = 0;
359                                         p += maxcol;
360                                 }
361                         }
362                 } else {
363                         if (p < HEADER_SIZE) {
364                                 /* clamp header values if they don't fit */
365                                 con_buf0[0] = min(vc->vc_rows, 0xFFu);
366                                 con_buf0[1] = min(vc->vc_cols, 0xFFu);
367                                 getconsxy(vc, con_buf0 + 2);
368
369                                 con_buf_start += p;
370                                 this_round += p;
371                                 if (this_round > CON_BUF_SIZE) {
372                                         this_round = CON_BUF_SIZE;
373                                         orig_count = this_round - p;
374                                 }
375
376                                 /* Advance state pointers and move on. */
377                                 this_round -= min(HEADER_SIZE, this_round);
378                                 p = HEADER_SIZE;
379                                 con_buf0 = con_buf + HEADER_SIZE;
380                                 /* If this_round >= 0, then p is even... */
381                         } else if (p & 1) {
382                                 /* Skip first byte for output if start address is odd
383                                  * Update region sizes up/down depending on free
384                                  * space in buffer.
385                                  */
386                                 con_buf_start++;
387                                 if (this_round < CON_BUF_SIZE)
388                                         this_round++;
389                                 else
390                                         orig_count--;
391                         }
392                         if (this_round > 0) {
393                                 unsigned short *tmp_buf = (unsigned short *)con_buf0;
394
395                                 p -= HEADER_SIZE;
396                                 p /= 2;
397                                 col = p % maxcol;
398
399                                 org = screen_pos(vc, p, viewed);
400                                 p += maxcol - col;
401
402                                 /* Buffer has even length, so we can always copy
403                                  * character + attribute. We do not copy last byte
404                                  * to userspace if this_round is odd.
405                                  */
406                                 this_round = (this_round + 1) >> 1;
407
408                                 while (this_round) {
409                                         *tmp_buf++ = vcs_scr_readw(vc, org++);
410                                         this_round --;
411                                         if (++col == maxcol) {
412                                                 org = screen_pos(vc, p, viewed);
413                                                 col = 0;
414                                                 p += maxcol;
415                                         }
416                                 }
417                         }
418                 }
419
420                 /* Finally, release the console semaphore while we push
421                  * all the data to userspace from our temporary buffer.
422                  *
423                  * AKPM: Even though it's a semaphore, we should drop it because
424                  * the pagefault handling code may want to call printk().
425                  */
426
427                 console_unlock();
428                 ret = copy_to_user(buf, con_buf_start, orig_count);
429                 console_lock();
430
431                 if (ret) {
432                         read += (orig_count - ret);
433                         ret = -EFAULT;
434                         break;
435                 }
436                 buf += orig_count;
437                 pos += orig_count;
438                 read += orig_count;
439                 count -= orig_count;
440         }
441         *ppos += read;
442         if (read)
443                 ret = read;
444 unlock_out:
445         console_unlock();
446         free_page((unsigned long) con_buf);
447         return ret;
448 }
449
450 static u16 *vcs_write_buf_noattr(struct vc_data *vc, const char *con_buf,
451                 unsigned int pos, unsigned int count, bool viewed, u16 **org0)
452 {
453         u16 *org;
454         unsigned int col, maxcol = vc->vc_cols;
455
456         *org0 = org = screen_pos(vc, pos, viewed);
457         col = pos % maxcol;
458         pos += maxcol - col;
459
460         while (count > 0) {
461                 unsigned char c = *con_buf++;
462
463                 count--;
464                 vcs_scr_writew(vc,
465                                (vcs_scr_readw(vc, org) & 0xff00) | c, org);
466                 org++;
467                 if (++col == maxcol) {
468                         org = screen_pos(vc, pos, viewed);
469                         col = 0;
470                         pos += maxcol;
471                 }
472         }
473
474         return org;
475 }
476
477 /*
478  * Compilers (gcc 10) are unable to optimize the swap in cpu_to_le16. So do it
479  * the poor man way.
480  */
481 static inline u16 vc_compile_le16(u8 hi, u8 lo)
482 {
483 #ifdef __BIG_ENDIAN
484         return (lo << 8u) | hi;
485 #else
486         return (hi << 8u) | lo;
487 #endif
488 }
489
490 static u16 *vcs_write_buf(struct vc_data *vc, const char *con_buf,
491                 unsigned int pos, unsigned int count, bool viewed, u16 **org0)
492 {
493         u16 *org;
494         unsigned int col, maxcol = vc->vc_cols;
495         unsigned char c;
496
497         /* header */
498         if (pos < HEADER_SIZE) {
499                 char header[HEADER_SIZE];
500
501                 getconsxy(vc, header + 2);
502                 while (pos < HEADER_SIZE && count > 0) {
503                         count--;
504                         header[pos++] = *con_buf++;
505                 }
506                 if (!viewed)
507                         putconsxy(vc, header + 2);
508         }
509
510         if (!count)
511                 return NULL;
512
513         pos -= HEADER_SIZE;
514         col = (pos/2) % maxcol;
515
516         *org0 = org = screen_pos(vc, pos/2, viewed);
517
518         /* odd pos -- the first single character */
519         if (pos & 1) {
520                 count--;
521                 c = *con_buf++;
522                 vcs_scr_writew(vc, vc_compile_le16(c, vcs_scr_readw(vc, org)),
523                                 org);
524                 org++;
525                 pos++;
526                 if (++col == maxcol) {
527                         org = screen_pos(vc, pos/2, viewed);
528                         col = 0;
529                 }
530         }
531
532         pos /= 2;
533         pos += maxcol - col;
534
535         /* even pos -- handle attr+character pairs */
536         while (count > 1) {
537                 unsigned short w;
538
539                 w = get_unaligned(((unsigned short *)con_buf));
540                 vcs_scr_writew(vc, w, org++);
541                 con_buf += 2;
542                 count -= 2;
543                 if (++col == maxcol) {
544                         org = screen_pos(vc, pos, viewed);
545                         col = 0;
546                         pos += maxcol;
547                 }
548         }
549
550         if (!count)
551                 return org;
552
553         /* odd pos -- the remaining character */
554         c = *con_buf++;
555         vcs_scr_writew(vc, vc_compile_le16(vcs_scr_readw(vc, org) >> 8, c),
556                                 org);
557
558         return org;
559 }
560
561 static ssize_t
562 vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
563 {
564         struct inode *inode = file_inode(file);
565         struct vc_data *vc;
566         char *con_buf;
567         u16 *org0, *org;
568         unsigned int written;
569         int size;
570         ssize_t ret;
571         loff_t pos;
572         bool viewed, attr;
573
574         if (use_unicode(inode))
575                 return -EOPNOTSUPP;
576
577         con_buf = (char *) __get_free_page(GFP_KERNEL);
578         if (!con_buf)
579                 return -ENOMEM;
580
581         pos = *ppos;
582
583         /* Select the proper current console and verify
584          * sanity of the situation under the console lock.
585          */
586         console_lock();
587
588         attr = use_attributes(inode);
589         ret = -ENXIO;
590         vc = vcs_vc(inode, &viewed);
591         if (!vc)
592                 goto unlock_out;
593
594         size = vcs_size(vc, attr, false);
595         if (size < 0) {
596                 ret = size;
597                 goto unlock_out;
598         }
599         ret = -EINVAL;
600         if (pos < 0 || pos > size)
601                 goto unlock_out;
602         if (count > size - pos)
603                 count = size - pos;
604         written = 0;
605         while (count) {
606                 unsigned int this_round = count;
607
608                 if (this_round > CON_BUF_SIZE)
609                         this_round = CON_BUF_SIZE;
610
611                 /* Temporarily drop the console lock so that we can read
612                  * in the write data from userspace safely.
613                  */
614                 console_unlock();
615                 ret = copy_from_user(con_buf, buf, this_round);
616                 console_lock();
617
618                 if (ret) {
619                         this_round -= ret;
620                         if (!this_round) {
621                                 /* Abort loop if no data were copied. Otherwise
622                                  * fail with -EFAULT.
623                                  */
624                                 if (written)
625                                         break;
626                                 ret = -EFAULT;
627                                 goto unlock_out;
628                         }
629                 }
630
631                 /* The vcs_size might have changed while we slept to grab
632                  * the user buffer, so recheck.
633                  * Return data written up to now on failure.
634                  */
635                 size = vcs_size(vc, attr, false);
636                 if (size < 0) {
637                         if (written)
638                                 break;
639                         ret = size;
640                         goto unlock_out;
641                 }
642                 if (pos >= size)
643                         break;
644                 if (this_round > size - pos)
645                         this_round = size - pos;
646
647                 /* OK, now actually push the write to the console
648                  * under the lock using the local kernel buffer.
649                  */
650
651                 if (attr)
652                         org = vcs_write_buf(vc, con_buf, pos, this_round,
653                                         viewed, &org0);
654                 else
655                         org = vcs_write_buf_noattr(vc, con_buf, pos, this_round,
656                                         viewed, &org0);
657
658                 count -= this_round;
659                 written += this_round;
660                 buf += this_round;
661                 pos += this_round;
662                 if (org)
663                         update_region(vc, (unsigned long)(org0), org - org0);
664         }
665         *ppos += written;
666         ret = written;
667         if (written)
668                 vcs_scr_updated(vc);
669
670 unlock_out:
671         console_unlock();
672         free_page((unsigned long) con_buf);
673         return ret;
674 }
675
676 static __poll_t
677 vcs_poll(struct file *file, poll_table *wait)
678 {
679         struct vcs_poll_data *poll = vcs_poll_data_get(file);
680         __poll_t ret = DEFAULT_POLLMASK|EPOLLERR;
681
682         if (poll) {
683                 poll_wait(file, &poll->waitq, wait);
684                 switch (poll->event) {
685                 case VT_UPDATE:
686                         ret = DEFAULT_POLLMASK|EPOLLPRI;
687                         break;
688                 case VT_DEALLOCATE:
689                         ret = DEFAULT_POLLMASK|EPOLLHUP|EPOLLERR;
690                         break;
691                 case 0:
692                         ret = DEFAULT_POLLMASK;
693                         break;
694                 }
695         }
696         return ret;
697 }
698
699 static int
700 vcs_fasync(int fd, struct file *file, int on)
701 {
702         struct vcs_poll_data *poll = file->private_data;
703
704         if (!poll) {
705                 /* don't allocate anything if all we want is disable fasync */
706                 if (!on)
707                         return 0;
708                 poll = vcs_poll_data_get(file);
709                 if (!poll)
710                         return -ENOMEM;
711         }
712
713         return fasync_helper(fd, file, on, &poll->fasync);
714 }
715
716 static int
717 vcs_open(struct inode *inode, struct file *filp)
718 {
719         unsigned int currcons = console(inode);
720         bool attr = use_attributes(inode);
721         bool uni_mode = use_unicode(inode);
722         int ret = 0;
723
724         /* we currently don't support attributes in unicode mode */
725         if (attr && uni_mode)
726                 return -EOPNOTSUPP;
727
728         console_lock();
729         if(currcons && !vc_cons_allocated(currcons-1))
730                 ret = -ENXIO;
731         console_unlock();
732         return ret;
733 }
734
735 static int vcs_release(struct inode *inode, struct file *file)
736 {
737         struct vcs_poll_data *poll = file->private_data;
738
739         if (poll)
740                 vcs_poll_data_free(poll);
741         return 0;
742 }
743
744 static const struct file_operations vcs_fops = {
745         .llseek         = vcs_lseek,
746         .read           = vcs_read,
747         .write          = vcs_write,
748         .poll           = vcs_poll,
749         .fasync         = vcs_fasync,
750         .open           = vcs_open,
751         .release        = vcs_release,
752 };
753
754 static struct class *vc_class;
755
756 void vcs_make_sysfs(int index)
757 {
758         device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL,
759                       "vcs%u", index + 1);
760         device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 65), NULL,
761                       "vcsu%u", index + 1);
762         device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL,
763                       "vcsa%u", index + 1);
764 }
765
766 void vcs_remove_sysfs(int index)
767 {
768         device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 1));
769         device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 65));
770         device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 129));
771 }
772
773 int __init vcs_init(void)
774 {
775         unsigned int i;
776
777         if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
778                 panic("unable to get major %d for vcs device", VCS_MAJOR);
779         vc_class = class_create(THIS_MODULE, "vc");
780
781         device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
782         device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 64), NULL, "vcsu");
783         device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");
784         for (i = 0; i < MIN_NR_CONSOLES; i++)
785                 vcs_make_sysfs(i);
786         return 0;
787 }