core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. ssize_t ret = 0;
  229. struct hwrng *rng;
  230. err = mutex_lock_interruptible(&rng_mutex);
  231. if (err)
  232. return -ERESTARTSYS;
  233. buf[0] = '\0';
  234. list_for_each_entry(rng, &rng_list, list) {
  235. strncat(buf, rng->name, PAGE_SIZE - ret - 1);
  236. ret += strlen(rng->name);
  237. strncat(buf, " ", PAGE_SIZE - ret - 1);
  238. ret++;
  239. }
  240. strncat(buf, "\n", PAGE_SIZE - ret - 1);
  241. ret++;
  242. mutex_unlock(&rng_mutex);
  243. return ret;
  244. }
  245. static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
  246. hwrng_attr_current_show,
  247. hwrng_attr_current_store);
  248. static DEVICE_ATTR(rng_available, S_IRUGO,
  249. hwrng_attr_available_show,
  250. NULL);
  251. static void unregister_miscdev(void)
  252. {
  253. device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available);
  254. device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
  255. misc_deregister(&rng_miscdev);
  256. }
  257. static int register_miscdev(void)
  258. {
  259. int err;
  260. err = misc_register(&rng_miscdev);
  261. if (err)
  262. goto out;
  263. err = device_create_file(rng_miscdev.this_device,
  264. &dev_attr_rng_current);
  265. if (err)
  266. goto err_misc_dereg;
  267. err = device_create_file(rng_miscdev.this_device,
  268. &dev_attr_rng_available);
  269. if (err)
  270. goto err_remove_current;
  271. out:
  272. return err;
  273. err_remove_current:
  274. device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
  275. err_misc_dereg:
  276. misc_deregister(&rng_miscdev);
  277. goto out;
  278. }
  279. static int hwrng_fillfn(void *unused)
  280. {
  281. long rc;
  282. while (!kthread_should_stop()) {
  283. if (!current_rng)
  284. break;
  285. rc = rng_get_data(current_rng, rng_fillbuf,
  286. rng_buffer_size(), 1);
  287. if (rc <= 0) {
  288. pr_warn("hwrng: no data available\n");
  289. msleep_interruptible(10000);
  290. continue;
  291. }
  292. add_hwgenerator_randomness((void *)rng_fillbuf, rc,
  293. rc * current_quality * 8 >> 10);
  294. }
  295. hwrng_fill = NULL;
  296. return 0;
  297. }
  298. static void start_khwrngd(void)
  299. {
  300. hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng");
  301. if (hwrng_fill == ERR_PTR(-ENOMEM)) {
  302. pr_err("hwrng_fill thread creation failed");
  303. hwrng_fill = NULL;
  304. }
  305. }
  306. int hwrng_register(struct hwrng *rng)
  307. {
  308. int err = -EINVAL;
  309. struct hwrng *old_rng, *tmp;
  310. if (rng->name == NULL ||
  311. (rng->data_read == NULL && rng->read == NULL))
  312. goto out;
  313. mutex_lock(&rng_mutex);
  314. /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
  315. err = -ENOMEM;
  316. if (!rng_buffer) {
  317. rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL);
  318. if (!rng_buffer)
  319. goto out_unlock;
  320. }
  321. if (!rng_fillbuf) {
  322. rng_fillbuf = kmalloc(rng_buffer_size(), GFP_KERNEL);
  323. if (!rng_fillbuf) {
  324. kfree(rng_buffer);
  325. goto out_unlock;
  326. }
  327. }
  328. /* Must not register two RNGs with the same name. */
  329. err = -EEXIST;
  330. list_for_each_entry(tmp, &rng_list, list) {
  331. if (strcmp(tmp->name, rng->name) == 0)
  332. goto out_unlock;
  333. }
  334. old_rng = current_rng;
  335. if (!old_rng) {
  336. err = hwrng_init(rng);
  337. if (err)
  338. goto out_unlock;
  339. current_rng = rng;
  340. }
  341. err = 0;
  342. if (!old_rng) {
  343. err = register_miscdev();
  344. if (err) {
  345. hwrng_cleanup(rng);
  346. current_rng = NULL;
  347. goto out_unlock;
  348. }
  349. }
  350. INIT_LIST_HEAD(&rng->list);
  351. list_add_tail(&rng->list, &rng_list);
  352. if (old_rng && !rng->init) {
  353. /*
  354. * Use a new device's input to add some randomness to
  355. * the system. If this rng device isn't going to be
  356. * used right away, its init function hasn't been
  357. * called yet; so only use the randomness from devices
  358. * that don't need an init callback.
  359. */
  360. add_early_randomness(rng);
  361. }
  362. out_unlock:
  363. mutex_unlock(&rng_mutex);
  364. out:
  365. return err;
  366. }
  367. EXPORT_SYMBOL_GPL(hwrng_register);
  368. void hwrng_unregister(struct hwrng *rng)
  369. {
  370. int err;
  371. mutex_lock(&rng_mutex);
  372. list_del(&rng->list);
  373. if (current_rng == rng) {
  374. hwrng_cleanup(rng);
  375. if (list_empty(&rng_list)) {
  376. current_rng = NULL;
  377. } else {
  378. current_rng = list_entry(rng_list.prev, struct hwrng, list);
  379. err = hwrng_init(current_rng);
  380. if (err)
  381. current_rng = NULL;
  382. }
  383. }
  384. if (list_empty(&rng_list)) {
  385. unregister_miscdev();
  386. if (hwrng_fill)
  387. kthread_stop(hwrng_fill);
  388. }
  389. mutex_unlock(&rng_mutex);
  390. }
  391. EXPORT_SYMBOL_GPL(hwrng_unregister);
  392. static void __exit hwrng_exit(void)
  393. {
  394. mutex_lock(&rng_mutex);
  395. BUG_ON(current_rng);
  396. kfree(rng_buffer);
  397. kfree(rng_fillbuf);
  398. mutex_unlock(&rng_mutex);
  399. }
  400. module_exit(hwrng_exit);
  401. MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
  402. MODULE_LICENSE("GPL");