random.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Copyright (C) 2005 - 2008 Jeff Dike <jdike@{linux.intel,addtoit}.com> */
  2. /* Much of this ripped from drivers/char/hw_random.c, see there for other
  3. * copyright.
  4. *
  5. * This software may be used and distributed according to the terms
  6. * of the GNU General Public License, incorporated herein by reference.
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/smp_lock.h>
  10. #include <linux/module.h>
  11. #include <linux/fs.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/miscdevice.h>
  14. #include <linux/delay.h>
  15. #include <asm/uaccess.h>
  16. #include "irq_kern.h"
  17. #include "os.h"
  18. /*
  19. * core module and version information
  20. */
  21. #define RNG_VERSION "1.0.0"
  22. #define RNG_MODULE_NAME "hw_random"
  23. #define RNG_MISCDEV_MINOR 183 /* official */
  24. /* Changed at init time, in the non-modular case, and at module load
  25. * time, in the module case. Presumably, the module subsystem
  26. * protects against a module being loaded twice at the same time.
  27. */
  28. static int random_fd = -1;
  29. static DECLARE_WAIT_QUEUE_HEAD(host_read_wait);
  30. static int rng_dev_open (struct inode *inode, struct file *filp)
  31. {
  32. cycle_kernel_lock();
  33. /* enforce read-only access to this chrdev */
  34. if ((filp->f_mode & FMODE_READ) == 0)
  35. return -EINVAL;
  36. if ((filp->f_mode & FMODE_WRITE) != 0)
  37. return -EINVAL;
  38. return 0;
  39. }
  40. static atomic_t host_sleep_count = ATOMIC_INIT(0);
  41. static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
  42. loff_t *offp)
  43. {
  44. u32 data;
  45. int n, ret = 0, have_data;
  46. while (size) {
  47. n = os_read_file(random_fd, &data, sizeof(data));
  48. if (n > 0) {
  49. have_data = n;
  50. while (have_data && size) {
  51. if (put_user((u8) data, buf++)) {
  52. ret = ret ? : -EFAULT;
  53. break;
  54. }
  55. size--;
  56. ret++;
  57. have_data--;
  58. data >>= 8;
  59. }
  60. }
  61. else if (n == -EAGAIN) {
  62. DECLARE_WAITQUEUE(wait, current);
  63. if (filp->f_flags & O_NONBLOCK)
  64. return ret ? : -EAGAIN;
  65. atomic_inc(&host_sleep_count);
  66. reactivate_fd(random_fd, RANDOM_IRQ);
  67. add_sigio_fd(random_fd);
  68. add_wait_queue(&host_read_wait, &wait);
  69. set_task_state(current, TASK_INTERRUPTIBLE);
  70. schedule();
  71. set_task_state(current, TASK_RUNNING);
  72. remove_wait_queue(&host_read_wait, &wait);
  73. if (atomic_dec_and_test(&host_sleep_count)) {
  74. ignore_sigio_fd(random_fd);
  75. deactivate_fd(random_fd, RANDOM_IRQ);
  76. }
  77. }
  78. else
  79. return n;
  80. if (signal_pending (current))
  81. return ret ? : -ERESTARTSYS;
  82. }
  83. return ret;
  84. }
  85. static const struct file_operations rng_chrdev_ops = {
  86. .owner = THIS_MODULE,
  87. .open = rng_dev_open,
  88. .read = rng_dev_read,
  89. };
  90. /* rng_init shouldn't be called more than once at boot time */
  91. static struct miscdevice rng_miscdev = {
  92. RNG_MISCDEV_MINOR,
  93. RNG_MODULE_NAME,
  94. &rng_chrdev_ops,
  95. };
  96. static irqreturn_t random_interrupt(int irq, void *data)
  97. {
  98. wake_up(&host_read_wait);
  99. return IRQ_HANDLED;
  100. }
  101. /*
  102. * rng_init - initialize RNG module
  103. */
  104. static int __init rng_init (void)
  105. {
  106. int err;
  107. err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0);
  108. if (err < 0)
  109. goto out;
  110. random_fd = err;
  111. err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt,
  112. IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "random",
  113. NULL);
  114. if (err)
  115. goto err_out_cleanup_hw;
  116. sigio_broken(random_fd, 1);
  117. err = misc_register (&rng_miscdev);
  118. if (err) {
  119. printk (KERN_ERR RNG_MODULE_NAME ": misc device register "
  120. "failed\n");
  121. goto err_out_cleanup_hw;
  122. }
  123. out:
  124. return err;
  125. err_out_cleanup_hw:
  126. os_close_file(random_fd);
  127. random_fd = -1;
  128. goto out;
  129. }
  130. /*
  131. * rng_cleanup - shutdown RNG module
  132. */
  133. static void __exit rng_cleanup (void)
  134. {
  135. os_close_file(random_fd);
  136. misc_deregister (&rng_miscdev);
  137. }
  138. module_init (rng_init);
  139. module_exit (rng_cleanup);
  140. MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver");
  141. MODULE_LICENSE("GPL");