Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Provide access to virtual console memory. |
| 4 | * /dev/vcs0: 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 | * |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/kernel.h> |
| 30 | #include <linux/major.h> |
| 31 | #include <linux/errno.h> |
Paul Gortmaker | 0e648f4 | 2011-05-27 10:46:24 -0400 | [diff] [blame] | 32 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/tty.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/device.h> |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 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> |
Matthias Kaehlcke | c831c33 | 2007-05-08 00:39:49 -0700 | [diff] [blame] | 48 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 49 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <asm/byteorder.h> |
| 51 | #include <asm/unaligned.h> |
| 52 | |
| 53 | #undef attr |
| 54 | #undef org |
| 55 | #undef addr |
| 56 | #define HEADER_SIZE 4 |
| 57 | |
Jiri Olsa | fcdba07 | 2011-02-07 19:31:25 +0100 | [diff] [blame] | 58 | #define CON_BUF_SIZE (CONFIG_BASE_SMALL ? 256 : PAGE_SIZE) |
| 59 | |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 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 | |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 80 | struct vcs_poll_data { |
| 81 | struct notifier_block notifier; |
| 82 | unsigned int cons_num; |
| 83 | bool seen_last_update; |
| 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 | |
| 97 | if (code != VT_UPDATE) |
| 98 | return NOTIFY_DONE; |
| 99 | |
| 100 | if (currcons == 0) |
| 101 | currcons = fg_console; |
| 102 | else |
| 103 | currcons--; |
| 104 | if (currcons != vc->vc_num) |
| 105 | return NOTIFY_DONE; |
| 106 | |
| 107 | poll->seen_last_update = false; |
| 108 | wake_up_interruptible(&poll->waitq); |
| 109 | kill_fasync(&poll->fasync, SIGIO, POLL_IN); |
| 110 | return NOTIFY_OK; |
| 111 | } |
| 112 | |
| 113 | static void |
| 114 | vcs_poll_data_free(struct vcs_poll_data *poll) |
| 115 | { |
| 116 | unregister_vt_notifier(&poll->notifier); |
| 117 | kfree(poll); |
| 118 | } |
| 119 | |
| 120 | static struct vcs_poll_data * |
| 121 | vcs_poll_data_get(struct file *file) |
| 122 | { |
Al Viro | e8cd816 | 2013-03-26 20:30:17 -0400 | [diff] [blame] | 123 | struct vcs_poll_data *poll = file->private_data, *kill = NULL; |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 124 | |
| 125 | if (poll) |
| 126 | return poll; |
| 127 | |
| 128 | poll = kzalloc(sizeof(*poll), GFP_KERNEL); |
| 129 | if (!poll) |
| 130 | return NULL; |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 131 | poll->cons_num = console(file_inode(file)); |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 132 | init_waitqueue_head(&poll->waitq); |
| 133 | poll->notifier.notifier_call = vcs_notifier; |
| 134 | if (register_vt_notifier(&poll->notifier) != 0) { |
| 135 | kfree(poll); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * This code may be called either through ->poll() or ->fasync(). |
| 141 | * If we have two threads using the same file descriptor, they could |
| 142 | * both enter this function, both notice that the structure hasn't |
| 143 | * been allocated yet and go ahead allocating it in parallel, but |
| 144 | * only one of them must survive and be shared otherwise we'd leak |
| 145 | * memory with a dangling notifier callback. |
| 146 | */ |
| 147 | spin_lock(&file->f_lock); |
| 148 | if (!file->private_data) { |
| 149 | file->private_data = poll; |
| 150 | } else { |
| 151 | /* someone else raced ahead of us */ |
Al Viro | e8cd816 | 2013-03-26 20:30:17 -0400 | [diff] [blame] | 152 | kill = poll; |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 153 | poll = file->private_data; |
| 154 | } |
| 155 | spin_unlock(&file->f_lock); |
Al Viro | e8cd816 | 2013-03-26 20:30:17 -0400 | [diff] [blame] | 156 | if (kill) |
| 157 | vcs_poll_data_free(kill); |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 158 | |
| 159 | return poll; |
| 160 | } |
| 161 | |
Jiri Olsa | fcdba07 | 2011-02-07 19:31:25 +0100 | [diff] [blame] | 162 | /* |
| 163 | * Returns VC for inode. |
| 164 | * Must be called with console_lock. |
| 165 | */ |
| 166 | static struct vc_data* |
| 167 | vcs_vc(struct inode *inode, int *viewed) |
| 168 | { |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 169 | unsigned int currcons = console(inode); |
Jiri Olsa | fcdba07 | 2011-02-07 19:31:25 +0100 | [diff] [blame] | 170 | |
| 171 | WARN_CONSOLE_UNLOCKED(); |
| 172 | |
| 173 | if (currcons == 0) { |
| 174 | currcons = fg_console; |
| 175 | if (viewed) |
| 176 | *viewed = 1; |
| 177 | } else { |
| 178 | currcons--; |
| 179 | if (viewed) |
| 180 | *viewed = 0; |
| 181 | } |
| 182 | return vc_cons[currcons].d; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Returns size for VC carried by inode. |
| 187 | * Must be called with console_lock. |
| 188 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | static int |
| 190 | vcs_size(struct inode *inode) |
| 191 | { |
| 192 | int size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | struct vc_data *vc; |
| 194 | |
Jiri Olsa | fcdba07 | 2011-02-07 19:31:25 +0100 | [diff] [blame] | 195 | WARN_CONSOLE_UNLOCKED(); |
| 196 | |
| 197 | vc = vcs_vc(inode, NULL); |
| 198 | if (!vc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | size = vc->vc_rows * vc->vc_cols; |
| 202 | |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 203 | if (use_attributes(inode)) { |
| 204 | if (use_unicode(inode)) |
| 205 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | size = 2*size + HEADER_SIZE; |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 207 | } else if (use_unicode(inode)) |
| 208 | size *= 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | return size; |
| 210 | } |
| 211 | |
| 212 | static loff_t vcs_lseek(struct file *file, loff_t offset, int orig) |
| 213 | { |
| 214 | int size; |
| 215 | |
Jiri Olsa | dc1892c | 2011-02-07 19:31:24 +0100 | [diff] [blame] | 216 | console_lock(); |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 217 | size = vcs_size(file_inode(file)); |
Jiri Olsa | dc1892c | 2011-02-07 19:31:24 +0100 | [diff] [blame] | 218 | console_unlock(); |
Jiri Olsa | fcdba07 | 2011-02-07 19:31:25 +0100 | [diff] [blame] | 219 | if (size < 0) |
Jiri Olsa | dc1892c | 2011-02-07 19:31:24 +0100 | [diff] [blame] | 220 | return size; |
Al Viro | 6500427 | 2013-06-17 15:31:22 +0400 | [diff] [blame] | 221 | return fixed_size_llseek(file, offset, orig, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | |
| 225 | static ssize_t |
| 226 | vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 227 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 228 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | struct vc_data *vc; |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 230 | struct vcs_poll_data *poll; |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 231 | long pos, read; |
| 232 | int attr, uni_mode, row, col, maxcol, viewed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | unsigned short *org = NULL; |
| 234 | ssize_t ret; |
Jiri Olsa | fcdba07 | 2011-02-07 19:31:25 +0100 | [diff] [blame] | 235 | char *con_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Jiri Olsa | fcdba07 | 2011-02-07 19:31:25 +0100 | [diff] [blame] | 237 | con_buf = (char *) __get_free_page(GFP_KERNEL); |
| 238 | if (!con_buf) |
| 239 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | pos = *ppos; |
| 242 | |
| 243 | /* Select the proper current console and verify |
| 244 | * sanity of the situation under the console lock. |
| 245 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 246 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 248 | uni_mode = use_unicode(inode); |
| 249 | attr = use_attributes(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | ret = -ENXIO; |
Jiri Olsa | fcdba07 | 2011-02-07 19:31:25 +0100 | [diff] [blame] | 251 | vc = vcs_vc(inode, &viewed); |
| 252 | if (!vc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | goto unlock_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | ret = -EINVAL; |
| 256 | if (pos < 0) |
| 257 | goto unlock_out; |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 258 | /* we enforce 32-bit alignment for pos and count in unicode mode */ |
| 259 | if (uni_mode && (pos | count) & 3) |
| 260 | goto unlock_out; |
| 261 | |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 262 | poll = file->private_data; |
| 263 | if (count && poll) |
| 264 | poll->seen_last_update = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | read = 0; |
| 266 | ret = 0; |
| 267 | while (count) { |
| 268 | char *con_buf0, *con_buf_start; |
| 269 | long this_round, size; |
| 270 | ssize_t orig_count; |
| 271 | long p = pos; |
| 272 | |
| 273 | /* Check whether we are above size each round, |
| 274 | * as copy_to_user at the end of this loop |
| 275 | * could sleep. |
| 276 | */ |
| 277 | size = vcs_size(inode); |
Jiri Olsa | dc1892c | 2011-02-07 19:31:24 +0100 | [diff] [blame] | 278 | if (size < 0) { |
| 279 | if (read) |
| 280 | break; |
| 281 | ret = size; |
| 282 | goto unlock_out; |
| 283 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | if (pos >= size) |
| 285 | break; |
| 286 | if (count > size - pos) |
| 287 | count = size - pos; |
| 288 | |
| 289 | this_round = count; |
| 290 | if (this_round > CON_BUF_SIZE) |
| 291 | this_round = CON_BUF_SIZE; |
| 292 | |
| 293 | /* Perform the whole read into the local con_buf. |
| 294 | * Then we can drop the console spinlock and safely |
| 295 | * attempt to move it to userspace. |
| 296 | */ |
| 297 | |
| 298 | con_buf_start = con_buf0 = con_buf; |
| 299 | orig_count = this_round; |
| 300 | maxcol = vc->vc_cols; |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 301 | if (uni_mode) { |
| 302 | unsigned int nr; |
| 303 | |
| 304 | ret = vc_uniscr_check(vc); |
| 305 | if (ret) |
| 306 | break; |
| 307 | p /= 4; |
| 308 | row = p / vc->vc_cols; |
| 309 | col = p % maxcol; |
| 310 | nr = maxcol - col; |
| 311 | do { |
| 312 | if (nr > this_round/4) |
| 313 | nr = this_round/4; |
Nicolas Pitre | 708d0bf | 2018-06-26 23:56:42 -0400 | [diff] [blame] | 314 | vc_uniscr_copy_line(vc, con_buf0, viewed, |
| 315 | row, col, nr); |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 316 | con_buf0 += nr * 4; |
| 317 | this_round -= nr * 4; |
| 318 | row++; |
| 319 | col = 0; |
| 320 | nr = maxcol; |
| 321 | } while (this_round); |
| 322 | } else if (!attr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | org = screen_pos(vc, p, viewed); |
| 324 | col = p % maxcol; |
| 325 | p += maxcol - col; |
| 326 | while (this_round-- > 0) { |
| 327 | *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff); |
| 328 | if (++col == maxcol) { |
| 329 | org = screen_pos(vc, p, viewed); |
| 330 | col = 0; |
| 331 | p += maxcol; |
| 332 | } |
| 333 | } |
| 334 | } else { |
| 335 | if (p < HEADER_SIZE) { |
| 336 | size_t tmp_count; |
| 337 | |
| 338 | con_buf0[0] = (char)vc->vc_rows; |
| 339 | con_buf0[1] = (char)vc->vc_cols; |
| 340 | getconsxy(vc, con_buf0 + 2); |
| 341 | |
| 342 | con_buf_start += p; |
| 343 | this_round += p; |
| 344 | if (this_round > CON_BUF_SIZE) { |
| 345 | this_round = CON_BUF_SIZE; |
| 346 | orig_count = this_round - p; |
| 347 | } |
| 348 | |
| 349 | tmp_count = HEADER_SIZE; |
| 350 | if (tmp_count > this_round) |
| 351 | tmp_count = this_round; |
| 352 | |
| 353 | /* Advance state pointers and move on. */ |
| 354 | this_round -= tmp_count; |
| 355 | p = HEADER_SIZE; |
| 356 | con_buf0 = con_buf + HEADER_SIZE; |
| 357 | /* If this_round >= 0, then p is even... */ |
| 358 | } else if (p & 1) { |
| 359 | /* Skip first byte for output if start address is odd |
| 360 | * Update region sizes up/down depending on free |
| 361 | * space in buffer. |
| 362 | */ |
| 363 | con_buf_start++; |
| 364 | if (this_round < CON_BUF_SIZE) |
| 365 | this_round++; |
| 366 | else |
| 367 | orig_count--; |
| 368 | } |
| 369 | if (this_round > 0) { |
| 370 | unsigned short *tmp_buf = (unsigned short *)con_buf0; |
| 371 | |
| 372 | p -= HEADER_SIZE; |
| 373 | p /= 2; |
| 374 | col = p % maxcol; |
| 375 | |
| 376 | org = screen_pos(vc, p, viewed); |
| 377 | p += maxcol - col; |
| 378 | |
| 379 | /* Buffer has even length, so we can always copy |
| 380 | * character + attribute. We do not copy last byte |
| 381 | * to userspace if this_round is odd. |
| 382 | */ |
| 383 | this_round = (this_round + 1) >> 1; |
| 384 | |
| 385 | while (this_round) { |
| 386 | *tmp_buf++ = vcs_scr_readw(vc, org++); |
| 387 | this_round --; |
| 388 | if (++col == maxcol) { |
| 389 | org = screen_pos(vc, p, viewed); |
| 390 | col = 0; |
| 391 | p += maxcol; |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /* Finally, release the console semaphore while we push |
| 398 | * all the data to userspace from our temporary buffer. |
| 399 | * |
| 400 | * AKPM: Even though it's a semaphore, we should drop it because |
| 401 | * the pagefault handling code may want to call printk(). |
| 402 | */ |
| 403 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 404 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | ret = copy_to_user(buf, con_buf_start, orig_count); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 406 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
| 408 | if (ret) { |
| 409 | read += (orig_count - ret); |
| 410 | ret = -EFAULT; |
| 411 | break; |
| 412 | } |
| 413 | buf += orig_count; |
| 414 | pos += orig_count; |
| 415 | read += orig_count; |
| 416 | count -= orig_count; |
| 417 | } |
| 418 | *ppos += read; |
| 419 | if (read) |
| 420 | ret = read; |
| 421 | unlock_out: |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 422 | console_unlock(); |
Jiri Olsa | fcdba07 | 2011-02-07 19:31:25 +0100 | [diff] [blame] | 423 | free_page((unsigned long) con_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | static ssize_t |
| 428 | vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
| 429 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 430 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | struct vc_data *vc; |
| 432 | long pos; |
Jiri Olsa | fcdba07 | 2011-02-07 19:31:25 +0100 | [diff] [blame] | 433 | long attr, size, written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | char *con_buf0; |
Jiri Olsa | fcdba07 | 2011-02-07 19:31:25 +0100 | [diff] [blame] | 435 | int col, maxcol, viewed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | u16 *org0 = NULL, *org = NULL; |
| 437 | size_t ret; |
Jiri Olsa | fcdba07 | 2011-02-07 19:31:25 +0100 | [diff] [blame] | 438 | char *con_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Nicolas Pitre | 627f3b9 | 2019-11-05 10:33:16 +0100 | [diff] [blame] | 440 | if (use_unicode(inode)) |
| 441 | return -EOPNOTSUPP; |
| 442 | |
Jiri Olsa | fcdba07 | 2011-02-07 19:31:25 +0100 | [diff] [blame] | 443 | con_buf = (char *) __get_free_page(GFP_KERNEL); |
| 444 | if (!con_buf) |
| 445 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
| 447 | pos = *ppos; |
| 448 | |
| 449 | /* Select the proper current console and verify |
| 450 | * sanity of the situation under the console lock. |
| 451 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 452 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 454 | attr = use_attributes(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | ret = -ENXIO; |
Jiri Olsa | fcdba07 | 2011-02-07 19:31:25 +0100 | [diff] [blame] | 456 | vc = vcs_vc(inode, &viewed); |
| 457 | if (!vc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | goto unlock_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
| 460 | size = vcs_size(inode); |
| 461 | ret = -EINVAL; |
| 462 | if (pos < 0 || pos > size) |
| 463 | goto unlock_out; |
| 464 | if (count > size - pos) |
| 465 | count = size - pos; |
| 466 | written = 0; |
| 467 | while (count) { |
| 468 | long this_round = count; |
| 469 | size_t orig_count; |
| 470 | long p; |
| 471 | |
| 472 | if (this_round > CON_BUF_SIZE) |
| 473 | this_round = CON_BUF_SIZE; |
| 474 | |
| 475 | /* Temporarily drop the console lock so that we can read |
| 476 | * in the write data from userspace safely. |
| 477 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 478 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | ret = copy_from_user(con_buf, buf, this_round); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 480 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
| 482 | if (ret) { |
| 483 | this_round -= ret; |
| 484 | if (!this_round) { |
| 485 | /* Abort loop if no data were copied. Otherwise |
| 486 | * fail with -EFAULT. |
| 487 | */ |
| 488 | if (written) |
| 489 | break; |
| 490 | ret = -EFAULT; |
| 491 | goto unlock_out; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | /* The vcs_size might have changed while we slept to grab |
| 496 | * the user buffer, so recheck. |
| 497 | * Return data written up to now on failure. |
| 498 | */ |
| 499 | size = vcs_size(inode); |
Jiri Olsa | dc1892c | 2011-02-07 19:31:24 +0100 | [diff] [blame] | 500 | if (size < 0) { |
| 501 | if (written) |
| 502 | break; |
| 503 | ret = size; |
| 504 | goto unlock_out; |
| 505 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | if (pos >= size) |
| 507 | break; |
| 508 | if (this_round > size - pos) |
| 509 | this_round = size - pos; |
| 510 | |
| 511 | /* OK, now actually push the write to the console |
| 512 | * under the lock using the local kernel buffer. |
| 513 | */ |
| 514 | |
| 515 | con_buf0 = con_buf; |
| 516 | orig_count = this_round; |
| 517 | maxcol = vc->vc_cols; |
| 518 | p = pos; |
| 519 | if (!attr) { |
| 520 | org0 = org = screen_pos(vc, p, viewed); |
| 521 | col = p % maxcol; |
| 522 | p += maxcol - col; |
| 523 | |
| 524 | while (this_round > 0) { |
| 525 | unsigned char c = *con_buf0++; |
| 526 | |
| 527 | this_round--; |
| 528 | vcs_scr_writew(vc, |
| 529 | (vcs_scr_readw(vc, org) & 0xff00) | c, org); |
| 530 | org++; |
| 531 | if (++col == maxcol) { |
| 532 | org = screen_pos(vc, p, viewed); |
| 533 | col = 0; |
| 534 | p += maxcol; |
| 535 | } |
| 536 | } |
| 537 | } else { |
| 538 | if (p < HEADER_SIZE) { |
| 539 | char header[HEADER_SIZE]; |
| 540 | |
| 541 | getconsxy(vc, header + 2); |
| 542 | while (p < HEADER_SIZE && this_round > 0) { |
| 543 | this_round--; |
| 544 | header[p++] = *con_buf0++; |
| 545 | } |
| 546 | if (!viewed) |
| 547 | putconsxy(vc, header + 2); |
| 548 | } |
| 549 | p -= HEADER_SIZE; |
| 550 | col = (p/2) % maxcol; |
| 551 | if (this_round > 0) { |
| 552 | org0 = org = screen_pos(vc, p/2, viewed); |
| 553 | if ((p & 1) && this_round > 0) { |
| 554 | char c; |
| 555 | |
| 556 | this_round--; |
| 557 | c = *con_buf0++; |
| 558 | #ifdef __BIG_ENDIAN |
| 559 | vcs_scr_writew(vc, c | |
| 560 | (vcs_scr_readw(vc, org) & 0xff00), org); |
| 561 | #else |
| 562 | vcs_scr_writew(vc, (c << 8) | |
| 563 | (vcs_scr_readw(vc, org) & 0xff), org); |
| 564 | #endif |
| 565 | org++; |
| 566 | p++; |
| 567 | if (++col == maxcol) { |
| 568 | org = screen_pos(vc, p/2, viewed); |
| 569 | col = 0; |
| 570 | } |
| 571 | } |
| 572 | p /= 2; |
| 573 | p += maxcol - col; |
| 574 | } |
| 575 | while (this_round > 1) { |
| 576 | unsigned short w; |
| 577 | |
Dave Jones | ee02594 | 2005-12-28 20:01:04 -0500 | [diff] [blame] | 578 | w = get_unaligned(((unsigned short *)con_buf0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | vcs_scr_writew(vc, w, org++); |
| 580 | con_buf0 += 2; |
| 581 | this_round -= 2; |
| 582 | if (++col == maxcol) { |
| 583 | org = screen_pos(vc, p, viewed); |
| 584 | col = 0; |
| 585 | p += maxcol; |
| 586 | } |
| 587 | } |
| 588 | if (this_round > 0) { |
| 589 | unsigned char c; |
| 590 | |
| 591 | c = *con_buf0++; |
| 592 | #ifdef __BIG_ENDIAN |
| 593 | vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org); |
| 594 | #else |
| 595 | vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org); |
| 596 | #endif |
| 597 | } |
| 598 | } |
| 599 | count -= orig_count; |
| 600 | written += orig_count; |
| 601 | buf += orig_count; |
| 602 | pos += orig_count; |
| 603 | if (org0) |
| 604 | update_region(vc, (unsigned long)(org0), org - org0); |
| 605 | } |
| 606 | *ppos += written; |
| 607 | ret = written; |
Nicolas Pitre | 432c9ed | 2010-10-01 00:10:44 -0400 | [diff] [blame] | 608 | if (written) |
| 609 | vcs_scr_updated(vc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 611 | unlock_out: |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 612 | console_unlock(); |
Jiri Olsa | fcdba07 | 2011-02-07 19:31:25 +0100 | [diff] [blame] | 613 | free_page((unsigned long) con_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | return ret; |
| 615 | } |
| 616 | |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 617 | static __poll_t |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 618 | vcs_poll(struct file *file, poll_table *wait) |
| 619 | { |
| 620 | struct vcs_poll_data *poll = vcs_poll_data_get(file); |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 621 | __poll_t ret = DEFAULT_POLLMASK|EPOLLERR|EPOLLPRI; |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 622 | |
| 623 | if (poll) { |
| 624 | poll_wait(file, &poll->waitq, wait); |
Nicolas Pitre | 47c344d | 2010-11-10 01:33:12 -0500 | [diff] [blame] | 625 | if (poll->seen_last_update) |
| 626 | ret = DEFAULT_POLLMASK; |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 627 | } |
| 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | static int |
| 632 | vcs_fasync(int fd, struct file *file, int on) |
| 633 | { |
| 634 | struct vcs_poll_data *poll = file->private_data; |
| 635 | |
| 636 | if (!poll) { |
| 637 | /* don't allocate anything if all we want is disable fasync */ |
| 638 | if (!on) |
| 639 | return 0; |
| 640 | poll = vcs_poll_data_get(file); |
| 641 | if (!poll) |
| 642 | return -ENOMEM; |
| 643 | } |
| 644 | |
| 645 | return fasync_helper(fd, file, on, &poll->fasync); |
| 646 | } |
| 647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | static int |
| 649 | vcs_open(struct inode *inode, struct file *filp) |
| 650 | { |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 651 | unsigned int currcons = console(inode); |
| 652 | bool attr = use_attributes(inode); |
| 653 | bool uni_mode = use_unicode(inode); |
Jonathan Corbet | f97259e | 2008-05-16 13:47:50 -0600 | [diff] [blame] | 654 | int ret = 0; |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 655 | |
| 656 | /* we currently don't support attributes in unicode mode */ |
| 657 | if (attr && uni_mode) |
| 658 | return -EOPNOTSUPP; |
| 659 | |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 660 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | if(currcons && !vc_cons_allocated(currcons-1)) |
Jonathan Corbet | f97259e | 2008-05-16 13:47:50 -0600 | [diff] [blame] | 662 | ret = -ENXIO; |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 663 | console_unlock(); |
Jonathan Corbet | f97259e | 2008-05-16 13:47:50 -0600 | [diff] [blame] | 664 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 667 | static int vcs_release(struct inode *inode, struct file *file) |
| 668 | { |
| 669 | struct vcs_poll_data *poll = file->private_data; |
| 670 | |
| 671 | if (poll) |
| 672 | vcs_poll_data_free(poll); |
| 673 | return 0; |
| 674 | } |
| 675 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 676 | static const struct file_operations vcs_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | .llseek = vcs_lseek, |
| 678 | .read = vcs_read, |
| 679 | .write = vcs_write, |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 680 | .poll = vcs_poll, |
| 681 | .fasync = vcs_fasync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | .open = vcs_open, |
Nicolas Pitre | 47725ac | 2010-10-05 14:22:37 -0400 | [diff] [blame] | 683 | .release = vcs_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | }; |
| 685 | |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 686 | static struct class *vc_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
Kay Sievers | 4995f8e | 2009-03-09 14:18:52 +0100 | [diff] [blame] | 688 | void vcs_make_sysfs(int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | { |
Kay Sievers | 4995f8e | 2009-03-09 14:18:52 +0100 | [diff] [blame] | 690 | device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL, |
| 691 | "vcs%u", index + 1); |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 692 | device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 65), NULL, |
| 693 | "vcsu%u", index + 1); |
Kay Sievers | 4995f8e | 2009-03-09 14:18:52 +0100 | [diff] [blame] | 694 | device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL, |
| 695 | "vcsa%u", index + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } |
Alan Cox | d09d7dd | 2006-09-29 01:59:47 -0700 | [diff] [blame] | 697 | |
Kay Sievers | 4995f8e | 2009-03-09 14:18:52 +0100 | [diff] [blame] | 698 | void vcs_remove_sysfs(int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | { |
Kay Sievers | 4995f8e | 2009-03-09 14:18:52 +0100 | [diff] [blame] | 700 | device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 1)); |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 701 | device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 65)); |
Kay Sievers | 4995f8e | 2009-03-09 14:18:52 +0100 | [diff] [blame] | 702 | device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 129)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | int __init vcs_init(void) |
| 706 | { |
Kay Sievers | c46a7ae | 2009-07-20 16:04:55 +0100 | [diff] [blame] | 707 | unsigned int i; |
| 708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops)) |
| 710 | panic("unable to get major %d for vcs device", VCS_MAJOR); |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 711 | vc_class = class_create(THIS_MODULE, "vc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
Greg Kroah-Hartman | 03457cd | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 713 | device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs"); |
Nicolas Pitre | d21b0be | 2018-06-26 23:56:41 -0400 | [diff] [blame] | 714 | device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 64), NULL, "vcsu"); |
Greg Kroah-Hartman | 03457cd | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 715 | device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa"); |
Kay Sievers | c46a7ae | 2009-07-20 16:04:55 +0100 | [diff] [blame] | 716 | for (i = 0; i < MIN_NR_CONSOLES; i++) |
| 717 | vcs_make_sysfs(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | return 0; |
| 719 | } |