vc_screen.c 14 KB

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