xref: /freebsd/sys/dev/random/darn.c (revision a0409676120c1e558d0ade943019934e0f15118d)
1 /*-
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>
6  * All rights reserved.
7  *
8  * Portions of this software were developed by Konstantin Belousov
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
20  *
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.
31  *
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/conf.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/random.h>
44 #include <sys/systm.h>
45 
46 #include <machine/cpu.h>
47 #include <machine/md_var.h>
48 
49 #include <dev/random/randomdev.h>
50 
51 /*
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
55  * bit.
56  */
57 
58 #define	RETRY_COUNT	10
59 
60 static u_int random_darn_read(void *, u_int);
61 
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
66 };
67 
68 static inline int
69 darn_rng_store(u_long *buf)
70 {
71 	u_long rndval;
72 	int retry;
73 
74 	for (retry = RETRY_COUNT; retry > 0; --retry) {
75 		/* "DARN %rN, 1" instruction */
76 		/*
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)
81 		 * 3 - Reserved
82 		 */
83 	    	__asm __volatile(".long 0x7c0105e6 | (%0 << 21)" :
84 	    	    "+r"(rndval));
85 		if (rndval != ~0)
86 			break;
87 	}
88 
89 	*buf = rndval;
90 	return (retry);
91 }
92 
93 /* It is required that buf length is a multiple of sizeof(u_long). */
94 static u_int
95 random_darn_read(void *buf, u_int c)
96 {
97 	u_long *b, rndval;
98 	u_int count;
99 
100 	KASSERT(c % sizeof(*b) == 0, ("partial read %d", c));
101 	b = buf;
102 	for (count = c; count > 0; count -= sizeof(*b)) {
103 		if (darn_rng_store(&rndval) == 0)
104 			break;
105 		*b++ = rndval;
106 	}
107 	return (c - count);
108 }
109 
110 static int
111 darn_modevent(module_t mod, int type, void *unused)
112 {
113 	int error = 0;
114 
115 	switch (type) {
116 	case MOD_LOAD:
117 		if (cpu_features2 & PPC_FEATURE2_DARN) {
118 			random_source_register(&random_darn);
119 			printf("random: fast provider: \"%s\"\n", random_darn.rs_ident);
120 		}
121 		break;
122 
123 	case MOD_UNLOAD:
124 		if (cpu_features2 & PPC_FEATURE2_DARN)
125 			random_source_deregister(&random_darn);
126 		break;
127 
128 	case MOD_SHUTDOWN:
129 		break;
130 
131 	default:
132 		error = EOPNOTSUPP;
133 		break;
134 
135 	}
136 
137 	return (error);
138 }
139 
140 static moduledata_t darn_mod = {
141 	"darn",
142 	darn_modevent,
143 	0
144 };
145 
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);
149