vc_screen.c 14 KB

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