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