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