vc_screen.c 14 KB

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