| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: kern_rndq.c,v 1.11 2013/06/13 00:55:01 tls Exp $ */ | | 1 | /* $NetBSD: kern_rndq.c,v 1.12 2013/06/13 01:37:03 tls Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 1997-2013 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 1997-2013 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Michael Graff <explorer@flame.org> and Thor Lancelot Simon. | | 8 | * by Michael Graff <explorer@flame.org> and Thor Lancelot Simon. |
9 | * This code uses ideas and algorithms from the Linux driver written by | | 9 | * This code uses ideas and algorithms from the Linux driver written by |
10 | * Ted Ts'o. | | 10 | * Ted Ts'o. |
11 | * | | 11 | * |
12 | * Redistribution and use in source and binary forms, with or without | | 12 | * Redistribution and use in source and binary forms, with or without |
13 | * modification, are permitted provided that the following conditions | | 13 | * modification, are permitted provided that the following conditions |
14 | * are met: | | 14 | * are met: |
| @@ -22,27 +22,27 @@ | | | @@ -22,27 +22,27 @@ |
22 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 22 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
23 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 23 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
31 | * POSSIBILITY OF SUCH DAMAGE. | | 31 | * POSSIBILITY OF SUCH DAMAGE. |
32 | */ | | 32 | */ |
33 | | | 33 | |
34 | #include <sys/cdefs.h> | | 34 | #include <sys/cdefs.h> |
35 | __KERNEL_RCSID(0, "$NetBSD: kern_rndq.c,v 1.11 2013/06/13 00:55:01 tls Exp $"); | | 35 | __KERNEL_RCSID(0, "$NetBSD: kern_rndq.c,v 1.12 2013/06/13 01:37:03 tls Exp $"); |
36 | | | 36 | |
37 | #include <sys/param.h> | | 37 | #include <sys/param.h> |
38 | #include <sys/ioctl.h> | | 38 | #include <sys/ioctl.h> |
39 | #include <sys/fcntl.h> | | 39 | #include <sys/fcntl.h> |
40 | #include <sys/select.h> | | 40 | #include <sys/select.h> |
41 | #include <sys/poll.h> | | 41 | #include <sys/poll.h> |
42 | #include <sys/kmem.h> | | 42 | #include <sys/kmem.h> |
43 | #include <sys/mutex.h> | | 43 | #include <sys/mutex.h> |
44 | #include <sys/proc.h> | | 44 | #include <sys/proc.h> |
45 | #include <sys/kernel.h> | | 45 | #include <sys/kernel.h> |
46 | #include <sys/conf.h> | | 46 | #include <sys/conf.h> |
47 | #include <sys/systm.h> | | 47 | #include <sys/systm.h> |
48 | #include <sys/callout.h> | | 48 | #include <sys/callout.h> |
| @@ -94,31 +94,26 @@ typedef struct _rnd_sample_t { | | | @@ -94,31 +94,26 @@ typedef struct _rnd_sample_t { |
94 | int entropy; | | 94 | int entropy; |
95 | u_int32_t ts[RND_SAMPLE_COUNT]; | | 95 | u_int32_t ts[RND_SAMPLE_COUNT]; |
96 | u_int32_t values[RND_SAMPLE_COUNT]; | | 96 | u_int32_t values[RND_SAMPLE_COUNT]; |
97 | } rnd_sample_t; | | 97 | } rnd_sample_t; |
98 | | | 98 | |
99 | /* | | 99 | /* |
100 | * The event queue. Fields are altered at an interrupt level. | | 100 | * The event queue. Fields are altered at an interrupt level. |
101 | * All accesses must be protected with the mutex. | | 101 | * All accesses must be protected with the mutex. |
102 | */ | | 102 | */ |
103 | SIMPLEQ_HEAD(, _rnd_sample_t) rnd_samples; | | 103 | SIMPLEQ_HEAD(, _rnd_sample_t) rnd_samples; |
104 | kmutex_t rnd_mtx; | | 104 | kmutex_t rnd_mtx; |
105 | | | 105 | |
106 | /* | | 106 | /* |
107 | * This lock protects dispatch of our soft interrupts. | | | |
108 | */ | | | |
109 | kmutex_t rndsoft_mtx; | | | |
110 | | | | |
111 | /* | | | |
112 | * Entropy sinks: usually other generators waiting to be rekeyed. | | 107 | * Entropy sinks: usually other generators waiting to be rekeyed. |
113 | * | | 108 | * |
114 | * A sink's callback MUST NOT re-add the sink to the list, or | | 109 | * A sink's callback MUST NOT re-add the sink to the list, or |
115 | * list corruption will occur. The list is protected by the | | 110 | * list corruption will occur. The list is protected by the |
116 | * rndsink_mtx, which must be released before calling any sink's | | 111 | * rndsink_mtx, which must be released before calling any sink's |
117 | * callback. | | 112 | * callback. |
118 | */ | | 113 | */ |
119 | TAILQ_HEAD(, rndsink) rnd_sinks; | | 114 | TAILQ_HEAD(, rndsink) rnd_sinks; |
120 | kmutex_t rndsink_mtx; | | 115 | kmutex_t rndsink_mtx; |
121 | | | 116 | |
122 | /* | | 117 | /* |
123 | * Memory pool for sample buffers | | 118 | * Memory pool for sample buffers |
124 | */ | | 119 | */ |
| @@ -197,29 +192,29 @@ rnd_counter(void) | | | @@ -197,29 +192,29 @@ rnd_counter(void) |
197 | return (tv.tv_sec * 1000000 + tv.tv_usec); | | 192 | return (tv.tv_sec * 1000000 + tv.tv_usec); |
198 | } | | 193 | } |
199 | /* when called from rnd_init, its too early to call microtime safely */ | | 194 | /* when called from rnd_init, its too early to call microtime safely */ |
200 | return (0); | | 195 | return (0); |
201 | } | | 196 | } |
202 | | | 197 | |
203 | /* | | 198 | /* |
204 | * We may be called from low IPL -- protect our softint. | | 199 | * We may be called from low IPL -- protect our softint. |
205 | */ | | 200 | */ |
206 | | | 201 | |
207 | static inline void | | 202 | static inline void |
208 | rnd_schedule_softint(void *softint) | | 203 | rnd_schedule_softint(void *softint) |
209 | { | | 204 | { |
210 | mutex_spin_enter(&rndsoft_mtx); | | 205 | kpreempt_disable(); |
211 | softint_schedule(softint); | | 206 | softint_schedule(softint); |
212 | mutex_spin_exit(&rndsoft_mtx); | | 207 | kpreempt_enable(); |
213 | } | | 208 | } |
214 | | | 209 | |
215 | /* | | 210 | /* |
216 | * XXX repulsive: we can't initialize our softints in rnd_init | | 211 | * XXX repulsive: we can't initialize our softints in rnd_init |
217 | * XXX (too early) so we wrap the points where we'd schedule them, thus. | | 212 | * XXX (too early) so we wrap the points where we'd schedule them, thus. |
218 | */ | | 213 | */ |
219 | static inline void | | 214 | static inline void |
220 | rnd_schedule_process(void) | | 215 | rnd_schedule_process(void) |
221 | { | | 216 | { |
222 | if (__predict_true(rnd_process)) { | | 217 | if (__predict_true(rnd_process)) { |
223 | rnd_schedule_softint(rnd_process); | | 218 | rnd_schedule_softint(rnd_process); |
224 | return; | | 219 | return; |
225 | } | | 220 | } |
| @@ -443,27 +438,26 @@ rnd_skew(void *arg) | | | @@ -443,27 +438,26 @@ rnd_skew(void *arg) |
443 | * rnd_init() must be called very early on in the boot process, so | | 438 | * rnd_init() must be called very early on in the boot process, so |
444 | * the pool is ready for other devices to attach as sources. | | 439 | * the pool is ready for other devices to attach as sources. |
445 | */ | | 440 | */ |
446 | void | | 441 | void |
447 | rnd_init(void) | | 442 | rnd_init(void) |
448 | { | | 443 | { |
449 | u_int32_t c; | | 444 | u_int32_t c; |
450 | | | 445 | |
451 | if (rnd_ready) | | 446 | if (rnd_ready) |
452 | return; | | 447 | return; |
453 | | | 448 | |
454 | mutex_init(&rnd_mtx, MUTEX_DEFAULT, IPL_VM); | | 449 | mutex_init(&rnd_mtx, MUTEX_DEFAULT, IPL_VM); |
455 | mutex_init(&rndsink_mtx, MUTEX_DEFAULT, IPL_VM); | | 450 | mutex_init(&rndsink_mtx, MUTEX_DEFAULT, IPL_VM); |
456 | mutex_init(&rndsoft_mtx, MUTEX_DEFAULT, IPL_VM); | | | |
457 | | | 451 | |
458 | /* | | 452 | /* |
459 | * take a counter early, hoping that there's some variance in | | 453 | * take a counter early, hoping that there's some variance in |
460 | * the following operations | | 454 | * the following operations |
461 | */ | | 455 | */ |
462 | c = rnd_counter(); | | 456 | c = rnd_counter(); |
463 | | | 457 | |
464 | LIST_INIT(&rnd_sources); | | 458 | LIST_INIT(&rnd_sources); |
465 | SIMPLEQ_INIT(&rnd_samples); | | 459 | SIMPLEQ_INIT(&rnd_samples); |
466 | TAILQ_INIT(&rnd_sinks); | | 460 | TAILQ_INIT(&rnd_sinks); |
467 | | | 461 | |
468 | rndpool_init(&rnd_pool); | | 462 | rndpool_init(&rnd_pool); |
469 | mutex_init(&rndpool_mtx, MUTEX_DEFAULT, IPL_VM); | | 463 | mutex_init(&rndpool_mtx, MUTEX_DEFAULT, IPL_VM); |