vc_screen.c 11 KB

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