FreeBSD kernel kern code
subr_prng.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright 2020 Conrad Meyer <cem@FreeBSD.org>. 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 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD$");
30
31#include <sys/param.h>
32#include <sys/kernel.h>
33#include <sys/pcpu.h>
34#include <sys/prng.h>
35#include <sys/smp.h>
36#include <sys/systm.h>
37
38#if !PCG_HAS_128BIT_OPS
39/* On 32-bit platforms, gang together two 32-bit generators. */
40typedef struct {
41 pcg32u_random_t states[2];
43
44static inline void
45pcg64u_srandom_r(pcg64u_random_t *state64, uint64_t seed)
46{
47 pcg32u_srandom_r(&state64->states[0], seed);
48 pcg32u_srandom_r(&state64->states[1], seed);
49}
50
51static inline uint64_t
53{
54 return ((((uint64_t)pcg32u_random_r(&state64->states[0])) << 32) |
55 pcg32u_random_r(&state64->states[1]));
56}
57
58static inline uint64_t
59pcg64u_boundedrand_r(pcg64u_random_t *state64, uint64_t bound)
60{
61 uint64_t threshold = -bound % bound;
62 for (;;) {
63 uint64_t r = pcg64u_random_r(state64);
64 if (r >= threshold)
65 return (r % bound);
66 }
67}
68#endif
69
70DPCPU_DEFINE_STATIC(pcg32u_random_t, pcpu_prng32_state);
72
73static void
74prng_init(void *dummy __unused)
75{
76 pcg32u_random_t *state;
77 pcg64u_random_t *state64;
78 int i;
79
80 CPU_FOREACH(i) {
81 state = DPCPU_ID_PTR(i, pcpu_prng32_state);
82 pcg32u_srandom_r(state, 1);
83 state64 = DPCPU_ID_PTR(i, pcpu_prng64_state);
84 pcg64u_srandom_r(state64, 1);
85 }
86}
87SYSINIT(prng_init, SI_SUB_CPU, SI_ORDER_ANY, prng_init, NULL);
88
89uint32_t
90prng32(void)
91{
92 uint32_t r;
93
94 critical_enter();
95 r = pcg32u_random_r(DPCPU_PTR(pcpu_prng32_state));
96 critical_exit();
97 return (r);
98}
99
100uint32_t
101prng32_bounded(uint32_t bound)
102{
103 uint32_t r;
104
105 critical_enter();
106 r = pcg32u_boundedrand_r(DPCPU_PTR(pcpu_prng32_state), bound);
107 critical_exit();
108 return (r);
109}
110
111uint64_t
113{
114 uint64_t r;
115
116 critical_enter();
117 r = pcg64u_random_r(DPCPU_PTR(pcpu_prng64_state));
118 critical_exit();
119 return (r);
120}
121
122uint64_t
123prng64_bounded(uint64_t bound)
124{
125 uint64_t r;
126
127 critical_enter();
128 r = pcg64u_boundedrand_r(DPCPU_PTR(pcpu_prng64_state), bound);
129 critical_exit();
130 return (r);
131}
pcg32u_random_t states[2]
Definition: subr_prng.c:41
uint64_t prng64_bounded(uint64_t bound)
Definition: subr_prng.c:123
uint32_t prng32_bounded(uint32_t bound)
Definition: subr_prng.c:101
uint64_t prng64(void)
Definition: subr_prng.c:112
uint32_t prng32(void)
Definition: subr_prng.c:90
static uint64_t pcg64u_random_r(pcg64u_random_t *state64)
Definition: subr_prng.c:52
__FBSDID("$FreeBSD$")
static uint64_t pcg64u_boundedrand_r(pcg64u_random_t *state64, uint64_t bound)
Definition: subr_prng.c:59
static void pcg64u_srandom_r(pcg64u_random_t *state64, uint64_t seed)
Definition: subr_prng.c:45
DPCPU_DEFINE_STATIC(pcg32u_random_t, pcpu_prng32_state)
SYSINIT(prng_init, SI_SUB_CPU, SI_ORDER_ANY, prng_init, NULL)
static void prng_init(void *dummy __unused)
Definition: subr_prng.c:74
static int dummy