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 #include <sys/param.h> 31 #include <sys/kernel.h> 32 #include <sys/conf.h> 33 #include <sys/lock.h> 34 #include <sys/malloc.h> 35 #include <sys/module.h> 36 #include <sys/random.h> 37 #include <sys/systm.h> 38 39 #include <machine/segments.h> 40 #include <machine/pcb.h> 41 #include <machine/md_var.h> 42 #include <machine/specialreg.h> 43 44 #include <dev/random/randomdev.h> 45 46 static void random_nehemiah_init(void); 47 static void random_nehemiah_deinit(void); 48 static u_int random_nehemiah_read(void *, u_int); 49 50 static struct random_source random_nehemiah = { 51 .rs_ident = "VIA Nehemiah Padlock RNG", 52 .rs_source = RANDOM_PURE_NEHEMIAH, 53 .rs_read = random_nehemiah_read 54 }; 55 56 static struct fpu_kern_ctx *fpu_ctx_save; 57 58 /* This H/W source never stores more than 8 bytes in one go */ 59 /* ARGSUSED */ 60 static __inline size_t 61 VIA_RNG_store(void *buf) 62 { 63 uint32_t retval = 0; 64 uint32_t rate = 0; 65 66 __asm __volatile( 67 "movl $0,%%edx\n\t" 68 ".byte 0x0f, 0xa7, 0xc0" 69 : "=a" (retval), "+d" (rate), "+D" (buf) 70 : 71 : "memory" 72 ); 73 if (rate == 0) 74 return (retval&0x1f); 75 return (0); 76 } 77 78 static void 79 random_nehemiah_init(void) 80 { 81 82 fpu_ctx_save = fpu_kern_alloc_ctx(FPU_KERN_NORMAL); 83 } 84 85 static void 86 random_nehemiah_deinit(void) 87 { 88 89 fpu_kern_free_ctx(fpu_ctx_save); 90 } 91 92 /* It is specifically allowed that buf is a multiple of sizeof(long) */ 93 static u_int 94 random_nehemiah_read(void *buf, u_int c) 95 { 96 uint8_t *b; 97 size_t count, ret; 98 uint64_t tmp; 99 100 fpu_kern_enter(curthread, fpu_ctx_save, FPU_KERN_NORMAL); 101 b = buf; 102 for (count = c; count > 0; count -= ret) { 103 ret = MIN(VIA_RNG_store(&tmp), count); 104 memcpy(b, &tmp, ret); 105 b += ret; 106 } 107 fpu_kern_leave(curthread, fpu_ctx_save); 108 109 return (c); 110 } 111 112 static int 113 nehemiah_modevent(module_t mod, int type, void *unused) 114 { 115 int error = 0; 116 117 switch (type) { 118 case MOD_LOAD: 119 if (via_feature_rng & VIA_HAS_RNG) { 120 random_source_register(&random_nehemiah); 121 printf("random: fast provider: \"%s\"\n", random_nehemiah.rs_ident); 122 random_nehemiah_init(); 123 } 124 break; 125 126 case MOD_UNLOAD: 127 if (via_feature_rng & VIA_HAS_RNG) { 128 random_nehemiah_deinit(); 129 random_source_deregister(&random_nehemiah); 130 } 131 break; 132 133 case MOD_SHUTDOWN: 134 break; 135 136 default: 137 error = EOPNOTSUPP; 138 break; 139 140 } 141 142 return (error); 143 } 144 145 static moduledata_t nehemiah_mod = { 146 "nehemiah", 147 nehemiah_modevent, 148 0 149 }; 150 151 DECLARE_MODULE(nehemiah, nehemiah_mod, SI_SUB_RANDOM, SI_ORDER_FOURTH); 152 MODULE_VERSION(nehemiah, 1); 153 MODULE_DEPEND(nehemiah, random_harvestq, 1, 1, 1); 154