vc_screen.c 16 KB

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