2 * Copyright (c) 2018 Justin Hibbits
3 * Copyright (c) 2013 The FreeBSD Foundation
4 * Copyright (c) 2013 David E. O'Brien <obrien@NUXI.org>
5 * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
8 * Portions of this software were developed by Konstantin Belousov
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer
16 * in this position and unchanged.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/random.h>
44 #include <sys/systm.h>
46 #include <machine/cpu.h>
47 #include <machine/md_var.h>
49 #include <dev/random/randomdev.h>
52 * Power ISA 3.0 adds a "darn" instruction (Deliver A Random Number). The RNG
53 * backing this instruction conforms to NIST SP800-90B and SP800-90C at the
54 * point of hardware design, and provides a minimum of 0.5 bits of entropy per
58 #define RETRY_COUNT 10
60 static u_int random_darn_read(void *, u_int);
62 static struct random_source random_darn = {
63 .rs_ident = "PowerISA DARN random number generator",
64 .rs_source = RANDOM_PURE_DARN,
65 .rs_read = random_darn_read
69 darn_rng_store(u_long *buf)
74 for (retry = RETRY_COUNT; retry > 0; --retry) {
75 /* "DARN %rN, 1" instruction */
77 * Arguments for DARN: rN and "L", where "L" can be one of:
78 * 0 - 32-bit conditional random number
79 * 1 - Conditional random number (conditioned to remove bias)
80 * 2 - Raw random number (unprocessed, may include bias)
83 __asm __volatile(".long 0x7c0105e6 | (%0 << 21)" :
93 /* It is required that buf length is a multiple of sizeof(u_long). */
95 random_darn_read(void *buf, u_int c)
100 KASSERT(c % sizeof(*b) == 0, ("partial read %d", c));
102 for (count = c; count > 0; count -= sizeof(*b)) {
103 if (darn_rng_store(&rndval) == 0)
111 darn_modevent(module_t mod, int type, void *unused)
117 if (cpu_features2 & PPC_FEATURE2_DARN) {
118 random_source_register(&random_darn);
119 printf("random: fast provider: \"%s\"\n", random_darn.rs_ident);
124 if (cpu_features2 & PPC_FEATURE2_DARN)
125 random_source_deregister(&random_darn);
140 static moduledata_t darn_mod = {
146 DECLARE_MODULE(darn, darn_mod, SI_SUB_RANDOM, SI_ORDER_FOURTH);
147 MODULE_VERSION(darn, 1);
148 MODULE_DEPEND(darn, random_harvestq, 1, 1, 1);