xref: /freebsd/sys/dev/random/nehemiah.c (revision 6dced2c6358e467ac1dccd99f6f648d4f71957a8)
1 /*-
2  * Copyright (c) 2013-2015 Mark R V Murray
3  * Copyright (c) 2013 David E. O'Brien <obrien@NUXI.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer
11  *    in this position and unchanged.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/conf.h>
32 #include <sys/lock.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35 #include <sys/random.h>
36 #include <sys/systm.h>
37 
38 #include <machine/segments.h>
39 #include <machine/pcb.h>
40 #include <machine/md_var.h>
41 #include <machine/specialreg.h>
42 
43 #include <dev/random/randomdev.h>
44 
45 static u_int random_nehemiah_read(void *, u_int);
46 
47 static struct random_source random_nehemiah = {
48 	.rs_ident = "VIA Nehemiah Padlock RNG",
49 	.rs_source = RANDOM_PURE_NEHEMIAH,
50 	.rs_read = random_nehemiah_read
51 };
52 
53 /* This H/W source never stores more than 8 bytes in one go */
54 /* ARGSUSED */
55 static __inline size_t
56 VIA_RNG_store(void *buf)
57 {
58 	uint32_t retval = 0;
59 	uint32_t rate = 0;
60 
61 	__asm __volatile(
62 		"movl	$0,%%edx\n\t"
63 		".byte 0x0f, 0xa7, 0xc0"
64 			: "=a" (retval), "+d" (rate), "+D" (buf)
65 			:
66 			: "memory"
67 	);
68 	if (rate == 0)
69 		return (retval&0x1f);
70 	return (0);
71 }
72 
73 /* It is specifically allowed that buf is a multiple of sizeof(long) */
74 static u_int
75 random_nehemiah_read(void *buf, u_int c)
76 {
77 	uint8_t *b;
78 	size_t count, ret;
79 	uint64_t tmp;
80 
81 	fpu_kern_enter(curthread, NULL, FPU_KERN_NORMAL | FPU_KERN_NOCTX);
82 	b = buf;
83 	for (count = c; count > 0; count -= ret) {
84 		ret = MIN(VIA_RNG_store(&tmp), count);
85 		memcpy(b, &tmp, ret);
86 		b += ret;
87 	}
88 	fpu_kern_leave(curthread, NULL);
89 
90 	return (c);
91 }
92 
93 static int
94 nehemiah_modevent(module_t mod, int type, void *unused)
95 {
96 	int error = 0;
97 
98 	switch (type) {
99 	case MOD_LOAD:
100 		if (via_feature_rng & VIA_HAS_RNG) {
101 			random_source_register(&random_nehemiah);
102 			printf("random: fast provider: \"%s\"\n", random_nehemiah.rs_ident);
103 		}
104 		break;
105 
106 	case MOD_UNLOAD:
107 		if (via_feature_rng & VIA_HAS_RNG) {
108 			random_source_deregister(&random_nehemiah);
109 		}
110 		break;
111 
112 	case MOD_SHUTDOWN:
113 		break;
114 
115 	default:
116 		error = EOPNOTSUPP;
117 		break;
118 
119 	}
120 
121 	return (error);
122 }
123 
124 static moduledata_t nehemiah_mod = {
125 	"nehemiah",
126 	nehemiah_modevent,
127 	0
128 };
129 
130 DECLARE_MODULE(nehemiah, nehemiah_mod, SI_SUB_RANDOM, SI_ORDER_FOURTH);
131 MODULE_VERSION(nehemiah, 1);
132 MODULE_DEPEND(nehemiah, random_harvestq, 1, 1, 1);
133