]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/random/nehemiah.c
Snapshot. This passes the build test, but has not yet been finished or debugged.
[FreeBSD/FreeBSD.git] / sys / dev / random / nehemiah.c
1 /*-
2  * Copyright (c) 2013 Mark R V Murray
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 <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/lock.h>
34 #include <sys/module.h>
35 #include <sys/random.h>
36 #include <sys/selinfo.h>
37 #include <sys/systm.h>
38
39 #include <machine/pcb.h>
40 #include <machine/md_var.h>
41 #include <machine/specialreg.h>
42
43 #include <dev/random/live_entropy_sources.h>
44 #include <dev/random/random_adaptors.h>
45 #include <dev/random/randomdev.h>
46
47 static void random_nehemiah_init(void);
48 static void random_nehemiah_deinit(void);
49 static int random_nehemiah_read(void *, int);
50
51 struct random_hardware_source random_nehemiah = {
52         .ident = "Hardware, VIA Nehemiah Padlock RNG",
53         .source = RANDOM_PURE_NEHEMIAH,
54         .read = random_nehemiah_read
55 };
56
57 /* This H/W RNG never stores more than 8 bytes in one go */
58
59 static struct fpu_kern_ctx *fpu_ctx_save;
60
61 /* ARGSUSED */
62 static __inline size_t
63 VIA_RNG_store(void *buf)
64 {
65         uint32_t retval = 0;
66         uint32_t rate = 0;
67
68 #ifdef __GNUCLIKE_ASM
69         __asm __volatile(
70                 "movl   $0,%%edx\n\t"
71                 "xstore"
72                         : "=a" (retval), "+d" (rate), "+D" (buf)
73                         :
74                         : "memory"
75         );
76 #endif
77         if (rate == 0)
78                 return (retval&0x1f);
79         return (0);
80 }
81
82 static void
83 random_nehemiah_init(void)
84 {
85
86         fpu_ctx_save = fpu_kern_alloc_ctx(FPU_KERN_NORMAL);
87 }
88
89 static void
90 random_nehemiah_deinit(void)
91 {
92
93         fpu_kern_free_ctx(fpu_ctx_save);
94 }
95
96 static int
97 random_nehemiah_read(void *buf, int c)
98 {
99         uint8_t *b;
100         size_t count, ret;
101         uint64_t tmp;
102
103         if ((fpu_kern_enter(curthread, fpu_ctx_save, FPU_KERN_NORMAL) == 0)) {
104                 b = buf;
105                 for (count = c; count > 0; count -= ret) {
106                         ret = MIN(VIA_RNG_store(&tmp), count);
107                         memcpy(b, &tmp, ret);
108                         b += ret;
109                 }
110                 fpu_kern_leave(curthread, fpu_ctx_save);
111         }
112         else
113                 c = 0;
114
115         return (c);
116 }
117
118 static int
119 nehemiah_modevent(module_t mod, int type, void *unused)
120 {
121         int error = 0;
122
123         switch (type) {
124         case MOD_LOAD:
125                 if (via_feature_rng & VIA_HAS_RNG) {
126                         live_entropy_source_register(&random_nehemiah);
127                         random_nehemiah_init();
128                 } else
129 #ifndef KLD_MODULE
130                         if (bootverbose)
131 #endif
132                                 printf("%s: VIA Padlock RNG not present\n",
133                                     random_nehemiah.ident);
134                 break;
135
136         case MOD_UNLOAD:
137                 if (via_feature_rng & VIA_HAS_RNG)
138                         random_nehemiah_deinit();
139                         live_entropy_source_deregister(&random_nehemiah);
140                 break;
141
142         case MOD_SHUTDOWN:
143                 break;
144
145         default:
146                 error = EOPNOTSUPP;
147                 break;
148
149         }
150
151         return (error);
152 }
153
154 LIVE_ENTROPY_SRC_MODULE(nehemiah, nehemiah_modevent, 1);