vc_screen.c 14 KB

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