1 /* 2 Added support for the AMD Geode LX RNG 3 (c) Copyright 2004-2005 Advanced Micro Devices, Inc. 4 5 derived from 6 7 Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG) 8 (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com> 9 10 derived from 11 12 Hardware driver for the AMD 768 Random Number Generator (RNG) 13 (c) Copyright 2001 Red Hat Inc <alan@redhat.com> 14 15 derived from 16 17 Hardware driver for Intel i810 Random Number Generator (RNG) 18 Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com> 19 Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com> 20 21 Added generic RNG API 22 Copyright 2006 Michael Buesch <m@bues.ch> 23 Copyright 2005 (c) MontaVista Software, Inc. 24 25 Please read Documentation/hw_random.txt for details on use. 26 27 ---------------------------------------------------------- 28 This software may be used and distributed according to the terms 29 of the GNU General Public License, incorporated herein by reference. 30 31 */ 32 33 34 #include <linux/device.h> 35 #include <linux/hw_random.h> 36 #include <linux/module.h> 37 #include <linux/kernel.h> 38 #include <linux/fs.h> 39 #include <linux/sched.h> 40 #include <linux/miscdevice.h> 41 #include <linux/kthread.h> 42 #include <linux/delay.h> 43 #include <linux/slab.h> 44 #include <linux/random.h> 45 #include <linux/err.h> 46 #include <asm/uaccess.h> 47 48 49 #define RNG_MODULE_NAME "hw_random" 50 #define PFX RNG_MODULE_NAME ": " 51 #define RNG_MISCDEV_MINOR 183 /* official */ 52 53 54 static struct hwrng *current_rng; 55 static struct task_struct *hwrng_fill; 56 static LIST_HEAD(rng_list); 57 /* Protects rng_list and current_rng */ 58 static DEFINE_MUTEX(rng_mutex); 59 /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */ 60 static DEFINE_MUTEX(reading_mutex); 61 static int data_avail; 62 static u8 *rng_buffer, *rng_fillbuf; 63 static unsigned short current_quality; 64 static unsigned short default_quality; /* = 0; default to "off" */ 65 66 module_param(current_quality, ushort, 0644); 67 MODULE_PARM_DESC(current_quality, 68 "current hwrng entropy estimation per mill"); 69 module_param(default_quality, ushort, 0644); 70 MODULE_PARM_DESC(default_quality, 71 "default entropy content of hwrng per mill"); 72 73 static void drop_current_rng(void); 74 static int hwrng_init(struct hwrng *rng); 75 static void start_khwrngd(void); 76 77 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size, 78 int wait); 79 80 static size_t rng_buffer_size(void) 81 { 82 return SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES; 83 } 84 85 static void add_early_randomness(struct hwrng *rng) 86 { 87 unsigned char bytes[16]; 88 int bytes_read; 89 90 mutex_lock(&reading_mutex); 91 bytes_read = rng_get_data(rng, bytes, sizeof(bytes), 1); 92 mutex_unlock(&reading_mutex); 93 if (bytes_read > 0) 94 add_device_randomness(bytes, bytes_read); 95 } 96 97 static inline void cleanup_rng(struct kref *kref) 98 { 99 struct hwrng *rng = container_of(kref, struct hwrng, ref); 100 101 if (rng->cleanup) 102 rng->cleanup(rng); 103 104 complete(&rng->cleanup_done); 105 } 106 107 static int set_current_rng(struct hwrng *rng) 108 { 109 int err; 110 111 BUG_ON(!mutex_is_locked(&rng_mutex)); 112 113 err = hwrng_init(rng); 114 if (err) 115 return err; 116 117 drop_current_rng(); 118 current_rng = rng; 119 120 return 0; 121 } 122 123 static void drop_current_rng(void) 124 { 125 BUG_ON(!mutex_is_locked(&rng_mutex)); 126 if (!current_rng) 127 return; 128 129 /* decrease last reference for triggering the cleanup */ 130 kref_put(¤t_rng->ref, cleanup_rng); 131 current_rng = NULL; 132 } 133 134 /* Returns ERR_PTR(), NULL or refcounted hwrng */ 135 static struct hwrng *get_current_rng(void) 136 { 137 struct hwrng *rng; 138 139 if (mutex_lock_interruptible(&rng_mutex)) 140 return ERR_PTR(-ERESTARTSYS); 141 142 rng = current_rng; 143 if (rng) 144 kref_get(&rng->ref); 145 146 mutex_unlock(&rng_mutex); 147 return rng; 148 } 149 150 static void put_rng(struct hwrng *rng) 151 { 152 /* 153 * Hold rng_mutex here so we serialize in case they set_current_rng 154 * on rng again immediately. 155 */ 156 mutex_lock(&rng_mutex); 157 if (rng) 158 kref_put(&rng->ref, cleanup_rng); 159 mutex_unlock(&rng_mutex); 160 } 161 162 static int hwrng_init(struct hwrng *rng) 163 { 164 if (kref_get_unless_zero(&rng->ref)) 165 goto skip_init; 166 167 if (rng->init) { 168 int ret; 169 170 ret = rng->init(rng); 171 if (ret) 172 return ret; 173 } 174 175 kref_init(&rng->ref); 176 reinit_completion(&rng->cleanup_done); 177 178 skip_init: 179 add_early_randomness(rng); 180 181 current_quality = rng->quality ? : default_quality; 182 current_quality &= 1023; 183 184 if (current_quality == 0 && hwrng_fill) 185 kthread_stop(hwrng_fill); 186 if (current_quality > 0 && !hwrng_fill) 187 start_khwrngd(); 188 189 return 0; 190 } 191 192 static int rng_dev_open(struct inode *inode, struct file *filp) 193 { 194 /* enforce read-only access to this chrdev */ 195 if ((filp->f_mode & FMODE_READ) == 0) 196 return -EINVAL; 197 if (filp->f_mode & FMODE_WRITE) 198 return -EINVAL; 199 return 0; 200 } 201 202 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size, 203 int wait) { 204 int present; 205 206 BUG_ON(!mutex_is_locked(&reading_mutex)); 207 if (rng->read) 208 return rng->read(rng, (void *)buffer, size, wait); 209 210 if (rng->data_present) 211 present = rng->data_present(rng, wait); 212 else 213 present = 1; 214 215 if (present) 216 return rng->data_read(rng, (u32 *)buffer); 217 218 return 0; 219 } 220 221 static ssize_t rng_dev_read(struct file *filp, char __user *buf, 222 size_t size, loff_t *offp) 223 { 224 ssize_t ret = 0; 225 int err = 0; 226 int bytes_read, len; 227 struct hwrng *rng; 228 229 while (size) { 230 rng = get_current_rng(); 231 if (IS_ERR(rng)) { 232 err = PTR_ERR(rng); 233 goto out; 234 } 235 if (!rng) { 236 err = -ENODEV; 237 goto out; 238 } 239 240 mutex_lock(&reading_mutex); 241 if (!data_avail) { 242 bytes_read = rng_get_data(rng, rng_buffer, 243 rng_buffer_size(), 244 !(filp->f_flags & O_NONBLOCK)); 245 if (bytes_read < 0) { 246 err = bytes_read; 247 goto out_unlock_reading; 248 } 249 data_avail = bytes_read; 250 } 251 252 if (!data_avail) { 253 if (filp->f_flags & O_NONBLOCK) { 254 err = -EAGAIN; 255 goto out_unlock_reading; 256 } 257 } else { 258 len = data_avail; 259 if (len > size) 260 len = size; 261 262 data_avail -= len; 263 264 if (copy_to_user(buf + ret, rng_buffer + data_avail, 265 len)) { 266 err = -EFAULT; 267 goto out_unlock_reading; 268 } 269 270 size -= len; 271 ret += len; 272 } 273 274 mutex_unlock(&reading_mutex); 275 put_rng(rng); 276 277 if (need_resched()) 278 schedule_timeout_interruptible(1); 279 280 if (signal_pending(current)) { 281 err = -ERESTARTSYS; 282 goto out; 283 } 284 } 285 out: 286 return ret ? : err; 287 288 out_unlock_reading: 289 mutex_unlock(&reading_mutex); 290 put_rng(rng); 291 goto out; 292 } 293 294 295 static const struct file_operations rng_chrdev_ops = { 296 .owner = THIS_MODULE, 297 .open = rng_dev_open, 298 .read = rng_dev_read, 299 .llseek = noop_llseek, 300 }; 301 302 static struct miscdevice rng_miscdev = { 303 .minor = RNG_MISCDEV_MINOR, 304 .name = RNG_MODULE_NAME, 305 .nodename = "hwrng", 306 .fops = &rng_chrdev_ops, 307 }; 308 309 310 static ssize_t hwrng_attr_current_store(struct device *dev, 311 struct device_attribute *attr, 312 const char *buf, size_t len) 313 { 314 int err; 315 struct hwrng *rng; 316 317 err = mutex_lock_interruptible(&rng_mutex); 318 if (err) 319 return -ERESTARTSYS; 320 err = -ENODEV; 321 list_for_each_entry(rng, &rng_list, list) { 322 if (strcmp(rng->name, buf) == 0) { 323 err = 0; 324 if (rng != current_rng) 325 err = set_current_rng(rng); 326 break; 327 } 328 } 329 mutex_unlock(&rng_mutex); 330 331 return err ? : len; 332 } 333 334 static ssize_t hwrng_attr_current_show(struct device *dev, 335 struct device_attribute *attr, 336 char *buf) 337 { 338 ssize_t ret; 339 struct hwrng *rng; 340 341 rng = get_current_rng(); 342 if (IS_ERR(rng)) 343 return PTR_ERR(rng); 344 345 ret = snprintf(buf, PAGE_SIZE, "%s\n", rng ? rng->name : "none"); 346 put_rng(rng); 347 348 return ret; 349 } 350 351 static ssize_t hwrng_attr_available_show(struct device *dev, 352 struct device_attribute *attr, 353 char *buf) 354 { 355 int err; 356 struct hwrng *rng; 357 358 err = mutex_lock_interruptible(&rng_mutex); 359 if (err) 360 return -ERESTARTSYS; 361 buf[0] = '\0'; 362 list_for_each_entry(rng, &rng_list, list) { 363 strlcat(buf, rng->name, PAGE_SIZE); 364 strlcat(buf, " ", PAGE_SIZE); 365 } 366 strlcat(buf, "\n", PAGE_SIZE); 367 mutex_unlock(&rng_mutex); 368 369 return strlen(buf); 370 } 371 372 static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR, 373 hwrng_attr_current_show, 374 hwrng_attr_current_store); 375 static DEVICE_ATTR(rng_available, S_IRUGO, 376 hwrng_attr_available_show, 377 NULL); 378 379 380 static void __exit unregister_miscdev(void) 381 { 382 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available); 383 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current); 384 misc_deregister(&rng_miscdev); 385 } 386 387 static int __init register_miscdev(void) 388 { 389 int err; 390 391 err = misc_register(&rng_miscdev); 392 if (err) 393 goto out; 394 err = device_create_file(rng_miscdev.this_device, 395 &dev_attr_rng_current); 396 if (err) 397 goto err_misc_dereg; 398 err = device_create_file(rng_miscdev.this_device, 399 &dev_attr_rng_available); 400 if (err) 401 goto err_remove_current; 402 out: 403 return err; 404 405 err_remove_current: 406 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current); 407 err_misc_dereg: 408 misc_deregister(&rng_miscdev); 409 goto out; 410 } 411 412 static int hwrng_fillfn(void *unused) 413 { 414 long rc; 415 416 while (!kthread_should_stop()) { 417 struct hwrng *rng; 418 419 rng = get_current_rng(); 420 if (IS_ERR(rng) || !rng) 421 break; 422 mutex_lock(&reading_mutex); 423 rc = rng_get_data(rng, rng_fillbuf, 424 rng_buffer_size(), 1); 425 mutex_unlock(&reading_mutex); 426 put_rng(rng); 427 if (rc <= 0) { 428 pr_warn("hwrng: no data available\n"); 429 msleep_interruptible(10000); 430 continue; 431 } 432 /* Outside lock, sure, but y'know: randomness. */ 433 add_hwgenerator_randomness((void *)rng_fillbuf, rc, 434 rc * current_quality * 8 >> 10); 435 } 436 hwrng_fill = NULL; 437 return 0; 438 } 439 440 static void start_khwrngd(void) 441 { 442 hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng"); 443 if (hwrng_fill == ERR_PTR(-ENOMEM)) { 444 pr_err("hwrng_fill thread creation failed"); 445 hwrng_fill = NULL; 446 } 447 } 448 449 int hwrng_register(struct hwrng *rng) 450 { 451 int err = -EINVAL; 452 struct hwrng *old_rng, *tmp; 453 454 if (rng->name == NULL || 455 (rng->data_read == NULL && rng->read == NULL)) 456 goto out; 457 458 mutex_lock(&rng_mutex); 459 460 /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */ 461 err = -ENOMEM; 462 if (!rng_buffer) { 463 rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL); 464 if (!rng_buffer) 465 goto out_unlock; 466 } 467 if (!rng_fillbuf) { 468 rng_fillbuf = kmalloc(rng_buffer_size(), GFP_KERNEL); 469 if (!rng_fillbuf) { 470 kfree(rng_buffer); 471 goto out_unlock; 472 } 473 } 474 475 /* Must not register two RNGs with the same name. */ 476 err = -EEXIST; 477 list_for_each_entry(tmp, &rng_list, list) { 478 if (strcmp(tmp->name, rng->name) == 0) 479 goto out_unlock; 480 } 481 482 init_completion(&rng->cleanup_done); 483 complete(&rng->cleanup_done); 484 485 old_rng = current_rng; 486 err = 0; 487 if (!old_rng) { 488 err = set_current_rng(rng); 489 if (err) 490 goto out_unlock; 491 } 492 list_add_tail(&rng->list, &rng_list); 493 494 if (old_rng && !rng->init) { 495 /* 496 * Use a new device's input to add some randomness to 497 * the system. If this rng device isn't going to be 498 * used right away, its init function hasn't been 499 * called yet; so only use the randomness from devices 500 * that don't need an init callback. 501 */ 502 add_early_randomness(rng); 503 } 504 505 out_unlock: 506 mutex_unlock(&rng_mutex); 507 out: 508 return err; 509 } 510 EXPORT_SYMBOL_GPL(hwrng_register); 511 512 void hwrng_unregister(struct hwrng *rng) 513 { 514 mutex_lock(&rng_mutex); 515 516 list_del(&rng->list); 517 if (current_rng == rng) { 518 drop_current_rng(); 519 if (!list_empty(&rng_list)) { 520 struct hwrng *tail; 521 522 tail = list_entry(rng_list.prev, struct hwrng, list); 523 524 set_current_rng(tail); 525 } 526 } 527 528 if (list_empty(&rng_list)) { 529 mutex_unlock(&rng_mutex); 530 if (hwrng_fill) 531 kthread_stop(hwrng_fill); 532 } else 533 mutex_unlock(&rng_mutex); 534 535 wait_for_completion(&rng->cleanup_done); 536 } 537 EXPORT_SYMBOL_GPL(hwrng_unregister); 538 539 static int __init hwrng_modinit(void) 540 { 541 return register_miscdev(); 542 } 543 544 static void __exit hwrng_modexit(void) 545 { 546 mutex_lock(&rng_mutex); 547 BUG_ON(current_rng); 548 kfree(rng_buffer); 549 kfree(rng_fillbuf); 550 mutex_unlock(&rng_mutex); 551 552 unregister_miscdev(); 553 } 554 555 module_init(hwrng_modinit); 556 module_exit(hwrng_modexit); 557 558 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver"); 559 MODULE_LICENSE("GPL"); 560