xref: /freebsd/sys/dev/random/ivy.c (revision 09d325677d53a12c79a43664ff29871e92247629)
1 /*-
2  * Copyright (c) 2013 The FreeBSD Foundation
3  * Copyright (c) 2013 David E. O'Brien <obrien@NUXI.org>
4  * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Portions of this software were developed by Konstantin Belousov
8  * under sponsorship from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer
15  *    in this position and unchanged.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/random.h>
42 #include <sys/selinfo.h>
43 #include <sys/systm.h>
44 
45 #include <machine/md_var.h>
46 #include <machine/specialreg.h>
47 
48 #include <dev/random/randomdev.h>
49 #include <dev/random/randomdev_soft.h>
50 #include <dev/random/random_harvestq.h>
51 #include <dev/random/live_entropy_sources.h>
52 #include <dev/random/random_adaptors.h>
53 
54 #define	RETRY_COUNT	10
55 
56 static int random_ivy_read(void *, int);
57 
58 static struct random_hardware_source random_ivy = {
59 	.ident = "Hardware, Intel IvyBridge+ RNG",
60 	.source = RANDOM_PURE_RDRAND,
61 	.read = random_ivy_read
62 };
63 
64 static inline int
65 ivy_rng_store(long *buf)
66 {
67 #ifdef __GNUCLIKE_ASM
68 	long tmp;
69 	int retry;
70 
71 	retry = RETRY_COUNT;
72 	__asm __volatile(
73 	    "1:\n\t"
74 	    "rdrand	%2\n\t"	/* read randomness into tmp */
75 	    "jb		2f\n\t" /* CF is set on success, exit retry loop */
76 	    "dec	%0\n\t" /* otherwise, retry-- */
77 	    "jne	1b\n\t" /* and loop if retries are not exhausted */
78 	    "jmp	3f\n"	/* failure, retry is 0, used as return value */
79 	    "2:\n\t"
80 	    "mov	%2,%1\n\t" /* *buf = tmp */
81 	    "3:"
82 	    : "+q" (retry), "=m" (*buf), "=q" (tmp) : : "cc");
83 	return (retry);
84 #else /* __GNUCLIKE_ASM */
85 	return (0);
86 #endif
87 }
88 
89 static int
90 random_ivy_read(void *buf, int c)
91 {
92 	long *b;
93 	int count;
94 
95 	KASSERT(c % sizeof(long) == 0, ("partial read %d", c));
96 	for (b = buf, count = c; count > 0; count -= sizeof(long), b++) {
97 		if (ivy_rng_store(b) == 0)
98 			break;
99 	}
100 	return (c - count);
101 }
102 
103 static int
104 rdrand_modevent(module_t mod, int type, void *unused)
105 {
106 	int error = 0;
107 
108 	switch (type) {
109 	case MOD_LOAD:
110 		if (cpu_feature2 & CPUID2_RDRAND)
111 			live_entropy_source_register(&random_ivy);
112 		else
113 #ifndef KLD_MODULE
114 			if (bootverbose)
115 #endif
116 				printf("%s: RDRAND is not present\n",
117 				    random_ivy.ident);
118 		break;
119 
120 	case MOD_UNLOAD:
121 		if (cpu_feature2 & CPUID2_RDRAND)
122 			live_entropy_source_deregister(&random_ivy);
123 		break;
124 
125 	case MOD_SHUTDOWN:
126 		break;
127 
128 	default:
129 		error = EOPNOTSUPP;
130 		break;
131 
132 	}
133 
134 	return (error);
135 }
136 
137 LIVE_ENTROPY_SRC_MODULE(random_rdrand, rdrand_modevent, 1);
138