vc_screen.c 14 KB

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