| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: kern_rndq.c,v 1.49 2015/04/14 12:25:41 riastradh Exp $ */ | | 1 | /* $NetBSD: kern_rndq.c,v 1.50 2015/04/14 12:28:12 riastradh 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.49 2015/04/14 12:25:41 riastradh Exp $"); | | 35 | __KERNEL_RCSID(0, "$NetBSD: kern_rndq.c,v 1.50 2015/04/14 12:28:12 riastradh Exp $"); |
36 | | | 36 | |
37 | #include <sys/param.h> | | 37 | #include <sys/param.h> |
38 | #include <sys/atomic.h> | | 38 | #include <sys/atomic.h> |
39 | #include <sys/ioctl.h> | | 39 | #include <sys/ioctl.h> |
40 | #include <sys/fcntl.h> | | 40 | #include <sys/fcntl.h> |
41 | #include <sys/select.h> | | 41 | #include <sys/select.h> |
42 | #include <sys/poll.h> | | 42 | #include <sys/poll.h> |
43 | #include <sys/kmem.h> | | 43 | #include <sys/kmem.h> |
44 | #include <sys/mutex.h> | | 44 | #include <sys/mutex.h> |
45 | #include <sys/proc.h> | | 45 | #include <sys/proc.h> |
46 | #include <sys/kernel.h> | | 46 | #include <sys/kernel.h> |
47 | #include <sys/conf.h> | | 47 | #include <sys/conf.h> |
48 | #include <sys/systm.h> | | 48 | #include <sys/systm.h> |
| @@ -91,27 +91,27 @@ static unsigned int deltacnt; | | | @@ -91,27 +91,27 @@ static unsigned int deltacnt; |
91 | /* | | 91 | /* |
92 | * This is a little bit of state information attached to each device that we | | 92 | * This is a little bit of state information attached to each device that we |
93 | * collect entropy from. This is simply a collection buffer, and when it | | 93 | * collect entropy from. This is simply a collection buffer, and when it |
94 | * is full it will be "detached" from the source and added to the entropy | | 94 | * is full it will be "detached" from the source and added to the entropy |
95 | * pool after entropy is distilled as much as possible. | | 95 | * pool after entropy is distilled as much as possible. |
96 | */ | | 96 | */ |
97 | #define RND_SAMPLE_COUNT 64 /* collect N samples, then compress */ | | 97 | #define RND_SAMPLE_COUNT 64 /* collect N samples, then compress */ |
98 | typedef struct _rnd_sample_t { | | 98 | typedef struct _rnd_sample_t { |
99 | SIMPLEQ_ENTRY(_rnd_sample_t) next; | | 99 | SIMPLEQ_ENTRY(_rnd_sample_t) next; |
100 | krndsource_t *source; | | 100 | krndsource_t *source; |
101 | int cursor; | | 101 | int cursor; |
102 | int entropy; | | 102 | int entropy; |
103 | uint32_t ts[RND_SAMPLE_COUNT]; | | 103 | uint32_t ts[RND_SAMPLE_COUNT]; |
104 | u_int32_t values[RND_SAMPLE_COUNT]; | | 104 | uint32_t values[RND_SAMPLE_COUNT]; |
105 | } rnd_sample_t; | | 105 | } rnd_sample_t; |
106 | | | 106 | |
107 | SIMPLEQ_HEAD(rnd_sampleq, _rnd_sample_t); | | 107 | SIMPLEQ_HEAD(rnd_sampleq, _rnd_sample_t); |
108 | | | 108 | |
109 | /* | | 109 | /* |
110 | * The sample queue. Samples are put into the queue and processed in a | | 110 | * The sample queue. Samples are put into the queue and processed in a |
111 | * softint in order to limit the latency of adding a sample. | | 111 | * softint in order to limit the latency of adding a sample. |
112 | */ | | 112 | */ |
113 | static struct { | | 113 | static struct { |
114 | kmutex_t lock; | | 114 | kmutex_t lock; |
115 | struct rnd_sampleq q; | | 115 | struct rnd_sampleq q; |
116 | } rnd_samples __cacheline_aligned; | | 116 | } rnd_samples __cacheline_aligned; |
117 | | | 117 | |
| @@ -837,28 +837,28 @@ rnd_add_data(krndsource_t *rs, const voi | | | @@ -837,28 +837,28 @@ rnd_add_data(krndsource_t *rs, const voi |
837 | * itself, random. Don't estimate entropy based on | | 837 | * itself, random. Don't estimate entropy based on |
838 | * timestamp, just directly add the data. | | 838 | * timestamp, just directly add the data. |
839 | */ | | 839 | */ |
840 | if (__predict_false(rs == NULL)) { | | 840 | if (__predict_false(rs == NULL)) { |
841 | mutex_spin_enter(&rndpool_mtx); | | 841 | mutex_spin_enter(&rndpool_mtx); |
842 | rndpool_add_data(&rnd_pool, data, len, entropy); | | 842 | rndpool_add_data(&rnd_pool, data, len, entropy); |
843 | mutex_spin_exit(&rndpool_mtx); | | 843 | mutex_spin_exit(&rndpool_mtx); |
844 | } else { | | 844 | } else { |
845 | rnd_add_data_ts(rs, data, len, entropy, rnd_counter()); | | 845 | rnd_add_data_ts(rs, data, len, entropy, rnd_counter()); |
846 | } | | 846 | } |
847 | } | | 847 | } |
848 | | | 848 | |
849 | static void | | 849 | static void |
850 | rnd_add_data_ts(krndsource_t *rs, const void *const data, u_int32_t len, | | 850 | rnd_add_data_ts(krndsource_t *rs, const void *const data, uint32_t len, |
851 | u_int32_t entropy, uint32_t ts) | | 851 | uint32_t entropy, uint32_t ts) |
852 | { | | 852 | { |
853 | rnd_sample_t *state = NULL; | | 853 | rnd_sample_t *state = NULL; |
854 | const uint8_t *p = data; | | 854 | const uint8_t *p = data; |
855 | uint32_t dint; | | 855 | uint32_t dint; |
856 | int todo, done, filled = 0; | | 856 | int todo, done, filled = 0; |
857 | int sample_count; | | 857 | int sample_count; |
858 | struct rnd_sampleq tmp_samples = SIMPLEQ_HEAD_INITIALIZER(tmp_samples); | | 858 | struct rnd_sampleq tmp_samples = SIMPLEQ_HEAD_INITIALIZER(tmp_samples); |
859 | | | 859 | |
860 | if (rs && (rs->flags & RND_FLAG_NO_COLLECT || | | 860 | if (rs && (rs->flags & RND_FLAG_NO_COLLECT || |
861 | __predict_false(!(rs->flags & | | 861 | __predict_false(!(rs->flags & |
862 | (RND_FLAG_COLLECT_TIME| | | 862 | (RND_FLAG_COLLECT_TIME| |
863 | RND_FLAG_COLLECT_VALUE))))) { | | 863 | RND_FLAG_COLLECT_VALUE))))) { |
864 | return; | | 864 | return; |
| @@ -1012,27 +1012,27 @@ rnd_hwrng_test(rnd_sample_t *sample) | | | @@ -1012,27 +1012,27 @@ rnd_hwrng_test(rnd_sample_t *sample) |
1012 | | | 1012 | |
1013 | /* | | 1013 | /* |
1014 | * Process the events in the ring buffer. Called by rnd_timeout or | | 1014 | * Process the events in the ring buffer. Called by rnd_timeout or |
1015 | * by the add routines directly if the callout has never fired (that | | 1015 | * by the add routines directly if the callout has never fired (that |
1016 | * is, if we are "cold" -- just booted). | | 1016 | * is, if we are "cold" -- just booted). |
1017 | * | | 1017 | * |
1018 | */ | | 1018 | */ |
1019 | static void | | 1019 | static void |
1020 | rnd_process_events(void) | | 1020 | rnd_process_events(void) |
1021 | { | | 1021 | { |
1022 | rnd_sample_t *sample = NULL; | | 1022 | rnd_sample_t *sample = NULL; |
1023 | krndsource_t *source, *badsource = NULL; | | 1023 | krndsource_t *source, *badsource = NULL; |
1024 | static krndsource_t *last_source; | | 1024 | static krndsource_t *last_source; |
1025 | u_int32_t entropy; | | 1025 | uint32_t entropy; |
1026 | size_t pool_entropy; | | 1026 | size_t pool_entropy; |
1027 | int found = 0, wake = 0; | | 1027 | int found = 0, wake = 0; |
1028 | struct rnd_sampleq dq_samples = SIMPLEQ_HEAD_INITIALIZER(dq_samples); | | 1028 | struct rnd_sampleq dq_samples = SIMPLEQ_HEAD_INITIALIZER(dq_samples); |
1029 | struct rnd_sampleq df_samples = SIMPLEQ_HEAD_INITIALIZER(df_samples); | | 1029 | struct rnd_sampleq df_samples = SIMPLEQ_HEAD_INITIALIZER(df_samples); |
1030 | | | 1030 | |
1031 | /* | | 1031 | /* |
1032 | * Drain to the on-stack queue and drop the lock. | | 1032 | * Drain to the on-stack queue and drop the lock. |
1033 | */ | | 1033 | */ |
1034 | mutex_spin_enter(&rnd_samples.lock); | | 1034 | mutex_spin_enter(&rnd_samples.lock); |
1035 | while ((sample = SIMPLEQ_FIRST(&rnd_samples.q))) { | | 1035 | while ((sample = SIMPLEQ_FIRST(&rnd_samples.q))) { |
1036 | found++; | | 1036 | found++; |
1037 | SIMPLEQ_REMOVE_HEAD(&rnd_samples.q, next); | | 1037 | SIMPLEQ_REMOVE_HEAD(&rnd_samples.q, next); |
1038 | /* | | 1038 | /* |
| @@ -1139,27 +1139,27 @@ skip: SIMPLEQ_INSERT_TAIL(&df_samples, | | | @@ -1139,27 +1139,27 @@ skip: SIMPLEQ_INSERT_TAIL(&df_samples, |
1139 | static void | | 1139 | static void |
1140 | rnd_intr(void *arg) | | 1140 | rnd_intr(void *arg) |
1141 | { | | 1141 | { |
1142 | rnd_process_events(); | | 1142 | rnd_process_events(); |
1143 | } | | 1143 | } |
1144 | | | 1144 | |
1145 | static void | | 1145 | static void |
1146 | rnd_wake(void *arg) | | 1146 | rnd_wake(void *arg) |
1147 | { | | 1147 | { |
1148 | rnd_wakeup_readers(); | | 1148 | rnd_wakeup_readers(); |
1149 | } | | 1149 | } |
1150 | | | 1150 | |
1151 | static uint32_t | | 1151 | static uint32_t |
1152 | rnd_extract_data(void *p, u_int32_t len, u_int32_t flags) | | 1152 | rnd_extract_data(void *p, uint32_t len, uint32_t flags) |
1153 | { | | 1153 | { |
1154 | static int timed_in; | | 1154 | static int timed_in; |
1155 | int entropy_count; | | 1155 | int entropy_count; |
1156 | uint32_t retval; | | 1156 | uint32_t retval; |
1157 | | | 1157 | |
1158 | mutex_spin_enter(&rndpool_mtx); | | 1158 | mutex_spin_enter(&rndpool_mtx); |
1159 | if (__predict_false(!timed_in)) { | | 1159 | if (__predict_false(!timed_in)) { |
1160 | if (boottime.tv_sec) { | | 1160 | if (boottime.tv_sec) { |
1161 | rndpool_add_data(&rnd_pool, &boottime, | | 1161 | rndpool_add_data(&rnd_pool, &boottime, |
1162 | sizeof(boottime), 0); | | 1162 | sizeof(boottime), 0); |
1163 | } | | 1163 | } |
1164 | timed_in++; | | 1164 | timed_in++; |
1165 | } | | 1165 | } |