xref: /freebsd/sys/dev/random/darn.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/conf.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/random.h>
42 #include <sys/systm.h>
43 
44 #include <machine/cpu.h>
45 #include <machine/md_var.h>
46 
47 #include <dev/random/randomdev.h>
48 
49 /*
50  * Power ISA 3.0 adds a "darn" instruction (Deliver A Random Number).  The RNG
51  * backing this instruction conforms to NIST SP800-90B and SP800-90C at the
52  * point of hardware design, and provides a minimum of 0.5 bits of entropy per
53  * bit.
54  */
55 
56 #define	RETRY_COUNT	10
57 
58 static u_int random_darn_read(void *, u_int);
59 
60 static struct random_source random_darn = {
61 	.rs_ident = "PowerISA DARN random number generator",
62 	.rs_source = RANDOM_PURE_DARN,
63 	.rs_read = random_darn_read
64 };
65 
66 static inline int
67 darn_rng_store(u_long *buf)
68 {
69 	u_long rndval;
70 	int retry;
71 
72 	for (retry = RETRY_COUNT; retry > 0; --retry) {
73 		/* "DARN %rN, 1" instruction */
74 		/*
75 		 * Arguments for DARN: rN and "L", where "L" can be one of:
76 		 * 0 - 32-bit conditional random number
77 		 * 1 - Conditional random number (conditioned to remove bias)
78 		 * 2 - Raw random number	 (unprocessed, may include bias)
79 		 * 3 - Reserved
80 		 */
81 	    	__asm __volatile(".long 0x7c0105e6 | (%0 << 21)" :
82 	    	    "+r"(rndval));
83 		if (rndval != ~0)
84 			break;
85 	}
86 
87 	*buf = rndval;
88 	return (retry);
89 }
90 
91 /* It is required that buf length is a multiple of sizeof(u_long). */
92 static u_int
93 random_darn_read(void *buf, u_int c)
94 {
95 	u_long *b, rndval;
96 	u_int count;
97 
98 	KASSERT(c % sizeof(*b) == 0, ("partial read %d", c));
99 	b = buf;
100 	for (count = c; count > 0; count -= sizeof(*b)) {
101 		if (darn_rng_store(&rndval) == 0)
102 			break;
103 		*b++ = rndval;
104 	}
105 	return (c - count);
106 }
107 
108 static int
109 darn_modevent(module_t mod, int type, void *unused)
110 {
111 	int error = 0;
112 
113 	switch (type) {
114 	case MOD_LOAD:
115 		if (cpu_features2 & PPC_FEATURE2_DARN) {
116 			random_source_register(&random_darn);
117 			printf("random: fast provider: \"%s\"\n", random_darn.rs_ident);
118 		}
119 		break;
120 
121 	case MOD_UNLOAD:
122 		if (cpu_features2 & PPC_FEATURE2_DARN)
123 			random_source_deregister(&random_darn);
124 		break;
125 
126 	case MOD_SHUTDOWN:
127 		break;
128 
129 	default:
130 		error = EOPNOTSUPP;
131 		break;
132 
133 	}
134 
135 	return (error);
136 }
137 
138 static moduledata_t darn_mod = {
139 	"darn",
140 	darn_modevent,
141 	0
142 };
143 
144 DECLARE_MODULE(darn, darn_mod, SI_SUB_RANDOM, SI_ORDER_FOURTH);
145 MODULE_VERSION(darn, 1);
146 MODULE_DEPEND(darn, random_harvestq, 1, 1, 1);
147