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/segments.h>
41#include <machine/pcb.h>
42#include <machine/md_var.h>
43#include <machine/specialreg.h>
44
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>
50
51static void random_nehemiah_init(void);
52static void random_nehemiah_deinit(void);
53static int random_nehemiah_read(void *, int);
54
55static struct random_hardware_source random_nehemiah = {
56	.ident = "Hardware, VIA Nehemiah Padlock RNG",
57	.source = RANDOM_PURE_NEHEMIAH,
58	.read = random_nehemiah_read
59};
60
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)() ?
64 */
65static struct fpu_kern_ctx *fpu_ctx_save;
66
67/* This H/W source never stores more than 8 bytes in one go */
68/* ARGSUSED */
69static __inline size_t
70VIA_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		".byte	0x0f, 0xa7, 0xc0" /* 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
89static void
90random_nehemiah_init(void)
91{
92
93	fpu_ctx_save = fpu_kern_alloc_ctx(FPU_KERN_NORMAL);
94}
95
96static void
97random_nehemiah_deinit(void)
98{
99
100	fpu_kern_free_ctx(fpu_ctx_save);
101}
102
103static int
104random_nehemiah_read(void *buf, int c)
105{
106	uint8_t *b;
107	size_t count, ret;
108	uint64_t tmp;
109
110	if ((fpu_kern_enter(curthread, fpu_ctx_save, FPU_KERN_NORMAL) == 0)) {
111		b = buf;
112		for (count = c; count > 0; count -= ret) {
113			ret = MIN(VIA_RNG_store(&tmp), count);
114			memcpy(b, &tmp, ret);
115			b += ret;
116		}
117		fpu_kern_leave(curthread, fpu_ctx_save);
118	}
119	else
120		c = 0;
121
122	return (c);
123}
124
125static int
126nehemiah_modevent(module_t mod, int type, void *unused)
127{
128	int error = 0;
129
130	switch (type) {
131	case MOD_LOAD:
132		if (via_feature_rng & VIA_HAS_RNG) {
133			live_entropy_source_register(&random_nehemiah);
134			random_nehemiah_init();
135		} else
136#ifndef KLD_MODULE
137			if (bootverbose)
138#endif
139				printf("%s: VIA Padlock RNG not present\n",
140				    random_nehemiah.ident);
141		break;
142
143	case MOD_UNLOAD:
144		if (via_feature_rng & VIA_HAS_RNG)
145			random_nehemiah_deinit();
146			live_entropy_source_deregister(&random_nehemiah);
147		break;
148
149	case MOD_SHUTDOWN:
150		break;
151
152	default:
153		error = EOPNOTSUPP;
154		break;
155
156	}
157
158	return (error);
159}
160
161LIVE_ENTROPY_SRC_MODULE(nehemiah, nehemiah_modevent, 1);
162