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/segments.h>
41 #include <machine/pcb.h>
42 #include <machine/md_var.h>
43 #include <machine/specialreg.h>
45 #include <dev/random/randomdev.h>
46 #include <dev/random/randomdev_soft.h>
47 #include <dev/random/random_harvestq.h>
48 #include <dev/random/live_entropy_sources.h>
49 #include <dev/random/random_adaptors.h>
51 static void random_nehemiah_init(void);
52 static void random_nehemiah_deinit(void);
53 static int random_nehemiah_read(void *, int);
55 static struct random_hardware_source random_nehemiah = {
56 .ident = "Hardware, VIA Nehemiah Padlock RNG",
57 .source = RANDOM_PURE_NEHEMIAH,
58 .read = random_nehemiah_read
61 /* TODO: 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)() ?
65 static struct fpu_kern_ctx *fpu_ctx_save;
67 /* This H/W source never stores more than 8 bytes in one go */
69 static __inline size_t
70 VIA_RNG_store(void *buf)
78 ".byte 0x0f, 0xa7, 0xc0" /* xstore */
79 : "=a" (retval), "+d" (rate), "+D" (buf)
90 random_nehemiah_init(void)
93 fpu_ctx_save = fpu_kern_alloc_ctx(FPU_KERN_NORMAL);
97 random_nehemiah_deinit(void)
100 fpu_kern_free_ctx(fpu_ctx_save);
104 random_nehemiah_read(void *buf, int c)
110 if ((fpu_kern_enter(curthread, fpu_ctx_save, FPU_KERN_NORMAL) == 0)) {
112 for (count = c; count > 0; count -= ret) {
113 ret = MIN(VIA_RNG_store(&tmp), count);
114 memcpy(b, &tmp, ret);
117 fpu_kern_leave(curthread, fpu_ctx_save);
126 nehemiah_modevent(module_t mod, int type, void *unused)
132 if (via_feature_rng & VIA_HAS_RNG) {
133 live_entropy_source_register(&random_nehemiah);
134 random_nehemiah_init();
139 printf("%s: VIA Padlock RNG not present\n",
140 random_nehemiah.ident);
144 if (via_feature_rng & VIA_HAS_RNG)
145 random_nehemiah_deinit();
146 live_entropy_source_deregister(&random_nehemiah);
161 LIVE_ENTROPY_SRC_MODULE(nehemiah, nehemiah_modevent, 1);