1 /*- 2 * Copyright (c) 2013-2015 Mark R V Murray 3 * Copyright (c) 2013 David E. O'Brien <obrien@NUXI.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer 11 * in this position and unchanged. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/conf.h> 35 #include <sys/lock.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 #include <sys/random.h> 39 #include <sys/systm.h> 40 41 #include <machine/segments.h> 42 #include <machine/pcb.h> 43 #include <machine/md_var.h> 44 #include <machine/specialreg.h> 45 46 #include <dev/random/randomdev.h> 47 48 static void random_nehemiah_init(void); 49 static void random_nehemiah_deinit(void); 50 static u_int random_nehemiah_read(void *, u_int); 51 52 static struct random_source random_nehemiah = { 53 .rs_ident = "VIA Nehemiah Padlock RNG", 54 .rs_source = RANDOM_PURE_NEHEMIAH, 55 .rs_read = random_nehemiah_read 56 }; 57 58 static struct fpu_kern_ctx *fpu_ctx_save; 59 60 /* This H/W source never stores more than 8 bytes in one go */ 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 __asm __volatile( 69 "movl $0,%%edx\n\t" 70 ".byte 0x0f, 0xa7, 0xc0" 71 : "=a" (retval), "+d" (rate), "+D" (buf) 72 : 73 : "memory" 74 ); 75 if (rate == 0) 76 return (retval&0x1f); 77 return (0); 78 } 79 80 static void 81 random_nehemiah_init(void) 82 { 83 84 fpu_ctx_save = fpu_kern_alloc_ctx(FPU_KERN_NORMAL); 85 } 86 87 static void 88 random_nehemiah_deinit(void) 89 { 90 91 fpu_kern_free_ctx(fpu_ctx_save); 92 } 93 94 /* It is specifically allowed that buf is a multiple of sizeof(long) */ 95 static u_int 96 random_nehemiah_read(void *buf, u_int c) 97 { 98 uint8_t *b; 99 size_t count, ret; 100 uint64_t tmp; 101 102 fpu_kern_enter(curthread, fpu_ctx_save, FPU_KERN_NORMAL); 103 b = buf; 104 for (count = c; count > 0; count -= ret) { 105 ret = MIN(VIA_RNG_store(&tmp), count); 106 memcpy(b, &tmp, ret); 107 b += ret; 108 } 109 fpu_kern_leave(curthread, fpu_ctx_save); 110 111 return (c); 112 } 113 114 static int 115 nehemiah_modevent(module_t mod, int type, void *unused) 116 { 117 int error = 0; 118 119 switch (type) { 120 case MOD_LOAD: 121 if (via_feature_rng & VIA_HAS_RNG) { 122 random_source_register(&random_nehemiah); 123 printf("random: fast provider: \"%s\"\n", random_nehemiah.rs_ident); 124 random_nehemiah_init(); 125 } 126 break; 127 128 case MOD_UNLOAD: 129 if (via_feature_rng & VIA_HAS_RNG) { 130 random_nehemiah_deinit(); 131 random_source_deregister(&random_nehemiah); 132 } 133 break; 134 135 case MOD_SHUTDOWN: 136 break; 137 138 default: 139 error = EOPNOTSUPP; 140 break; 141 142 } 143 144 return (error); 145 } 146 147 static moduledata_t nehemiah_mod = { 148 "nehemiah", 149 nehemiah_modevent, 150 0 151 }; 152 153 DECLARE_MODULE(nehemiah, nehemiah_mod, SI_SUB_RANDOM, SI_ORDER_FOURTH); 154 MODULE_VERSION(nehemiah, 1); 155 MODULE_DEPEND(nehemiah, random_harvestq, 1, 1, 1); 156