]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/random/random_harvestq.c
Merge ^/vendor/compiler-rt/dist up to its last change, and resolve conflicts.
[FreeBSD/FreeBSD.git] / sys / dev / random / random_harvestq.c
1 /*-
2  * Copyright (c) 2017 Oliver Pinter
3  * Copyright (c) 2017 W. Dean Freeman
4  * Copyright (c) 2000-2015 Mark R V Murray
5  * Copyright (c) 2013 Arthur Mesh
6  * Copyright (c) 2004 Robert N. M. Watson
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer
14  *    in this position and unchanged.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/ck.h>
38 #include <sys/conf.h>
39 #include <sys/epoch.h>
40 #include <sys/eventhandler.h>
41 #include <sys/hash.h>
42 #include <sys/kernel.h>
43 #include <sys/kthread.h>
44 #include <sys/linker.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/random.h>
50 #include <sys/sbuf.h>
51 #include <sys/sysctl.h>
52 #include <sys/unistd.h>
53
54 #include <machine/atomic.h>
55 #include <machine/cpu.h>
56
57 #include <crypto/rijndael/rijndael-api-fst.h>
58 #include <crypto/sha2/sha256.h>
59
60 #include <dev/random/hash.h>
61 #include <dev/random/randomdev.h>
62 #include <dev/random/random_harvestq.h>
63
64 #if defined(RANDOM_ENABLE_ETHER)
65 #define _RANDOM_HARVEST_ETHER_OFF 0
66 #else
67 #define _RANDOM_HARVEST_ETHER_OFF (1u << RANDOM_NET_ETHER)
68 #endif
69 #if defined(RANDOM_ENABLE_UMA)
70 #define _RANDOM_HARVEST_UMA_OFF 0
71 #else
72 #define _RANDOM_HARVEST_UMA_OFF (1u << RANDOM_UMA)
73 #endif
74
75 static void random_kthread(void);
76 static void random_sources_feed(void);
77
78 static u_int read_rate;
79
80 /*
81  * Random must initialize much earlier than epoch, but we can initialize the
82  * epoch code before SMP starts.  Prior to SMP, we can safely bypass
83  * concurrency primitives.
84  */
85 static __read_mostly bool epoch_inited;
86 static __read_mostly epoch_t rs_epoch;
87
88 /*
89  * How many events to queue up. We create this many items in
90  * an 'empty' queue, then transfer them to the 'harvest' queue with
91  * supplied junk. When used, they are transferred back to the
92  * 'empty' queue.
93  */
94 #define RANDOM_RING_MAX         1024
95 #define RANDOM_ACCUM_MAX        8
96
97 /* 1 to let the kernel thread run, 0 to terminate, -1 to mark completion */
98 volatile int random_kthread_control;
99
100
101 /* Allow the sysadmin to select the broad category of
102  * entropy types to harvest.
103  */
104 __read_frequently u_int hc_source_mask;
105
106 struct random_sources {
107         CK_LIST_ENTRY(random_sources)    rrs_entries;
108         struct random_source            *rrs_source;
109 };
110
111 static CK_LIST_HEAD(sources_head, random_sources) source_list =
112     CK_LIST_HEAD_INITIALIZER(source_list);
113
114 SYSCTL_NODE(_kern_random, OID_AUTO, harvest, CTLFLAG_RW, 0,
115     "Entropy Device Parameters");
116
117 /*
118  * Put all the harvest queue context stuff in one place.
119  * this make is a bit easier to lock and protect.
120  */
121 static struct harvest_context {
122         /* The harvest mutex protects all of harvest_context and
123          * the related data.
124          */
125         struct mtx hc_mtx;
126         /* Round-robin destination cache. */
127         u_int hc_destination[ENTROPYSOURCE];
128         /* The context of the kernel thread processing harvested entropy */
129         struct proc *hc_kthread_proc;
130         /*
131          * Lockless ring buffer holding entropy events
132          * If ring.in == ring.out,
133          *     the buffer is empty.
134          * If ring.in != ring.out,
135          *     the buffer contains harvested entropy.
136          * If (ring.in + 1) == ring.out (mod RANDOM_RING_MAX),
137          *     the buffer is full.
138          *
139          * NOTE: ring.in points to the last added element,
140          * and ring.out points to the last consumed element.
141          *
142          * The ring.in variable needs locking as there are multiple
143          * sources to the ring. Only the sources may change ring.in,
144          * but the consumer may examine it.
145          *
146          * The ring.out variable does not need locking as there is
147          * only one consumer. Only the consumer may change ring.out,
148          * but the sources may examine it.
149          */
150         struct entropy_ring {
151                 struct harvest_event ring[RANDOM_RING_MAX];
152                 volatile u_int in;
153                 volatile u_int out;
154         } hc_entropy_ring;
155         struct fast_entropy_accumulator {
156                 volatile u_int pos;
157                 uint32_t buf[RANDOM_ACCUM_MAX];
158         } hc_entropy_fast_accumulator;
159 } harvest_context;
160
161 static struct kproc_desc random_proc_kp = {
162         "rand_harvestq",
163         random_kthread,
164         &harvest_context.hc_kthread_proc,
165 };
166
167 /* Pass the given event straight through to Fortuna/Whatever. */
168 static __inline void
169 random_harvestq_fast_process_event(struct harvest_event *event)
170 {
171         p_random_alg_context->ra_event_processor(event);
172         explicit_bzero(event, sizeof(*event));
173 }
174
175 static void
176 random_kthread(void)
177 {
178         u_int maxloop, ring_out, i;
179
180         /*
181          * Locking is not needed as this is the only place we modify ring.out, and
182          * we only examine ring.in without changing it. Both of these are volatile,
183          * and this is a unique thread.
184          */
185         for (random_kthread_control = 1; random_kthread_control;) {
186                 /* Deal with events, if any. Restrict the number we do in one go. */
187                 maxloop = RANDOM_RING_MAX;
188                 while (harvest_context.hc_entropy_ring.out != harvest_context.hc_entropy_ring.in) {
189                         ring_out = (harvest_context.hc_entropy_ring.out + 1)%RANDOM_RING_MAX;
190                         random_harvestq_fast_process_event(harvest_context.hc_entropy_ring.ring + ring_out);
191                         harvest_context.hc_entropy_ring.out = ring_out;
192                         if (!--maxloop)
193                                 break;
194                 }
195                 random_sources_feed();
196                 /* XXX: FIX!! Increase the high-performance data rate? Need some measurements first. */
197                 for (i = 0; i < RANDOM_ACCUM_MAX; i++) {
198                         if (harvest_context.hc_entropy_fast_accumulator.buf[i]) {
199                                 random_harvest_direct(harvest_context.hc_entropy_fast_accumulator.buf + i, sizeof(harvest_context.hc_entropy_fast_accumulator.buf[0]), RANDOM_UMA);
200                                 harvest_context.hc_entropy_fast_accumulator.buf[i] = 0;
201                         }
202                 }
203                 /* XXX: FIX!! This is a *great* place to pass hardware/live entropy to random(9) */
204                 tsleep_sbt(&harvest_context.hc_kthread_proc, 0, "-", SBT_1S/10, 0, C_PREL(1));
205         }
206         random_kthread_control = -1;
207         wakeup(&harvest_context.hc_kthread_proc);
208         kproc_exit(0);
209         /* NOTREACHED */
210 }
211 /* This happens well after SI_SUB_RANDOM */
212 SYSINIT(random_device_h_proc, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, kproc_start,
213     &random_proc_kp);
214
215 static void
216 rs_epoch_init(void *dummy __unused)
217 {
218         rs_epoch = epoch_alloc("Random Sources", EPOCH_PREEMPT);
219         epoch_inited = true;
220 }
221 SYSINIT(rs_epoch_init, SI_SUB_EPOCH, SI_ORDER_ANY, rs_epoch_init, NULL);
222
223 /*
224  * Run through all fast sources reading entropy for the given
225  * number of rounds, which should be a multiple of the number
226  * of entropy accumulation pools in use; it is 32 for Fortuna.
227  */
228 static void
229 random_sources_feed(void)
230 {
231         uint32_t entropy[HARVESTSIZE];
232         struct epoch_tracker et;
233         struct random_sources *rrs;
234         u_int i, n, local_read_rate;
235         bool rse_warm;
236
237         rse_warm = epoch_inited;
238
239         /*
240          * Step over all of live entropy sources, and feed their output
241          * to the system-wide RNG.
242          */
243         local_read_rate = atomic_readandclear_32(&read_rate);
244         /* Perform at least one read per round */
245         local_read_rate = MAX(local_read_rate, 1);
246         /* But not exceeding RANDOM_KEYSIZE_WORDS */
247         local_read_rate = MIN(local_read_rate, RANDOM_KEYSIZE_WORDS);
248         if (rse_warm)
249                 epoch_enter_preempt(rs_epoch, &et);
250         CK_LIST_FOREACH(rrs, &source_list, rrs_entries) {
251                 for (i = 0; i < p_random_alg_context->ra_poolcount*local_read_rate; i++) {
252                         n = rrs->rrs_source->rs_read(entropy, sizeof(entropy));
253                         KASSERT((n <= sizeof(entropy)), ("%s: rs_read returned too much data (%u > %zu)", __func__, n, sizeof(entropy)));
254                         /*
255                          * Sometimes the HW entropy source doesn't have anything
256                          * ready for us.  This isn't necessarily untrustworthy.
257                          * We don't perform any other verification of an entropy
258                          * source (i.e., length is allowed to be anywhere from 1
259                          * to sizeof(entropy), quality is unchecked, etc), so
260                          * don't balk verbosely at slow random sources either.
261                          * There are reports that RDSEED on x86 metal falls
262                          * behind the rate at which we query it, for example.
263                          * But it's still a better entropy source than RDRAND.
264                          */
265                         if (n == 0)
266                                 continue;
267                         random_harvest_direct(entropy, n, rrs->rrs_source->rs_source);
268                 }
269         }
270         if (rse_warm)
271                 epoch_exit_preempt(rs_epoch, &et);
272         explicit_bzero(entropy, sizeof(entropy));
273 }
274
275 void
276 read_rate_increment(u_int chunk)
277 {
278
279         atomic_add_32(&read_rate, chunk);
280 }
281
282 /* ARGSUSED */
283 static int
284 random_check_uint_harvestmask(SYSCTL_HANDLER_ARGS)
285 {
286         static const u_int user_immutable_mask =
287             (((1 << ENTROPYSOURCE) - 1) & (-1UL << RANDOM_PURE_START)) |
288             _RANDOM_HARVEST_ETHER_OFF | _RANDOM_HARVEST_UMA_OFF;
289
290         int error;
291         u_int value, orig_value;
292
293         orig_value = value = hc_source_mask;
294         error = sysctl_handle_int(oidp, &value, 0, req);
295         if (error != 0 || req->newptr == NULL)
296                 return (error);
297
298         if (flsl(value) > ENTROPYSOURCE)
299                 return (EINVAL);
300
301         /*
302          * Disallow userspace modification of pure entropy sources.
303          */
304         hc_source_mask = (value & ~user_immutable_mask) |
305             (orig_value & user_immutable_mask);
306         return (0);
307 }
308 SYSCTL_PROC(_kern_random_harvest, OID_AUTO, mask, CTLTYPE_UINT | CTLFLAG_RW,
309     NULL, 0, random_check_uint_harvestmask, "IU", "Entropy harvesting mask");
310
311 /* ARGSUSED */
312 static int
313 random_print_harvestmask(SYSCTL_HANDLER_ARGS)
314 {
315         struct sbuf sbuf;
316         int error, i;
317
318         error = sysctl_wire_old_buffer(req, 0);
319         if (error == 0) {
320                 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
321                 for (i = ENTROPYSOURCE - 1; i >= 0; i--)
322                         sbuf_cat(&sbuf, (hc_source_mask & (1 << i)) ? "1" : "0");
323                 error = sbuf_finish(&sbuf);
324                 sbuf_delete(&sbuf);
325         }
326         return (error);
327 }
328 SYSCTL_PROC(_kern_random_harvest, OID_AUTO, mask_bin,
329     CTLTYPE_STRING | CTLFLAG_RD, NULL, 0, random_print_harvestmask, "A",
330     "Entropy harvesting mask (printable)");
331
332 static const char *random_source_descr[ENTROPYSOURCE] = {
333         [RANDOM_CACHED] = "CACHED",
334         [RANDOM_ATTACH] = "ATTACH",
335         [RANDOM_KEYBOARD] = "KEYBOARD",
336         [RANDOM_MOUSE] = "MOUSE",
337         [RANDOM_NET_TUN] = "NET_TUN",
338         [RANDOM_NET_ETHER] = "NET_ETHER",
339         [RANDOM_NET_NG] = "NET_NG",
340         [RANDOM_INTERRUPT] = "INTERRUPT",
341         [RANDOM_SWI] = "SWI",
342         [RANDOM_FS_ATIME] = "FS_ATIME",
343         [RANDOM_UMA] = "UMA", /* ENVIRONMENTAL_END */
344         [RANDOM_PURE_OCTEON] = "PURE_OCTEON", /* PURE_START */
345         [RANDOM_PURE_SAFE] = "PURE_SAFE",
346         [RANDOM_PURE_GLXSB] = "PURE_GLXSB",
347         [RANDOM_PURE_UBSEC] = "PURE_UBSEC",
348         [RANDOM_PURE_HIFN] = "PURE_HIFN",
349         [RANDOM_PURE_RDRAND] = "PURE_RDRAND",
350         [RANDOM_PURE_NEHEMIAH] = "PURE_NEHEMIAH",
351         [RANDOM_PURE_RNDTEST] = "PURE_RNDTEST",
352         [RANDOM_PURE_VIRTIO] = "PURE_VIRTIO",
353         [RANDOM_PURE_BROADCOM] = "PURE_BROADCOM",
354         [RANDOM_PURE_CCP] = "PURE_CCP",
355         [RANDOM_PURE_DARN] = "PURE_DARN",
356         [RANDOM_PURE_TPM] = "PURE_TPM",
357         [RANDOM_PURE_VMGENID] = "VMGENID",
358         /* "ENTROPYSOURCE" */
359 };
360
361 /* ARGSUSED */
362 static int
363 random_print_harvestmask_symbolic(SYSCTL_HANDLER_ARGS)
364 {
365         struct sbuf sbuf;
366         int error, i;
367         bool first;
368
369         first = true;
370         error = sysctl_wire_old_buffer(req, 0);
371         if (error == 0) {
372                 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
373                 for (i = ENTROPYSOURCE - 1; i >= 0; i--) {
374                         if (i >= RANDOM_PURE_START &&
375                             (hc_source_mask & (1 << i)) == 0)
376                                 continue;
377                         if (!first)
378                                 sbuf_cat(&sbuf, ",");
379                         sbuf_cat(&sbuf, !(hc_source_mask & (1 << i)) ? "[" : "");
380                         sbuf_cat(&sbuf, random_source_descr[i]);
381                         sbuf_cat(&sbuf, !(hc_source_mask & (1 << i)) ? "]" : "");
382                         first = false;
383                 }
384                 error = sbuf_finish(&sbuf);
385                 sbuf_delete(&sbuf);
386         }
387         return (error);
388 }
389 SYSCTL_PROC(_kern_random_harvest, OID_AUTO, mask_symbolic,
390     CTLTYPE_STRING | CTLFLAG_RD, NULL, 0, random_print_harvestmask_symbolic,
391     "A", "Entropy harvesting mask (symbolic)");
392
393 /* ARGSUSED */
394 static void
395 random_harvestq_init(void *unused __unused)
396 {
397         static const u_int almost_everything_mask =
398             (((1 << (RANDOM_ENVIRONMENTAL_END + 1)) - 1) &
399             ~_RANDOM_HARVEST_ETHER_OFF & ~_RANDOM_HARVEST_UMA_OFF);
400
401         hc_source_mask = almost_everything_mask;
402         RANDOM_HARVEST_INIT_LOCK();
403         harvest_context.hc_entropy_ring.in = harvest_context.hc_entropy_ring.out = 0;
404 }
405 SYSINIT(random_device_h_init, SI_SUB_RANDOM, SI_ORDER_THIRD, random_harvestq_init, NULL);
406
407 /*
408  * Subroutine to slice up a contiguous chunk of 'entropy' and feed it into the
409  * underlying algorithm.  Returns number of bytes actually fed into underlying
410  * algorithm.
411  */
412 static size_t
413 random_early_prime(char *entropy, size_t len)
414 {
415         struct harvest_event event;
416         size_t i;
417
418         len = rounddown(len, sizeof(event.he_entropy));
419         if (len == 0)
420                 return (0);
421
422         for (i = 0; i < len; i += sizeof(event.he_entropy)) {
423                 event.he_somecounter = (uint32_t)get_cyclecount();
424                 event.he_size = sizeof(event.he_entropy);
425                 event.he_source = RANDOM_CACHED;
426                 event.he_destination =
427                     harvest_context.hc_destination[RANDOM_CACHED]++;
428                 memcpy(event.he_entropy, entropy + i, sizeof(event.he_entropy));
429                 random_harvestq_fast_process_event(&event);
430         }
431         explicit_bzero(entropy, len);
432         return (len);
433 }
434
435 /*
436  * Subroutine to search for known loader-loaded files in memory and feed them
437  * into the underlying algorithm early in boot.  Returns the number of bytes
438  * loaded (zero if none were loaded).
439  */
440 static size_t
441 random_prime_loader_file(const char *type)
442 {
443         uint8_t *keyfile, *data;
444         size_t size;
445
446         keyfile = preload_search_by_type(type);
447         if (keyfile == NULL)
448                 return (0);
449
450         data = preload_fetch_addr(keyfile);
451         size = preload_fetch_size(keyfile);
452         if (data == NULL)
453                 return (0);
454
455         return (random_early_prime(data, size));
456 }
457
458 /*
459  * This is used to prime the RNG by grabbing any early random stuff
460  * known to the kernel, and inserting it directly into the hashing
461  * module, currently Fortuna.
462  */
463 /* ARGSUSED */
464 static void
465 random_harvestq_prime(void *unused __unused)
466 {
467         size_t size;
468
469         /*
470          * Get entropy that may have been preloaded by loader(8)
471          * and use it to pre-charge the entropy harvest queue.
472          */
473         size = random_prime_loader_file(RANDOM_CACHED_BOOT_ENTROPY_MODULE);
474         if (bootverbose) {
475                 if (size > 0)
476                         printf("random: read %zu bytes from preloaded cache\n",
477                             size);
478                 else
479                         printf("random: no preloaded entropy cache\n");
480         }
481 }
482 SYSINIT(random_device_prime, SI_SUB_RANDOM, SI_ORDER_MIDDLE, random_harvestq_prime, NULL);
483
484 /* ARGSUSED */
485 static void
486 random_harvestq_deinit(void *unused __unused)
487 {
488
489         /* Command the hash/reseed thread to end and wait for it to finish */
490         random_kthread_control = 0;
491         while (random_kthread_control >= 0)
492                 tsleep(&harvest_context.hc_kthread_proc, 0, "harvqterm", hz/5);
493 }
494 SYSUNINIT(random_device_h_init, SI_SUB_RANDOM, SI_ORDER_THIRD, random_harvestq_deinit, NULL);
495
496 /*-
497  * Entropy harvesting queue routine.
498  *
499  * This is supposed to be fast; do not do anything slow in here!
500  * It is also illegal (and morally reprehensible) to insert any
501  * high-rate data here. "High-rate" is defined as a data source
502  * that will usually cause lots of failures of the "Lockless read"
503  * check a few lines below. This includes the "always-on" sources
504  * like the Intel "rdrand" or the VIA Nehamiah "xstore" sources.
505  */
506 /* XXXRW: get_cyclecount() is cheap on most modern hardware, where cycle
507  * counters are built in, but on older hardware it will do a real time clock
508  * read which can be quite expensive.
509  */
510 void
511 random_harvest_queue_(const void *entropy, u_int size, enum random_entropy_source origin)
512 {
513         struct harvest_event *event;
514         u_int ring_in;
515
516         KASSERT(origin >= RANDOM_START && origin < ENTROPYSOURCE, ("%s: origin %d invalid\n", __func__, origin));
517         RANDOM_HARVEST_LOCK();
518         ring_in = (harvest_context.hc_entropy_ring.in + 1)%RANDOM_RING_MAX;
519         if (ring_in != harvest_context.hc_entropy_ring.out) {
520                 /* The ring is not full */
521                 event = harvest_context.hc_entropy_ring.ring + ring_in;
522                 event->he_somecounter = (uint32_t)get_cyclecount();
523                 event->he_source = origin;
524                 event->he_destination = harvest_context.hc_destination[origin]++;
525                 if (size <= sizeof(event->he_entropy)) {
526                         event->he_size = size;
527                         memcpy(event->he_entropy, entropy, size);
528                 }
529                 else {
530                         /* Big event, so squash it */
531                         event->he_size = sizeof(event->he_entropy[0]);
532                         event->he_entropy[0] = jenkins_hash(entropy, size, (uint32_t)(uintptr_t)event);
533                 }
534                 harvest_context.hc_entropy_ring.in = ring_in;
535         }
536         RANDOM_HARVEST_UNLOCK();
537 }
538
539 /*-
540  * Entropy harvesting fast routine.
541  *
542  * This is supposed to be very fast; do not do anything slow in here!
543  * This is the right place for high-rate harvested data.
544  */
545 void
546 random_harvest_fast_(const void *entropy, u_int size)
547 {
548         u_int pos;
549
550         pos = harvest_context.hc_entropy_fast_accumulator.pos;
551         harvest_context.hc_entropy_fast_accumulator.buf[pos] ^= jenkins_hash(entropy, size, (uint32_t)get_cyclecount());
552         harvest_context.hc_entropy_fast_accumulator.pos = (pos + 1)%RANDOM_ACCUM_MAX;
553 }
554
555 /*-
556  * Entropy harvesting direct routine.
557  *
558  * This is not supposed to be fast, but will only be used during
559  * (e.g.) booting when initial entropy is being gathered.
560  */
561 void
562 random_harvest_direct_(const void *entropy, u_int size, enum random_entropy_source origin)
563 {
564         struct harvest_event event;
565
566         KASSERT(origin >= RANDOM_START && origin < ENTROPYSOURCE, ("%s: origin %d invalid\n", __func__, origin));
567         size = MIN(size, sizeof(event.he_entropy));
568         event.he_somecounter = (uint32_t)get_cyclecount();
569         event.he_size = size;
570         event.he_source = origin;
571         event.he_destination = harvest_context.hc_destination[origin]++;
572         memcpy(event.he_entropy, entropy, size);
573         random_harvestq_fast_process_event(&event);
574 }
575
576 void
577 random_harvest_register_source(enum random_entropy_source source)
578 {
579
580         hc_source_mask |= (1 << source);
581 }
582
583 void
584 random_harvest_deregister_source(enum random_entropy_source source)
585 {
586
587         hc_source_mask &= ~(1 << source);
588 }
589
590 void
591 random_source_register(struct random_source *rsource)
592 {
593         struct random_sources *rrs;
594
595         KASSERT(rsource != NULL, ("invalid input to %s", __func__));
596
597         rrs = malloc(sizeof(*rrs), M_ENTROPY, M_WAITOK);
598         rrs->rrs_source = rsource;
599
600         random_harvest_register_source(rsource->rs_source);
601
602         printf("random: registering fast source %s\n", rsource->rs_ident);
603
604         RANDOM_HARVEST_LOCK();
605         CK_LIST_INSERT_HEAD(&source_list, rrs, rrs_entries);
606         RANDOM_HARVEST_UNLOCK();
607 }
608
609 void
610 random_source_deregister(struct random_source *rsource)
611 {
612         struct random_sources *rrs = NULL;
613
614         KASSERT(rsource != NULL, ("invalid input to %s", __func__));
615
616         random_harvest_deregister_source(rsource->rs_source);
617
618         RANDOM_HARVEST_LOCK();
619         CK_LIST_FOREACH(rrs, &source_list, rrs_entries)
620                 if (rrs->rrs_source == rsource) {
621                         CK_LIST_REMOVE(rrs, rrs_entries);
622                         break;
623                 }
624         RANDOM_HARVEST_UNLOCK();
625
626         if (rrs != NULL && epoch_inited)
627                 epoch_wait_preempt(rs_epoch);
628         free(rrs, M_ENTROPY);
629 }
630
631 static int
632 random_source_handler(SYSCTL_HANDLER_ARGS)
633 {
634         struct epoch_tracker et;
635         struct random_sources *rrs;
636         struct sbuf sbuf;
637         int error, count;
638
639         error = sysctl_wire_old_buffer(req, 0);
640         if (error != 0)
641                 return (error);
642
643         sbuf_new_for_sysctl(&sbuf, NULL, 64, req);
644         count = 0;
645         epoch_enter_preempt(rs_epoch, &et);
646         CK_LIST_FOREACH(rrs, &source_list, rrs_entries) {
647                 sbuf_cat(&sbuf, (count++ ? ",'" : "'"));
648                 sbuf_cat(&sbuf, rrs->rrs_source->rs_ident);
649                 sbuf_cat(&sbuf, "'");
650         }
651         epoch_exit_preempt(rs_epoch, &et);
652         error = sbuf_finish(&sbuf);
653         sbuf_delete(&sbuf);
654         return (error);
655 }
656 SYSCTL_PROC(_kern_random, OID_AUTO, random_sources, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
657             NULL, 0, random_source_handler, "A",
658             "List of active fast entropy sources.");
659
660 MODULE_VERSION(random_harvestq, 1);