]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/random/nehemiah.c
There is no Python in the FreeBSD base system
[FreeBSD/FreeBSD.git] / sys / dev / random / nehemiah.c
1 /*-
2  * Copyright (c) 2013 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/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/conf.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/random.h>
39 #include <sys/systm.h>
40
41 #include <machine/segments.h>
42 #include <machine/pcb.h>
43 #include <machine/md_var.h>
44 #include <machine/specialreg.h>
45
46 #include <dev/random/randomdev.h>
47 #include <dev/random/randomdev_soft.h>
48 #include <dev/random/random_adaptors.h>
49 #include <dev/random/live_entropy_sources.h>
50
51 static void random_nehemiah_init(void);
52 static void random_nehemiah_deinit(void);
53 static u_int random_nehemiah_read(void *, u_int);
54
55 static struct live_entropy_source random_nehemiah = {
56         .les_ident = "VIA Nehemiah Padlock RNG",
57         .les_source = RANDOM_PURE_NEHEMIAH,
58         .les_read = random_nehemiah_read
59 };
60
61 /* XXX: FIX? Now that the Davies-Meyer hash is gone and we only use
62  * the 'xstore' instruction, do we still need to preserve the
63  * FPU state with fpu_kern_(enter|leave)() ?
64  */
65 static struct fpu_kern_ctx *fpu_ctx_save;
66
67 /* This H/W source never stores more than 8 bytes in one go */
68 /* ARGSUSED */
69 static __inline size_t
70 VIA_RNG_store(void *buf)
71 {
72         uint32_t retval = 0;
73         uint32_t rate = 0;
74
75 #ifdef __GNUCLIKE_ASM
76         __asm __volatile(
77                 "movl   $0,%%edx\n\t"
78                 "xstore"
79                         : "=a" (retval), "+d" (rate), "+D" (buf)
80                         :
81                         : "memory"
82         );
83 #endif
84         if (rate == 0)
85                 return (retval&0x1f);
86         return (0);
87 }
88
89 static void
90 random_nehemiah_init(void)
91 {
92
93         fpu_ctx_save = fpu_kern_alloc_ctx(FPU_KERN_NORMAL);
94 }
95
96 static void
97 random_nehemiah_deinit(void)
98 {
99
100         fpu_kern_free_ctx(fpu_ctx_save);
101 }
102
103 /* It is specifically allowed that buf is a multiple of sizeof(long) */
104 static u_int
105 random_nehemiah_read(void *buf, u_int c)
106 {
107         uint8_t *b;
108         size_t count, ret;
109         uint64_t tmp;
110
111         if ((fpu_kern_enter(curthread, fpu_ctx_save, FPU_KERN_NORMAL) == 0)) {
112                 b = buf;
113                 for (count = c; count > 0; count -= ret) {
114                         ret = MIN(VIA_RNG_store(&tmp), count);
115                         memcpy(b, &tmp, ret);
116                         b += ret;
117                 }
118                 fpu_kern_leave(curthread, fpu_ctx_save);
119         }
120         else
121                 c = 0;
122
123         return (c);
124 }
125
126 static int
127 nehemiah_modevent(module_t mod, int type, void *unused)
128 {
129         int error = 0;
130
131         switch (type) {
132         case MOD_LOAD:
133                 if (via_feature_rng & VIA_HAS_RNG) {
134                         live_entropy_source_register(&random_nehemiah);
135                         printf("random: live provider: \"%s\"\n", random_nehemiah.les_ident);
136                         random_nehemiah_init();
137                 }
138                 break;
139
140         case MOD_UNLOAD:
141                 if (via_feature_rng & VIA_HAS_RNG)
142                         random_nehemiah_deinit();
143                         live_entropy_source_deregister(&random_nehemiah);
144                 break;
145
146         case MOD_SHUTDOWN:
147                 break;
148
149         default:
150                 error = EOPNOTSUPP;
151                 break;
152
153         }
154
155         return (error);
156 }
157
158 DEV_MODULE(nehemiah, nehemiah_modevent, NULL);
159 MODULE_VERSION(nehemiah, 1);
160 MODULE_DEPEND(nehemiah, randomdev, 1, 1, 1);