vc_screen.c 11 KB

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