vc_screen.c 14 KB

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