core.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. Added support for the AMD Geode LX RNG
  3. (c) Copyright 2004-2005 Advanced Micro Devices, Inc.
  4. derived from
  5. Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
  6. (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
  7. derived from
  8. Hardware driver for the AMD 768 Random Number Generator (RNG)
  9. (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
  10. derived from
  11. Hardware driver for Intel i810 Random Number Generator (RNG)
  12. Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
  13. Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
  14. Added generic RNG API
  15. Copyright 2006 Michael Buesch <m@bues.ch>
  16. Copyright 2005 (c) MontaVista Software, Inc.
  17. Please read Documentation/hw_random.txt for details on use.
  18. ----------------------------------------------------------
  19. This software may be used and distributed according to the terms
  20. of the GNU General Public License, incorporated herein by reference.
  21. */
  22. #include <linux/device.h>
  23. #include <linux/hw_random.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/fs.h>
  27. #include <linux/sched.h>
  28. #include <linux/miscdevice.h>
  29. #include <linux/kthread.h>
  30. #include <linux/delay.h>
  31. #include <linux/slab.h>
  32. #include <linux/random.h>
  33. #include <asm/uaccess.h>
  34. #define RNG_MODULE_NAME "hw_random"
  35. #define PFX RNG_MODULE_NAME ": "
  36. #define RNG_MISCDEV_MINOR 183 /* official */
  37. static struct hwrng *current_rng;
  38. static struct task_struct *hwrng_fill;
  39. static LIST_HEAD(rng_list);
  40. static DEFINE_MUTEX(rng_mutex);
  41. static int data_avail;
  42. static u8 *rng_buffer, *rng_fillbuf;
  43. static unsigned short current_quality;
  44. static unsigned short default_quality; /* = 0; default to "off" */
  45. module_param(current_quality, ushort, 0644);
  46. MODULE_PARM_DESC(current_quality,
  47. "current hwrng entropy estimation per mill");
  48. module_param(default_quality, ushort, 0644);
  49. MODULE_PARM_DESC(default_quality,
  50. "default entropy content of hwrng per mill");
  51. static void start_khwrngd(void);
  52. static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
  53. int wait);
  54. static size_t rng_buffer_size(void)
  55. {
  56. return SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES;
  57. }
  58. static void add_early_randomness(struct hwrng *rng)
  59. {
  60. unsigned char bytes[16];
  61. int bytes_read;
  62. bytes_read = rng_get_data(rng, bytes, sizeof(bytes), 1);
  63. if (bytes_read > 0)
  64. add_device_randomness(bytes, bytes_read);
  65. }
  66. static inline int hwrng_init(struct hwrng *rng)
  67. {
  68. if (rng->init) {
  69. int ret;
  70. ret = rng->init(rng);
  71. if (ret)
  72. return ret;
  73. }
  74. add_early_randomness(rng);
  75. current_quality = rng->quality ? : default_quality;
  76. current_quality &= 1023;
  77. if (current_quality == 0 && hwrng_fill)
  78. kthread_stop(hwrng_fill);
  79. if (current_quality > 0 && !hwrng_fill)
  80. start_khwrngd();
  81. return 0;
  82. }
  83. static inline void hwrng_cleanup(struct hwrng *rng)
  84. {
  85. if (rng && rng->cleanup)
  86. rng->cleanup(rng);
  87. }
  88. static int rng_dev_open(struct inode *inode, struct file *filp)
  89. {
  90. /* enforce read-only access to this chrdev */
  91. if ((filp->f_mode & FMODE_READ) == 0)
  92. return -EINVAL;
  93. if (filp->f_mode & FMODE_WRITE)
  94. return -EINVAL;
  95. return 0;
  96. }
  97. static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
  98. int wait) {
  99. int present;
  100. if (rng->read)
  101. return rng->read(rng, (void *)buffer, size, wait);
  102. if (rng->data_present)
  103. present = rng->data_present(rng, wait);
  104. else
  105. present = 1;
  106. if (present)
  107. return rng->data_read(rng, (u32 *)buffer);
  108. return 0;
  109. }
  110. static ssize_t rng_dev_read(struct file *filp, char __user *buf,
  111. size_t size, loff_t *offp)
  112. {
  113. ssize_t ret = 0;
  114. int err = 0;
  115. int bytes_read, len;
  116. while (size) {
  117. if (mutex_lock_interruptible(&rng_mutex)) {
  118. err = -ERESTARTSYS;
  119. goto out;
  120. }
  121. if (!current_rng) {
  122. err = -ENODEV;
  123. goto out_unlock;
  124. }
  125. if (!data_avail) {
  126. bytes_read = rng_get_data(current_rng, rng_buffer,
  127. rng_buffer_size(),
  128. !(filp->f_flags & O_NONBLOCK));
  129. if (bytes_read < 0) {
  130. err = bytes_read;
  131. goto out_unlock;
  132. }
  133. data_avail = bytes_read;
  134. }
  135. if (!data_avail) {
  136. if (filp->f_flags & O_NONBLOCK) {
  137. err = -EAGAIN;
  138. goto out_unlock;
  139. }
  140. } else {
  141. len = data_avail;
  142. if (len > size)
  143. len = size;
  144. data_avail -= len;
  145. if (copy_to_user(buf + ret, rng_buffer + data_avail,
  146. len)) {
  147. err = -EFAULT;
  148. goto out_unlock;
  149. }
  150. size -= len;
  151. ret += len;
  152. }
  153. mutex_unlock(&rng_mutex);
  154. if (need_resched())
  155. schedule_timeout_interruptible(1);
  156. if (signal_pending(current)) {
  157. err = -ERESTARTSYS;
  158. goto out;
  159. }
  160. }
  161. out:
  162. return ret ? : err;
  163. out_unlock:
  164. mutex_unlock(&rng_mutex);
  165. goto out;
  166. }
  167. static const struct file_operations rng_chrdev_ops = {
  168. .owner = THIS_MODULE,
  169. .open = rng_dev_open,
  170. .read = rng_dev_read,
  171. .llseek = noop_llseek,
  172. };
  173. static struct miscdevice rng_miscdev = {
  174. .minor = RNG_MISCDEV_MINOR,
  175. .name = RNG_MODULE_NAME,
  176. .nodename = "hwrng",
  177. .fops = &rng_chrdev_ops,
  178. };
  179. static ssize_t hwrng_attr_current_store(struct device *dev,
  180. struct device_attribute *attr,
  181. const char *buf, size_t len)
  182. {
  183. int err;
  184. struct hwrng *rng;
  185. err = mutex_lock_interruptible(&rng_mutex);
  186. if (err)
  187. return -ERESTARTSYS;
  188. err = -ENODEV;
  189. list_for_each_entry(rng, &rng_list, list) {
  190. if (strcmp(rng->name, buf) == 0) {
  191. if (rng == current_rng) {
  192. err = 0;
  193. break;
  194. }
  195. err = hwrng_init(rng);
  196. if (err)
  197. break;
  198. hwrng_cleanup(current_rng);
  199. current_rng = rng;
  200. err = 0;
  201. break;
  202. }
  203. }
  204. mutex_unlock(&rng_mutex);
  205. return err ? : len;
  206. }
  207. static ssize_t hwrng_attr_current_show(struct device *dev,
  208. struct device_attribute *attr,
  209. char *buf)
  210. {
  211. int err;
  212. ssize_t ret;
  213. const char *name = "none";
  214. err = mutex_lock_interruptible(&rng_mutex);
  215. if (err)
  216. return -ERESTARTSYS;
  217. if (current_rng)
  218. name = current_rng->name;
  219. ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
  220. mutex_unlock(&rng_mutex);
  221. return ret;
  222. }
  223. static ssize_t hwrng_attr_available_show(struct device *dev,
  224. struct device_attribute *attr,
  225. char *buf)
  226. {
  227. int err;
  228. struct hwrng *rng;
  229. err = mutex_lock_interruptible(&rng_mutex);
  230. if (err)
  231. return -ERESTARTSYS;
  232. buf[0] = '\0';
  233. list_for_each_entry(rng, &rng_list, list) {
  234. strlcat(buf, rng->name, PAGE_SIZE);
  235. strlcat(buf, " ", PAGE_SIZE);
  236. }
  237. strlcat(buf, "\n", PAGE_SIZE);
  238. mutex_unlock(&rng_mutex);
  239. return strlen(buf);
  240. }
  241. static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
  242. hwrng_attr_current_show,
  243. hwrng_attr_current_store);
  244. static DEVICE_ATTR(rng_available, S_IRUGO,
  245. hwrng_attr_available_show,
  246. NULL);
  247. static void unregister_miscdev(void)
  248. {
  249. device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available);
  250. device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
  251. misc_deregister(&rng_miscdev);
  252. }
  253. static int register_miscdev(void)
  254. {
  255. int err;
  256. err = misc_register(&rng_miscdev);
  257. if (err)
  258. goto out;
  259. err = device_create_file(rng_miscdev.this_device,
  260. &dev_attr_rng_current);
  261. if (err)
  262. goto err_misc_dereg;
  263. err = device_create_file(rng_miscdev.this_device,
  264. &dev_attr_rng_available);
  265. if (err)
  266. goto err_remove_current;
  267. out:
  268. return err;
  269. err_remove_current:
  270. device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
  271. err_misc_dereg:
  272. misc_deregister(&rng_miscdev);
  273. goto out;
  274. }
  275. static int hwrng_fillfn(void *unused)
  276. {
  277. long rc;
  278. while (!kthread_should_stop()) {
  279. if (!current_rng)
  280. break;
  281. rc = rng_get_data(current_rng, rng_fillbuf,
  282. rng_buffer_size(), 1);
  283. if (rc <= 0) {
  284. pr_warn("hwrng: no data available\n");
  285. msleep_interruptible(10000);
  286. continue;
  287. }
  288. add_hwgenerator_randomness((void *)rng_fillbuf, rc,
  289. rc * current_quality * 8 >> 10);
  290. }
  291. hwrng_fill = NULL;
  292. return 0;
  293. }
  294. static void start_khwrngd(void)
  295. {
  296. hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng");
  297. if (hwrng_fill == ERR_PTR(-ENOMEM)) {
  298. pr_err("hwrng_fill thread creation failed");
  299. hwrng_fill = NULL;
  300. }
  301. }
  302. int hwrng_register(struct hwrng *rng)
  303. {
  304. int err = -EINVAL;
  305. struct hwrng *old_rng, *tmp;
  306. if (rng->name == NULL ||
  307. (rng->data_read == NULL && rng->read == NULL))
  308. goto out;
  309. mutex_lock(&rng_mutex);
  310. /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
  311. err = -ENOMEM;
  312. if (!rng_buffer) {
  313. rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL);
  314. if (!rng_buffer)
  315. goto out_unlock;
  316. }
  317. if (!rng_fillbuf) {
  318. rng_fillbuf = kmalloc(rng_buffer_size(), GFP_KERNEL);
  319. if (!rng_fillbuf) {
  320. kfree(rng_buffer);
  321. goto out_unlock;
  322. }
  323. }
  324. /* Must not register two RNGs with the same name. */
  325. err = -EEXIST;
  326. list_for_each_entry(tmp, &rng_list, list) {
  327. if (strcmp(tmp->name, rng->name) == 0)
  328. goto out_unlock;
  329. }
  330. old_rng = current_rng;
  331. if (!old_rng) {
  332. err = hwrng_init(rng);
  333. if (err)
  334. goto out_unlock;
  335. current_rng = rng;
  336. }
  337. err = 0;
  338. if (!old_rng) {
  339. err = register_miscdev();
  340. if (err) {
  341. hwrng_cleanup(rng);
  342. current_rng = NULL;
  343. goto out_unlock;
  344. }
  345. }
  346. INIT_LIST_HEAD(&rng->list);
  347. list_add_tail(&rng->list, &rng_list);
  348. if (old_rng && !rng->init) {
  349. /*
  350. * Use a new device's input to add some randomness to
  351. * the system. If this rng device isn't going to be
  352. * used right away, its init function hasn't been
  353. * called yet; so only use the randomness from devices
  354. * that don't need an init callback.
  355. */
  356. add_early_randomness(rng);
  357. }
  358. out_unlock:
  359. mutex_unlock(&rng_mutex);
  360. out:
  361. return err;
  362. }
  363. EXPORT_SYMBOL_GPL(hwrng_register);
  364. void hwrng_unregister(struct hwrng *rng)
  365. {
  366. int err;
  367. mutex_lock(&rng_mutex);
  368. list_del(&rng->list);
  369. if (current_rng == rng) {
  370. hwrng_cleanup(rng);
  371. if (list_empty(&rng_list)) {
  372. current_rng = NULL;
  373. } else {
  374. current_rng = list_entry(rng_list.prev, struct hwrng, list);
  375. err = hwrng_init(current_rng);
  376. if (err)
  377. current_rng = NULL;
  378. }
  379. }
  380. if (list_empty(&rng_list)) {
  381. unregister_miscdev();
  382. if (hwrng_fill)
  383. kthread_stop(hwrng_fill);
  384. }
  385. mutex_unlock(&rng_mutex);
  386. }
  387. EXPORT_SYMBOL_GPL(hwrng_unregister);
  388. static void __exit hwrng_exit(void)
  389. {
  390. mutex_lock(&rng_mutex);
  391. BUG_ON(current_rng);
  392. kfree(rng_buffer);
  393. kfree(rng_fillbuf);
  394. mutex_unlock(&rng_mutex);
  395. }
  396. module_exit(hwrng_exit);
  397. MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
  398. MODULE_LICENSE("GPL");