vc_screen.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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/config.h>
  24. #include <linux/kernel.h>
  25. #include <linux/major.h>
  26. #include <linux/errno.h>
  27. #include <linux/tty.h>
  28. #include <linux/devfs_fs_kernel.h>
  29. #include <linux/sched.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/mm.h>
  32. #include <linux/init.h>
  33. #include <linux/vt_kern.h>
  34. #include <linux/selection.h>
  35. #include <linux/kbd_kern.h>
  36. #include <linux/console.h>
  37. #include <linux/smp_lock.h>
  38. #include <linux/device.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. static int
  47. vcs_size(struct inode *inode)
  48. {
  49. int size;
  50. int minor = iminor(inode);
  51. int currcons = minor & 127;
  52. struct vc_data *vc;
  53. if (currcons == 0)
  54. currcons = fg_console;
  55. else
  56. currcons--;
  57. if (!vc_cons_allocated(currcons))
  58. return -ENXIO;
  59. vc = vc_cons[currcons].d;
  60. size = vc->vc_rows * vc->vc_cols;
  61. if (minor & 128)
  62. size = 2*size + HEADER_SIZE;
  63. return size;
  64. }
  65. static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
  66. {
  67. int size;
  68. down(&con_buf_sem);
  69. size = vcs_size(file->f_dentry->d_inode);
  70. switch (orig) {
  71. default:
  72. up(&con_buf_sem);
  73. return -EINVAL;
  74. case 2:
  75. offset += size;
  76. break;
  77. case 1:
  78. offset += file->f_pos;
  79. case 0:
  80. break;
  81. }
  82. if (offset < 0 || offset > size) {
  83. up(&con_buf_sem);
  84. return -EINVAL;
  85. }
  86. file->f_pos = offset;
  87. up(&con_buf_sem);
  88. return file->f_pos;
  89. }
  90. static ssize_t
  91. vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  92. {
  93. struct inode *inode = file->f_dentry->d_inode;
  94. unsigned int currcons = iminor(inode);
  95. struct vc_data *vc;
  96. long pos;
  97. long viewed, attr, read;
  98. int col, maxcol;
  99. unsigned short *org = NULL;
  100. ssize_t ret;
  101. down(&con_buf_sem);
  102. pos = *ppos;
  103. /* Select the proper current console and verify
  104. * sanity of the situation under the console lock.
  105. */
  106. acquire_console_sem();
  107. attr = (currcons & 128);
  108. currcons = (currcons & 127);
  109. if (currcons == 0) {
  110. currcons = fg_console;
  111. viewed = 1;
  112. } else {
  113. currcons--;
  114. viewed = 0;
  115. }
  116. ret = -ENXIO;
  117. if (!vc_cons_allocated(currcons))
  118. goto unlock_out;
  119. vc = vc_cons[currcons].d;
  120. ret = -EINVAL;
  121. if (pos < 0)
  122. goto unlock_out;
  123. read = 0;
  124. ret = 0;
  125. while (count) {
  126. char *con_buf0, *con_buf_start;
  127. long this_round, size;
  128. ssize_t orig_count;
  129. long p = pos;
  130. /* Check whether we are above size each round,
  131. * as copy_to_user at the end of this loop
  132. * could sleep.
  133. */
  134. size = vcs_size(inode);
  135. if (pos >= size)
  136. break;
  137. if (count > size - pos)
  138. count = size - pos;
  139. this_round = count;
  140. if (this_round > CON_BUF_SIZE)
  141. this_round = CON_BUF_SIZE;
  142. /* Perform the whole read into the local con_buf.
  143. * Then we can drop the console spinlock and safely
  144. * attempt to move it to userspace.
  145. */
  146. con_buf_start = con_buf0 = con_buf;
  147. orig_count = this_round;
  148. maxcol = vc->vc_cols;
  149. if (!attr) {
  150. org = screen_pos(vc, p, viewed);
  151. col = p % maxcol;
  152. p += maxcol - col;
  153. while (this_round-- > 0) {
  154. *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff);
  155. if (++col == maxcol) {
  156. org = screen_pos(vc, p, viewed);
  157. col = 0;
  158. p += maxcol;
  159. }
  160. }
  161. } else {
  162. if (p < HEADER_SIZE) {
  163. size_t tmp_count;
  164. con_buf0[0] = (char)vc->vc_rows;
  165. con_buf0[1] = (char)vc->vc_cols;
  166. getconsxy(vc, con_buf0 + 2);
  167. con_buf_start += p;
  168. this_round += p;
  169. if (this_round > CON_BUF_SIZE) {
  170. this_round = CON_BUF_SIZE;
  171. orig_count = this_round - p;
  172. }
  173. tmp_count = HEADER_SIZE;
  174. if (tmp_count > this_round)
  175. tmp_count = this_round;
  176. /* Advance state pointers and move on. */
  177. this_round -= tmp_count;
  178. p = HEADER_SIZE;
  179. con_buf0 = con_buf + HEADER_SIZE;
  180. /* If this_round >= 0, then p is even... */
  181. } else if (p & 1) {
  182. /* Skip first byte for output if start address is odd
  183. * Update region sizes up/down depending on free
  184. * space in buffer.
  185. */
  186. con_buf_start++;
  187. if (this_round < CON_BUF_SIZE)
  188. this_round++;
  189. else
  190. orig_count--;
  191. }
  192. if (this_round > 0) {
  193. unsigned short *tmp_buf = (unsigned short *)con_buf0;
  194. p -= HEADER_SIZE;
  195. p /= 2;
  196. col = p % maxcol;
  197. org = screen_pos(vc, p, viewed);
  198. p += maxcol - col;
  199. /* Buffer has even length, so we can always copy
  200. * character + attribute. We do not copy last byte
  201. * to userspace if this_round is odd.
  202. */
  203. this_round = (this_round + 1) >> 1;
  204. while (this_round) {
  205. *tmp_buf++ = vcs_scr_readw(vc, org++);
  206. this_round --;
  207. if (++col == maxcol) {
  208. org = screen_pos(vc, p, viewed);
  209. col = 0;
  210. p += maxcol;
  211. }
  212. }
  213. }
  214. }
  215. /* Finally, release the console semaphore while we push
  216. * all the data to userspace from our temporary buffer.
  217. *
  218. * AKPM: Even though it's a semaphore, we should drop it because
  219. * the pagefault handling code may want to call printk().
  220. */
  221. release_console_sem();
  222. ret = copy_to_user(buf, con_buf_start, orig_count);
  223. acquire_console_sem();
  224. if (ret) {
  225. read += (orig_count - ret);
  226. ret = -EFAULT;
  227. break;
  228. }
  229. buf += orig_count;
  230. pos += orig_count;
  231. read += orig_count;
  232. count -= orig_count;
  233. }
  234. *ppos += read;
  235. if (read)
  236. ret = read;
  237. unlock_out:
  238. release_console_sem();
  239. up(&con_buf_sem);
  240. return ret;
  241. }
  242. static ssize_t
  243. vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  244. {
  245. struct inode *inode = file->f_dentry->d_inode;
  246. unsigned int currcons = iminor(inode);
  247. struct vc_data *vc;
  248. long pos;
  249. long viewed, attr, size, written;
  250. char *con_buf0;
  251. int col, maxcol;
  252. u16 *org0 = NULL, *org = NULL;
  253. size_t ret;
  254. down(&con_buf_sem);
  255. pos = *ppos;
  256. /* Select the proper current console and verify
  257. * sanity of the situation under the console lock.
  258. */
  259. acquire_console_sem();
  260. attr = (currcons & 128);
  261. currcons = (currcons & 127);
  262. if (currcons == 0) {
  263. currcons = fg_console;
  264. viewed = 1;
  265. } else {
  266. currcons--;
  267. viewed = 0;
  268. }
  269. ret = -ENXIO;
  270. if (!vc_cons_allocated(currcons))
  271. goto unlock_out;
  272. vc = vc_cons[currcons].d;
  273. size = vcs_size(inode);
  274. ret = -EINVAL;
  275. if (pos < 0 || pos > size)
  276. goto unlock_out;
  277. if (count > size - pos)
  278. count = size - pos;
  279. written = 0;
  280. while (count) {
  281. long this_round = count;
  282. size_t orig_count;
  283. long p;
  284. if (this_round > CON_BUF_SIZE)
  285. this_round = CON_BUF_SIZE;
  286. /* Temporarily drop the console lock so that we can read
  287. * in the write data from userspace safely.
  288. */
  289. release_console_sem();
  290. ret = copy_from_user(con_buf, buf, this_round);
  291. acquire_console_sem();
  292. if (ret) {
  293. this_round -= ret;
  294. if (!this_round) {
  295. /* Abort loop if no data were copied. Otherwise
  296. * fail with -EFAULT.
  297. */
  298. if (written)
  299. break;
  300. ret = -EFAULT;
  301. goto unlock_out;
  302. }
  303. }
  304. /* The vcs_size might have changed while we slept to grab
  305. * the user buffer, so recheck.
  306. * Return data written up to now on failure.
  307. */
  308. size = vcs_size(inode);
  309. if (pos >= size)
  310. break;
  311. if (this_round > size - pos)
  312. this_round = size - pos;
  313. /* OK, now actually push the write to the console
  314. * under the lock using the local kernel buffer.
  315. */
  316. con_buf0 = con_buf;
  317. orig_count = this_round;
  318. maxcol = vc->vc_cols;
  319. p = pos;
  320. if (!attr) {
  321. org0 = org = screen_pos(vc, p, viewed);
  322. col = p % maxcol;
  323. p += maxcol - col;
  324. while (this_round > 0) {
  325. unsigned char c = *con_buf0++;
  326. this_round--;
  327. vcs_scr_writew(vc,
  328. (vcs_scr_readw(vc, org) & 0xff00) | c, org);
  329. org++;
  330. if (++col == maxcol) {
  331. org = screen_pos(vc, p, viewed);
  332. col = 0;
  333. p += maxcol;
  334. }
  335. }
  336. } else {
  337. if (p < HEADER_SIZE) {
  338. char header[HEADER_SIZE];
  339. getconsxy(vc, header + 2);
  340. while (p < HEADER_SIZE && this_round > 0) {
  341. this_round--;
  342. header[p++] = *con_buf0++;
  343. }
  344. if (!viewed)
  345. putconsxy(vc, header + 2);
  346. }
  347. p -= HEADER_SIZE;
  348. col = (p/2) % maxcol;
  349. if (this_round > 0) {
  350. org0 = org = screen_pos(vc, p/2, viewed);
  351. if ((p & 1) && this_round > 0) {
  352. char c;
  353. this_round--;
  354. c = *con_buf0++;
  355. #ifdef __BIG_ENDIAN
  356. vcs_scr_writew(vc, c |
  357. (vcs_scr_readw(vc, org) & 0xff00), org);
  358. #else
  359. vcs_scr_writew(vc, (c << 8) |
  360. (vcs_scr_readw(vc, org) & 0xff), org);
  361. #endif
  362. org++;
  363. p++;
  364. if (++col == maxcol) {
  365. org = screen_pos(vc, p/2, viewed);
  366. col = 0;
  367. }
  368. }
  369. p /= 2;
  370. p += maxcol - col;
  371. }
  372. while (this_round > 1) {
  373. unsigned short w;
  374. w = get_unaligned(((unsigned short *)con_buf0));
  375. vcs_scr_writew(vc, w, org++);
  376. con_buf0 += 2;
  377. this_round -= 2;
  378. if (++col == maxcol) {
  379. org = screen_pos(vc, p, viewed);
  380. col = 0;
  381. p += maxcol;
  382. }
  383. }
  384. if (this_round > 0) {
  385. unsigned char c;
  386. c = *con_buf0++;
  387. #ifdef __BIG_ENDIAN
  388. vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org);
  389. #else
  390. vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org);
  391. #endif
  392. }
  393. }
  394. count -= orig_count;
  395. written += orig_count;
  396. buf += orig_count;
  397. pos += orig_count;
  398. if (org0)
  399. update_region(vc, (unsigned long)(org0), org - org0);
  400. }
  401. *ppos += written;
  402. ret = written;
  403. unlock_out:
  404. release_console_sem();
  405. up(&con_buf_sem);
  406. return ret;
  407. }
  408. static int
  409. vcs_open(struct inode *inode, struct file *filp)
  410. {
  411. unsigned int currcons = iminor(inode) & 127;
  412. if(currcons && !vc_cons_allocated(currcons-1))
  413. return -ENXIO;
  414. return 0;
  415. }
  416. static struct file_operations vcs_fops = {
  417. .llseek = vcs_lseek,
  418. .read = vcs_read,
  419. .write = vcs_write,
  420. .open = vcs_open,
  421. };
  422. static struct class *vc_class;
  423. void vcs_make_devfs(struct tty_struct *tty)
  424. {
  425. devfs_mk_cdev(MKDEV(VCS_MAJOR, tty->index + 1),
  426. S_IFCHR|S_IRUSR|S_IWUSR,
  427. "vcc/%u", tty->index + 1);
  428. devfs_mk_cdev(MKDEV(VCS_MAJOR, tty->index + 129),
  429. S_IFCHR|S_IRUSR|S_IWUSR,
  430. "vcc/a%u", tty->index + 1);
  431. class_device_create(vc_class, NULL, MKDEV(VCS_MAJOR, tty->index + 1),
  432. NULL, "vcs%u", tty->index + 1);
  433. class_device_create(vc_class, NULL, MKDEV(VCS_MAJOR, tty->index + 129),
  434. NULL, "vcsa%u", tty->index + 1);
  435. }
  436. void vcs_remove_devfs(struct tty_struct *tty)
  437. {
  438. devfs_remove("vcc/%u", tty->index + 1);
  439. devfs_remove("vcc/a%u", tty->index + 1);
  440. class_device_destroy(vc_class, MKDEV(VCS_MAJOR, tty->index + 1));
  441. class_device_destroy(vc_class, MKDEV(VCS_MAJOR, tty->index + 129));
  442. }
  443. int __init vcs_init(void)
  444. {
  445. if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
  446. panic("unable to get major %d for vcs device", VCS_MAJOR);
  447. vc_class = class_create(THIS_MODULE, "vc");
  448. devfs_mk_cdev(MKDEV(VCS_MAJOR, 0), S_IFCHR|S_IRUSR|S_IWUSR, "vcc/0");
  449. devfs_mk_cdev(MKDEV(VCS_MAJOR, 128), S_IFCHR|S_IRUSR|S_IWUSR, "vcc/a0");
  450. class_device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
  451. class_device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");
  452. return 0;
  453. }