vc_screen.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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/sched.h>
  37. #include <linux/fs.h>
  38. #include <linux/poll.h>
  39. #include <linux/signal.h>
  40. #include <linux/slab.h>
  41. #include <linux/notifier.h>
  42. #include <asm/uaccess.h>
  43. #include <asm/byteorder.h>
  44. #include <asm/unaligned.h>
  45. #undef attr
  46. #undef org
  47. #undef addr
  48. #define HEADER_SIZE 4
  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;
  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->f_path.dentry->d_inode) & 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. vcs_poll_data_free(poll);
  113. poll = file->private_data;
  114. }
  115. spin_unlock(&file->f_lock);
  116. return poll;
  117. }
  118. static int
  119. vcs_size(struct inode *inode)
  120. {
  121. int size;
  122. int minor = iminor(inode);
  123. int currcons = minor & 127;
  124. struct vc_data *vc;
  125. if (currcons == 0)
  126. currcons = fg_console;
  127. else
  128. currcons--;
  129. if (!vc_cons_allocated(currcons))
  130. return -ENXIO;
  131. vc = vc_cons[currcons].d;
  132. size = vc->vc_rows * vc->vc_cols;
  133. if (minor & 128)
  134. size = 2*size + HEADER_SIZE;
  135. return size;
  136. }
  137. static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
  138. {
  139. int size;
  140. mutex_lock(&con_buf_mtx);
  141. size = vcs_size(file->f_path.dentry->d_inode);
  142. switch (orig) {
  143. default:
  144. mutex_unlock(&con_buf_mtx);
  145. return -EINVAL;
  146. case 2:
  147. offset += size;
  148. break;
  149. case 1:
  150. offset += file->f_pos;
  151. case 0:
  152. break;
  153. }
  154. if (offset < 0 || offset > size) {
  155. mutex_unlock(&con_buf_mtx);
  156. return -EINVAL;
  157. }
  158. file->f_pos = offset;
  159. mutex_unlock(&con_buf_mtx);
  160. return file->f_pos;
  161. }
  162. static ssize_t
  163. vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  164. {
  165. struct inode *inode = file->f_path.dentry->d_inode;
  166. unsigned int currcons = iminor(inode);
  167. struct vc_data *vc;
  168. struct vcs_poll_data *poll;
  169. long pos;
  170. long viewed, attr, read;
  171. int col, maxcol;
  172. unsigned short *org = NULL;
  173. ssize_t ret;
  174. mutex_lock(&con_buf_mtx);
  175. pos = *ppos;
  176. /* Select the proper current console and verify
  177. * sanity of the situation under the console lock.
  178. */
  179. console_lock();
  180. attr = (currcons & 128);
  181. currcons = (currcons & 127);
  182. if (currcons == 0) {
  183. currcons = fg_console;
  184. viewed = 1;
  185. } else {
  186. currcons--;
  187. viewed = 0;
  188. }
  189. ret = -ENXIO;
  190. if (!vc_cons_allocated(currcons))
  191. goto unlock_out;
  192. vc = vc_cons[currcons].d;
  193. ret = -EINVAL;
  194. if (pos < 0)
  195. goto unlock_out;
  196. poll = file->private_data;
  197. if (count && poll)
  198. poll->seen_last_update = true;
  199. read = 0;
  200. ret = 0;
  201. while (count) {
  202. char *con_buf0, *con_buf_start;
  203. long this_round, size;
  204. ssize_t orig_count;
  205. long p = pos;
  206. /* Check whether we are above size each round,
  207. * as copy_to_user at the end of this loop
  208. * could sleep.
  209. */
  210. size = vcs_size(inode);
  211. if (pos >= size)
  212. break;
  213. if (count > size - pos)
  214. count = size - pos;
  215. this_round = count;
  216. if (this_round > CON_BUF_SIZE)
  217. this_round = CON_BUF_SIZE;
  218. /* Perform the whole read into the local con_buf.
  219. * Then we can drop the console spinlock and safely
  220. * attempt to move it to userspace.
  221. */
  222. con_buf_start = con_buf0 = con_buf;
  223. orig_count = this_round;
  224. maxcol = vc->vc_cols;
  225. if (!attr) {
  226. org = screen_pos(vc, p, viewed);
  227. col = p % maxcol;
  228. p += maxcol - col;
  229. while (this_round-- > 0) {
  230. *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff);
  231. if (++col == maxcol) {
  232. org = screen_pos(vc, p, viewed);
  233. col = 0;
  234. p += maxcol;
  235. }
  236. }
  237. } else {
  238. if (p < HEADER_SIZE) {
  239. size_t tmp_count;
  240. con_buf0[0] = (char)vc->vc_rows;
  241. con_buf0[1] = (char)vc->vc_cols;
  242. getconsxy(vc, con_buf0 + 2);
  243. con_buf_start += p;
  244. this_round += p;
  245. if (this_round > CON_BUF_SIZE) {
  246. this_round = CON_BUF_SIZE;
  247. orig_count = this_round - p;
  248. }
  249. tmp_count = HEADER_SIZE;
  250. if (tmp_count > this_round)
  251. tmp_count = this_round;
  252. /* Advance state pointers and move on. */
  253. this_round -= tmp_count;
  254. p = HEADER_SIZE;
  255. con_buf0 = con_buf + HEADER_SIZE;
  256. /* If this_round >= 0, then p is even... */
  257. } else if (p & 1) {
  258. /* Skip first byte for output if start address is odd
  259. * Update region sizes up/down depending on free
  260. * space in buffer.
  261. */
  262. con_buf_start++;
  263. if (this_round < CON_BUF_SIZE)
  264. this_round++;
  265. else
  266. orig_count--;
  267. }
  268. if (this_round > 0) {
  269. unsigned short *tmp_buf = (unsigned short *)con_buf0;
  270. p -= HEADER_SIZE;
  271. p /= 2;
  272. col = p % maxcol;
  273. org = screen_pos(vc, p, viewed);
  274. p += maxcol - col;
  275. /* Buffer has even length, so we can always copy
  276. * character + attribute. We do not copy last byte
  277. * to userspace if this_round is odd.
  278. */
  279. this_round = (this_round + 1) >> 1;
  280. while (this_round) {
  281. *tmp_buf++ = vcs_scr_readw(vc, org++);
  282. this_round --;
  283. if (++col == maxcol) {
  284. org = screen_pos(vc, p, viewed);
  285. col = 0;
  286. p += maxcol;
  287. }
  288. }
  289. }
  290. }
  291. /* Finally, release the console semaphore while we push
  292. * all the data to userspace from our temporary buffer.
  293. *
  294. * AKPM: Even though it's a semaphore, we should drop it because
  295. * the pagefault handling code may want to call printk().
  296. */
  297. console_unlock();
  298. ret = copy_to_user(buf, con_buf_start, orig_count);
  299. console_lock();
  300. if (ret) {
  301. read += (orig_count - ret);
  302. ret = -EFAULT;
  303. break;
  304. }
  305. buf += orig_count;
  306. pos += orig_count;
  307. read += orig_count;
  308. count -= orig_count;
  309. }
  310. *ppos += read;
  311. if (read)
  312. ret = read;
  313. unlock_out:
  314. console_unlock();
  315. mutex_unlock(&con_buf_mtx);
  316. return ret;
  317. }
  318. static ssize_t
  319. vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  320. {
  321. struct inode *inode = file->f_path.dentry->d_inode;
  322. unsigned int currcons = iminor(inode);
  323. struct vc_data *vc;
  324. long pos;
  325. long viewed, attr, size, written;
  326. char *con_buf0;
  327. int col, maxcol;
  328. u16 *org0 = NULL, *org = NULL;
  329. size_t ret;
  330. mutex_lock(&con_buf_mtx);
  331. pos = *ppos;
  332. /* Select the proper current console and verify
  333. * sanity of the situation under the console lock.
  334. */
  335. console_lock();
  336. attr = (currcons & 128);
  337. currcons = (currcons & 127);
  338. if (currcons == 0) {
  339. currcons = fg_console;
  340. viewed = 1;
  341. } else {
  342. currcons--;
  343. viewed = 0;
  344. }
  345. ret = -ENXIO;
  346. if (!vc_cons_allocated(currcons))
  347. goto unlock_out;
  348. vc = vc_cons[currcons].d;
  349. size = vcs_size(inode);
  350. ret = -EINVAL;
  351. if (pos < 0 || pos > size)
  352. goto unlock_out;
  353. if (count > size - pos)
  354. count = size - pos;
  355. written = 0;
  356. while (count) {
  357. long this_round = count;
  358. size_t orig_count;
  359. long p;
  360. if (this_round > CON_BUF_SIZE)
  361. this_round = CON_BUF_SIZE;
  362. /* Temporarily drop the console lock so that we can read
  363. * in the write data from userspace safely.
  364. */
  365. console_unlock();
  366. ret = copy_from_user(con_buf, buf, this_round);
  367. console_lock();
  368. if (ret) {
  369. this_round -= ret;
  370. if (!this_round) {
  371. /* Abort loop if no data were copied. Otherwise
  372. * fail with -EFAULT.
  373. */
  374. if (written)
  375. break;
  376. ret = -EFAULT;
  377. goto unlock_out;
  378. }
  379. }
  380. /* The vcs_size might have changed while we slept to grab
  381. * the user buffer, so recheck.
  382. * Return data written up to now on failure.
  383. */
  384. size = vcs_size(inode);
  385. if (pos >= size)
  386. break;
  387. if (this_round > size - pos)
  388. this_round = size - pos;
  389. /* OK, now actually push the write to the console
  390. * under the lock using the local kernel buffer.
  391. */
  392. con_buf0 = con_buf;
  393. orig_count = this_round;
  394. maxcol = vc->vc_cols;
  395. p = pos;
  396. if (!attr) {
  397. org0 = org = screen_pos(vc, p, viewed);
  398. col = p % maxcol;
  399. p += maxcol - col;
  400. while (this_round > 0) {
  401. unsigned char c = *con_buf0++;
  402. this_round--;
  403. vcs_scr_writew(vc,
  404. (vcs_scr_readw(vc, org) & 0xff00) | c, org);
  405. org++;
  406. if (++col == maxcol) {
  407. org = screen_pos(vc, p, viewed);
  408. col = 0;
  409. p += maxcol;
  410. }
  411. }
  412. } else {
  413. if (p < HEADER_SIZE) {
  414. char header[HEADER_SIZE];
  415. getconsxy(vc, header + 2);
  416. while (p < HEADER_SIZE && this_round > 0) {
  417. this_round--;
  418. header[p++] = *con_buf0++;
  419. }
  420. if (!viewed)
  421. putconsxy(vc, header + 2);
  422. }
  423. p -= HEADER_SIZE;
  424. col = (p/2) % maxcol;
  425. if (this_round > 0) {
  426. org0 = org = screen_pos(vc, p/2, viewed);
  427. if ((p & 1) && this_round > 0) {
  428. char c;
  429. this_round--;
  430. c = *con_buf0++;
  431. #ifdef __BIG_ENDIAN
  432. vcs_scr_writew(vc, c |
  433. (vcs_scr_readw(vc, org) & 0xff00), org);
  434. #else
  435. vcs_scr_writew(vc, (c << 8) |
  436. (vcs_scr_readw(vc, org) & 0xff), org);
  437. #endif
  438. org++;
  439. p++;
  440. if (++col == maxcol) {
  441. org = screen_pos(vc, p/2, viewed);
  442. col = 0;
  443. }
  444. }
  445. p /= 2;
  446. p += maxcol - col;
  447. }
  448. while (this_round > 1) {
  449. unsigned short w;
  450. w = get_unaligned(((unsigned short *)con_buf0));
  451. vcs_scr_writew(vc, w, org++);
  452. con_buf0 += 2;
  453. this_round -= 2;
  454. if (++col == maxcol) {
  455. org = screen_pos(vc, p, viewed);
  456. col = 0;
  457. p += maxcol;
  458. }
  459. }
  460. if (this_round > 0) {
  461. unsigned char c;
  462. c = *con_buf0++;
  463. #ifdef __BIG_ENDIAN
  464. vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org);
  465. #else
  466. vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org);
  467. #endif
  468. }
  469. }
  470. count -= orig_count;
  471. written += orig_count;
  472. buf += orig_count;
  473. pos += orig_count;
  474. if (org0)
  475. update_region(vc, (unsigned long)(org0), org - org0);
  476. }
  477. *ppos += written;
  478. ret = written;
  479. if (written)
  480. vcs_scr_updated(vc);
  481. unlock_out:
  482. console_unlock();
  483. mutex_unlock(&con_buf_mtx);
  484. return ret;
  485. }
  486. static unsigned int
  487. vcs_poll(struct file *file, poll_table *wait)
  488. {
  489. struct vcs_poll_data *poll = vcs_poll_data_get(file);
  490. int ret = DEFAULT_POLLMASK|POLLERR|POLLPRI;
  491. if (poll) {
  492. poll_wait(file, &poll->waitq, wait);
  493. if (poll->seen_last_update)
  494. ret = DEFAULT_POLLMASK;
  495. }
  496. return ret;
  497. }
  498. static int
  499. vcs_fasync(int fd, struct file *file, int on)
  500. {
  501. struct vcs_poll_data *poll = file->private_data;
  502. if (!poll) {
  503. /* don't allocate anything if all we want is disable fasync */
  504. if (!on)
  505. return 0;
  506. poll = vcs_poll_data_get(file);
  507. if (!poll)
  508. return -ENOMEM;
  509. }
  510. return fasync_helper(fd, file, on, &poll->fasync);
  511. }
  512. static int
  513. vcs_open(struct inode *inode, struct file *filp)
  514. {
  515. unsigned int currcons = iminor(inode) & 127;
  516. int ret = 0;
  517. tty_lock();
  518. if(currcons && !vc_cons_allocated(currcons-1))
  519. ret = -ENXIO;
  520. tty_unlock();
  521. return ret;
  522. }
  523. static int vcs_release(struct inode *inode, struct file *file)
  524. {
  525. struct vcs_poll_data *poll = file->private_data;
  526. if (poll)
  527. vcs_poll_data_free(poll);
  528. return 0;
  529. }
  530. static const struct file_operations vcs_fops = {
  531. .llseek = vcs_lseek,
  532. .read = vcs_read,
  533. .write = vcs_write,
  534. .poll = vcs_poll,
  535. .fasync = vcs_fasync,
  536. .open = vcs_open,
  537. .release = vcs_release,
  538. };
  539. static struct class *vc_class;
  540. void vcs_make_sysfs(int index)
  541. {
  542. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL,
  543. "vcs%u", index + 1);
  544. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL,
  545. "vcsa%u", index + 1);
  546. }
  547. void vcs_remove_sysfs(int index)
  548. {
  549. device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 1));
  550. device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 129));
  551. }
  552. int __init vcs_init(void)
  553. {
  554. unsigned int i;
  555. if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
  556. panic("unable to get major %d for vcs device", VCS_MAJOR);
  557. vc_class = class_create(THIS_MODULE, "vc");
  558. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
  559. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");
  560. for (i = 0; i < MIN_NR_CONSOLES; i++)
  561. vcs_make_sysfs(i);
  562. return 0;
  563. }