2 * Copyright (c) 2013 Mark R V Murray
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/kernel.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>
40 #include <machine/pcb.h>
41 #include <machine/md_var.h>
42 #include <machine/specialreg.h>
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>
50 static void random_nehemiah_init(void);
51 static void random_nehemiah_deinit(void);
52 static int random_nehemiah_read(void *, int);
54 struct random_hardware_source random_nehemiah = {
55 .ident = "Hardware, VIA Nehemiah Padlock RNG",
56 .source = RANDOM_PURE_NEHEMIAH,
57 .read = random_nehemiah_read
60 /* This H/W RNG never stores more than 8 bytes in one go */
62 static struct fpu_kern_ctx *fpu_ctx_save;
65 static __inline size_t
66 VIA_RNG_store(void *buf)
75 : "=a" (retval), "+d" (rate), "+D" (buf)
86 random_nehemiah_init(void)
89 fpu_ctx_save = fpu_kern_alloc_ctx(FPU_KERN_NORMAL);
93 random_nehemiah_deinit(void)
96 fpu_kern_free_ctx(fpu_ctx_save);
100 random_nehemiah_read(void *buf, int c)
106 if ((fpu_kern_enter(curthread, fpu_ctx_save, FPU_KERN_NORMAL) == 0)) {
108 for (count = c; count > 0; count -= ret) {
109 ret = MIN(VIA_RNG_store(&tmp), count);
110 memcpy(b, &tmp, ret);
113 fpu_kern_leave(curthread, fpu_ctx_save);
122 nehemiah_modevent(module_t mod, int type, void *unused)
128 if (via_feature_rng & VIA_HAS_RNG) {
129 live_entropy_source_register(&random_nehemiah);
130 random_nehemiah_init();
135 printf("%s: VIA Padlock RNG not present\n",
136 random_nehemiah.ident);
140 if (via_feature_rng & VIA_HAS_RNG)
141 random_nehemiah_deinit();
142 live_entropy_source_deregister(&random_nehemiah);
157 LIVE_ENTROPY_SRC_MODULE(nehemiah, nehemiah_modevent, 1);