xref: /freebsd/sys/dev/random/ivy.c (revision ab2043b81eaba0d7d7769b4a58b2b6d17bc464a3)
1 /*-
2  * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_cpu.h"
32 
33 #ifdef RDRAND_RNG
34 
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/selinfo.h>
40 #include <sys/systm.h>
41 #include <dev/random/randomdev.h>
42 
43 #define	RETRY_COUNT	10
44 
45 static void random_ivy_init(void);
46 static void random_ivy_deinit(void);
47 static int random_ivy_read(void *, int);
48 
49 struct random_systat random_ivy = {
50 	.ident = "Hardware, Intel IvyBridge+ RNG",
51 	.init = random_ivy_init,
52 	.deinit = random_ivy_deinit,
53 	.read = random_ivy_read,
54 	.write = (random_write_func_t *)random_null_func,
55 	.reseed = (random_reseed_func_t *)random_null_func,
56 	.seeded = 1,
57 };
58 
59 static inline int
60 ivy_rng_store(long *tmp)
61 {
62 #ifdef __GNUCLIKE_ASM
63 	uint32_t count;
64 
65 	__asm __volatile(
66 #ifdef __amd64__
67 	    ".byte\t0x48,0x0f,0xc7,0xf0\n\t" /* rdrand %rax */
68 	    "jnc\t1f\n\t"
69 	    "movq\t%%rax,%1\n\t"
70 	    "movl\t$8,%%eax\n"
71 #else /* i386 */
72 	    ".byte\t0x0f,0xc7,0xf0\n\t" /* rdrand %eax */
73 	    "jnc\t1f\n\t"
74 	    "movl\t%%eax,%1\n\t"
75 	    "movl\t$4,%%eax\n"
76 #endif
77 	    "1:\n"	/* %eax is cleared by processor on failure */
78 	    : "=a" (count), "=g" (*tmp) : "a" (0) : "cc");
79 	return (count);
80 #else /* __GNUCLIKE_ASM */
81 	return (0);
82 #endif
83 }
84 
85 static void
86 random_ivy_init(void)
87 {
88 }
89 
90 void
91 random_ivy_deinit(void)
92 {
93 }
94 
95 static int
96 random_ivy_read(void *buf, int c)
97 {
98 	char *b;
99 	long tmp;
100 	int count, res, retry;
101 
102 	for (count = c, b = buf; count > 0; count -= res, b += res) {
103 		for (retry = 0; retry < RETRY_COUNT; retry++) {
104 			res = ivy_rng_store(&tmp);
105 			if (res != 0)
106 				break;
107 		}
108 		if (res == 0)
109 			break;
110 		if (res > count)
111 			res = count;
112 		memcpy(b, &tmp, res);
113 	}
114 	return (c - count);
115 }
116 
117 #endif
118