core.c 9.8 KB

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