vc_screen.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  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. *
  13. * This replaces screendump and part of selection, so that the system
  14. * administrator can control access using file system permissions.
  15. *
  16. * aeb@cwi.nl - efter Friedas begravelse - 950211
  17. *
  18. * machek@k332.feld.cvut.cz - modified not to send characters to wrong console
  19. * - fixed some fatal off-by-one bugs (0-- no longer == -1 -> looping and looping and looping...)
  20. * - making it shorter - scr_readw are macros which expand in PRETTY long code
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/major.h>
  24. #include <linux/errno.h>
  25. #include <linux/export.h>
  26. #include <linux/tty.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/mm.h>
  29. #include <linux/init.h>
  30. #include <linux/vt_kern.h>
  31. #include <linux/selection.h>
  32. #include <linux/kbd_kern.h>
  33. #include <linux/console.h>
  34. #include <linux/device.h>
  35. #include <linux/sched.h>
  36. #include <linux/fs.h>
  37. #include <linux/poll.h>
  38. #include <linux/signal.h>
  39. #include <linux/slab.h>
  40. #include <linux/notifier.h>
  41. #include <linux/uaccess.h>
  42. #include <asm/byteorder.h>
  43. #include <asm/unaligned.h>
  44. #undef attr
  45. #undef org
  46. #undef addr
  47. #define HEADER_SIZE 4
  48. #define CON_BUF_SIZE (CONFIG_BASE_SMALL ? 256 : PAGE_SIZE)
  49. struct vcs_poll_data {
  50. struct notifier_block notifier;
  51. unsigned int cons_num;
  52. bool seen_last_update;
  53. wait_queue_head_t waitq;
  54. struct fasync_struct *fasync;
  55. };
  56. static int
  57. vcs_notifier(struct notifier_block *nb, unsigned long code, void *_param)
  58. {
  59. struct vt_notifier_param *param = _param;
  60. struct vc_data *vc = param->vc;
  61. struct vcs_poll_data *poll =
  62. container_of(nb, struct vcs_poll_data, notifier);
  63. int currcons = poll->cons_num;
  64. if (code != VT_UPDATE)
  65. return NOTIFY_DONE;
  66. if (currcons == 0)
  67. currcons = fg_console;
  68. else
  69. currcons--;
  70. if (currcons != vc->vc_num)
  71. return NOTIFY_DONE;
  72. poll->seen_last_update = false;
  73. wake_up_interruptible(&poll->waitq);
  74. kill_fasync(&poll->fasync, SIGIO, POLL_IN);
  75. return NOTIFY_OK;
  76. }
  77. static void
  78. vcs_poll_data_free(struct vcs_poll_data *poll)
  79. {
  80. unregister_vt_notifier(&poll->notifier);
  81. kfree(poll);
  82. }
  83. static struct vcs_poll_data *
  84. vcs_poll_data_get(struct file *file)
  85. {
  86. struct vcs_poll_data *poll = file->private_data, *kill = NULL;
  87. if (poll)
  88. return poll;
  89. poll = kzalloc(sizeof(*poll), GFP_KERNEL);
  90. if (!poll)
  91. return NULL;
  92. poll->cons_num = iminor(file_inode(file)) & 127;
  93. init_waitqueue_head(&poll->waitq);
  94. poll->notifier.notifier_call = vcs_notifier;
  95. if (register_vt_notifier(&poll->notifier) != 0) {
  96. kfree(poll);
  97. return NULL;
  98. }
  99. /*
  100. * This code may be called either through ->poll() or ->fasync().
  101. * If we have two threads using the same file descriptor, they could
  102. * both enter this function, both notice that the structure hasn't
  103. * been allocated yet and go ahead allocating it in parallel, but
  104. * only one of them must survive and be shared otherwise we'd leak
  105. * memory with a dangling notifier callback.
  106. */
  107. spin_lock(&file->f_lock);
  108. if (!file->private_data) {
  109. file->private_data = poll;
  110. } else {
  111. /* someone else raced ahead of us */
  112. kill = poll;
  113. poll = file->private_data;
  114. }
  115. spin_unlock(&file->f_lock);
  116. if (kill)
  117. vcs_poll_data_free(kill);
  118. return poll;
  119. }
  120. /*
  121. * Returns VC for inode.
  122. * Must be called with console_lock.
  123. */
  124. static struct vc_data*
  125. vcs_vc(struct inode *inode, int *viewed)
  126. {
  127. unsigned int currcons = iminor(inode) & 127;
  128. WARN_CONSOLE_UNLOCKED();
  129. if (currcons == 0) {
  130. currcons = fg_console;
  131. if (viewed)
  132. *viewed = 1;
  133. } else {
  134. currcons--;
  135. if (viewed)
  136. *viewed = 0;
  137. }
  138. return vc_cons[currcons].d;
  139. }
  140. /*
  141. * Returns size for VC carried by inode.
  142. * Must be called with console_lock.
  143. */
  144. static int
  145. vcs_size(struct inode *inode)
  146. {
  147. int size;
  148. int minor = iminor(inode);
  149. struct vc_data *vc;
  150. WARN_CONSOLE_UNLOCKED();
  151. vc = vcs_vc(inode, NULL);
  152. if (!vc)
  153. return -ENXIO;
  154. size = vc->vc_rows * vc->vc_cols;
  155. if (minor & 128)
  156. size = 2*size + HEADER_SIZE;
  157. return size;
  158. }
  159. static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
  160. {
  161. int size;
  162. console_lock();
  163. size = vcs_size(file_inode(file));
  164. console_unlock();
  165. if (size < 0)
  166. return size;
  167. return fixed_size_llseek(file, offset, orig, size);
  168. }
  169. static ssize_t
  170. vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  171. {
  172. struct inode *inode = file_inode(file);
  173. unsigned int currcons = iminor(inode);
  174. struct vc_data *vc;
  175. struct vcs_poll_data *poll;
  176. long pos;
  177. long attr, read;
  178. int col, maxcol, viewed;
  179. unsigned short *org = NULL;
  180. ssize_t ret;
  181. char *con_buf;
  182. con_buf = (char *) __get_free_page(GFP_KERNEL);
  183. if (!con_buf)
  184. return -ENOMEM;
  185. pos = *ppos;
  186. /* Select the proper current console and verify
  187. * sanity of the situation under the console lock.
  188. */
  189. console_lock();
  190. attr = (currcons & 128);
  191. ret = -ENXIO;
  192. vc = vcs_vc(inode, &viewed);
  193. if (!vc)
  194. goto unlock_out;
  195. ret = -EINVAL;
  196. if (pos < 0)
  197. goto unlock_out;
  198. poll = file->private_data;
  199. if (count && poll)
  200. poll->seen_last_update = true;
  201. read = 0;
  202. ret = 0;
  203. while (count) {
  204. char *con_buf0, *con_buf_start;
  205. long this_round, size;
  206. ssize_t orig_count;
  207. long p = pos;
  208. /* Check whether we are above size each round,
  209. * as copy_to_user at the end of this loop
  210. * could sleep.
  211. */
  212. size = vcs_size(inode);
  213. if (size < 0) {
  214. if (read)
  215. break;
  216. ret = size;
  217. goto unlock_out;
  218. }
  219. if (pos >= size)
  220. break;
  221. if (count > size - pos)
  222. count = size - pos;
  223. this_round = count;
  224. if (this_round > CON_BUF_SIZE)
  225. this_round = CON_BUF_SIZE;
  226. /* Perform the whole read into the local con_buf.
  227. * Then we can drop the console spinlock and safely
  228. * attempt to move it to userspace.
  229. */
  230. con_buf_start = con_buf0 = con_buf;
  231. orig_count = this_round;
  232. maxcol = vc->vc_cols;
  233. if (!attr) {
  234. org = screen_pos(vc, p, viewed);
  235. col = p % maxcol;
  236. p += maxcol - col;
  237. while (this_round-- > 0) {
  238. *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff);
  239. if (++col == maxcol) {
  240. org = screen_pos(vc, p, viewed);
  241. col = 0;
  242. p += maxcol;
  243. }
  244. }
  245. } else {
  246. if (p < HEADER_SIZE) {
  247. size_t tmp_count;
  248. con_buf0[0] = (char)vc->vc_rows;
  249. con_buf0[1] = (char)vc->vc_cols;
  250. getconsxy(vc, con_buf0 + 2);
  251. con_buf_start += p;
  252. this_round += p;
  253. if (this_round > CON_BUF_SIZE) {
  254. this_round = CON_BUF_SIZE;
  255. orig_count = this_round - p;
  256. }
  257. tmp_count = HEADER_SIZE;
  258. if (tmp_count > this_round)
  259. tmp_count = this_round;
  260. /* Advance state pointers and move on. */
  261. this_round -= tmp_count;
  262. p = HEADER_SIZE;
  263. con_buf0 = con_buf + HEADER_SIZE;
  264. /* If this_round >= 0, then p is even... */
  265. } else if (p & 1) {
  266. /* Skip first byte for output if start address is odd
  267. * Update region sizes up/down depending on free
  268. * space in buffer.
  269. */
  270. con_buf_start++;
  271. if (this_round < CON_BUF_SIZE)
  272. this_round++;
  273. else
  274. orig_count--;
  275. }
  276. if (this_round > 0) {
  277. unsigned short *tmp_buf = (unsigned short *)con_buf0;
  278. p -= HEADER_SIZE;
  279. p /= 2;
  280. col = p % maxcol;
  281. org = screen_pos(vc, p, viewed);
  282. p += maxcol - col;
  283. /* Buffer has even length, so we can always copy
  284. * character + attribute. We do not copy last byte
  285. * to userspace if this_round is odd.
  286. */
  287. this_round = (this_round + 1) >> 1;
  288. while (this_round) {
  289. *tmp_buf++ = vcs_scr_readw(vc, org++);
  290. this_round --;
  291. if (++col == maxcol) {
  292. org = screen_pos(vc, p, viewed);
  293. col = 0;
  294. p += maxcol;
  295. }
  296. }
  297. }
  298. }
  299. /* Finally, release the console semaphore while we push
  300. * all the data to userspace from our temporary buffer.
  301. *
  302. * AKPM: Even though it's a semaphore, we should drop it because
  303. * the pagefault handling code may want to call printk().
  304. */
  305. console_unlock();
  306. ret = copy_to_user(buf, con_buf_start, orig_count);
  307. console_lock();
  308. if (ret) {
  309. read += (orig_count - ret);
  310. ret = -EFAULT;
  311. break;
  312. }
  313. buf += orig_count;
  314. pos += orig_count;
  315. read += orig_count;
  316. count -= orig_count;
  317. }
  318. *ppos += read;
  319. if (read)
  320. ret = read;
  321. unlock_out:
  322. console_unlock();
  323. free_page((unsigned long) con_buf);
  324. return ret;
  325. }
  326. static ssize_t
  327. vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  328. {
  329. struct inode *inode = file_inode(file);
  330. unsigned int currcons = iminor(inode);
  331. struct vc_data *vc;
  332. long pos;
  333. long attr, size, written;
  334. char *con_buf0;
  335. int col, maxcol, viewed;
  336. u16 *org0 = NULL, *org = NULL;
  337. size_t ret;
  338. char *con_buf;
  339. con_buf = (char *) __get_free_page(GFP_KERNEL);
  340. if (!con_buf)
  341. return -ENOMEM;
  342. pos = *ppos;
  343. /* Select the proper current console and verify
  344. * sanity of the situation under the console lock.
  345. */
  346. console_lock();
  347. attr = (currcons & 128);
  348. ret = -ENXIO;
  349. vc = vcs_vc(inode, &viewed);
  350. if (!vc)
  351. goto unlock_out;
  352. size = vcs_size(inode);
  353. ret = -EINVAL;
  354. if (pos < 0 || pos > size)
  355. goto unlock_out;
  356. if (count > size - pos)
  357. count = size - pos;
  358. written = 0;
  359. while (count) {
  360. long this_round = count;
  361. size_t orig_count;
  362. long p;
  363. if (this_round > CON_BUF_SIZE)
  364. this_round = CON_BUF_SIZE;
  365. /* Temporarily drop the console lock so that we can read
  366. * in the write data from userspace safely.
  367. */
  368. console_unlock();
  369. ret = copy_from_user(con_buf, buf, this_round);
  370. console_lock();
  371. if (ret) {
  372. this_round -= ret;
  373. if (!this_round) {
  374. /* Abort loop if no data were copied. Otherwise
  375. * fail with -EFAULT.
  376. */
  377. if (written)
  378. break;
  379. ret = -EFAULT;
  380. goto unlock_out;
  381. }
  382. }
  383. /* The vcs_size might have changed while we slept to grab
  384. * the user buffer, so recheck.
  385. * Return data written up to now on failure.
  386. */
  387. size = vcs_size(inode);
  388. if (size < 0) {
  389. if (written)
  390. break;
  391. ret = size;
  392. goto unlock_out;
  393. }
  394. if (pos >= size)
  395. break;
  396. if (this_round > size - pos)
  397. this_round = size - pos;
  398. /* OK, now actually push the write to the console
  399. * under the lock using the local kernel buffer.
  400. */
  401. con_buf0 = con_buf;
  402. orig_count = this_round;
  403. maxcol = vc->vc_cols;
  404. p = pos;
  405. if (!attr) {
  406. org0 = org = screen_pos(vc, p, viewed);
  407. col = p % maxcol;
  408. p += maxcol - col;
  409. while (this_round > 0) {
  410. unsigned char c = *con_buf0++;
  411. this_round--;
  412. vcs_scr_writew(vc,
  413. (vcs_scr_readw(vc, org) & 0xff00) | c, org);
  414. org++;
  415. if (++col == maxcol) {
  416. org = screen_pos(vc, p, viewed);
  417. col = 0;
  418. p += maxcol;
  419. }
  420. }
  421. } else {
  422. if (p < HEADER_SIZE) {
  423. char header[HEADER_SIZE];
  424. getconsxy(vc, header + 2);
  425. while (p < HEADER_SIZE && this_round > 0) {
  426. this_round--;
  427. header[p++] = *con_buf0++;
  428. }
  429. if (!viewed)
  430. putconsxy(vc, header + 2);
  431. }
  432. p -= HEADER_SIZE;
  433. col = (p/2) % maxcol;
  434. if (this_round > 0) {
  435. org0 = org = screen_pos(vc, p/2, viewed);
  436. if ((p & 1) && this_round > 0) {
  437. char c;
  438. this_round--;
  439. c = *con_buf0++;
  440. #ifdef __BIG_ENDIAN
  441. vcs_scr_writew(vc, c |
  442. (vcs_scr_readw(vc, org) & 0xff00), org);
  443. #else
  444. vcs_scr_writew(vc, (c << 8) |
  445. (vcs_scr_readw(vc, org) & 0xff), org);
  446. #endif
  447. org++;
  448. p++;
  449. if (++col == maxcol) {
  450. org = screen_pos(vc, p/2, viewed);
  451. col = 0;
  452. }
  453. }
  454. p /= 2;
  455. p += maxcol - col;
  456. }
  457. while (this_round > 1) {
  458. unsigned short w;
  459. w = get_unaligned(((unsigned short *)con_buf0));
  460. vcs_scr_writew(vc, w, org++);
  461. con_buf0 += 2;
  462. this_round -= 2;
  463. if (++col == maxcol) {
  464. org = screen_pos(vc, p, viewed);
  465. col = 0;
  466. p += maxcol;
  467. }
  468. }
  469. if (this_round > 0) {
  470. unsigned char c;
  471. c = *con_buf0++;
  472. #ifdef __BIG_ENDIAN
  473. vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org);
  474. #else
  475. vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org);
  476. #endif
  477. }
  478. }
  479. count -= orig_count;
  480. written += orig_count;
  481. buf += orig_count;
  482. pos += orig_count;
  483. if (org0)
  484. update_region(vc, (unsigned long)(org0), org - org0);
  485. }
  486. *ppos += written;
  487. ret = written;
  488. if (written)
  489. vcs_scr_updated(vc);
  490. unlock_out:
  491. console_unlock();
  492. free_page((unsigned long) con_buf);
  493. return ret;
  494. }
  495. static __poll_t
  496. vcs_poll(struct file *file, poll_table *wait)
  497. {
  498. struct vcs_poll_data *poll = vcs_poll_data_get(file);
  499. __poll_t ret = DEFAULT_POLLMASK|EPOLLERR|EPOLLPRI;
  500. if (poll) {
  501. poll_wait(file, &poll->waitq, wait);
  502. if (poll->seen_last_update)
  503. ret = DEFAULT_POLLMASK;
  504. }
  505. return ret;
  506. }
  507. static int
  508. vcs_fasync(int fd, struct file *file, int on)
  509. {
  510. struct vcs_poll_data *poll = file->private_data;
  511. if (!poll) {
  512. /* don't allocate anything if all we want is disable fasync */
  513. if (!on)
  514. return 0;
  515. poll = vcs_poll_data_get(file);
  516. if (!poll)
  517. return -ENOMEM;
  518. }
  519. return fasync_helper(fd, file, on, &poll->fasync);
  520. }
  521. static int
  522. vcs_open(struct inode *inode, struct file *filp)
  523. {
  524. unsigned int currcons = iminor(inode) & 127;
  525. int ret = 0;
  526. console_lock();
  527. if(currcons && !vc_cons_allocated(currcons-1))
  528. ret = -ENXIO;
  529. console_unlock();
  530. return ret;
  531. }
  532. static int vcs_release(struct inode *inode, struct file *file)
  533. {
  534. struct vcs_poll_data *poll = file->private_data;
  535. if (poll)
  536. vcs_poll_data_free(poll);
  537. return 0;
  538. }
  539. static const struct file_operations vcs_fops = {
  540. .llseek = vcs_lseek,
  541. .read = vcs_read,
  542. .write = vcs_write,
  543. .poll = vcs_poll,
  544. .fasync = vcs_fasync,
  545. .open = vcs_open,
  546. .release = vcs_release,
  547. };
  548. static struct class *vc_class;
  549. void vcs_make_sysfs(int index)
  550. {
  551. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL,
  552. "vcs%u", index + 1);
  553. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL,
  554. "vcsa%u", index + 1);
  555. }
  556. void vcs_remove_sysfs(int index)
  557. {
  558. device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 1));
  559. device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 129));
  560. }
  561. int __init vcs_init(void)
  562. {
  563. unsigned int i;
  564. if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
  565. panic("unable to get major %d for vcs device", VCS_MAJOR);
  566. vc_class = class_create(THIS_MODULE, "vc");
  567. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
  568. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");
  569. for (i = 0; i < MIN_NR_CONSOLES; i++)
  570. vcs_make_sysfs(i);
  571. return 0;
  572. }