The branch main has been updated by markj:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=e2a96b83404fcee0a079a3c3adbb448b86a38d1e

commit e2a96b83404fcee0a079a3c3adbb448b86a38d1e
Author:     Mark Johnston <ma...@freebsd.org>
AuthorDate: 2025-07-03 17:27:47 +0000
Commit:     Mark Johnston <ma...@freebsd.org>
CommitDate: 2025-07-03 17:31:42 +0000

    random: Define a macro for getting the CPU cycle count
    
    Entropy queue entries always include the low 32 bits of a CPU cycle
    count reading.  Introduce a macro for this instead of hard-coding
    get_cyclecount() calls everywhere; this is handy for testing purposes
    since this way, random(4)'s use of the cycle counter (e.g., the number
    of bits we use) can be changed in one place.
    
    No functional change intended.
    
    Reviewed by:    cem, delphij
    MFC after:      1 week
    Sponsored by:   Stormshield
    Sponsored by:   Klara, Inc.
    Differential Revision:  https://reviews.freebsd.org/D51113
---
 sys/dev/random/random_harvestq.c | 9 +++++----
 sys/dev/random/random_harvestq.h | 9 +++++++++
 sys/dev/random/randomdev.c       | 6 +++---
 3 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c
index 1a8cbd9dcad7..ef12fe2d2e44 100644
--- a/sys/dev/random/random_harvestq.c
+++ b/sys/dev/random/random_harvestq.c
@@ -459,7 +459,7 @@ random_early_prime(char *entropy, size_t len)
                return (0);
 
        for (i = 0; i < len; i += sizeof(event.he_entropy)) {
-               event.he_somecounter = (uint32_t)get_cyclecount();
+               event.he_somecounter = random_get_cyclecount();
                event.he_size = sizeof(event.he_entropy);
                event.he_source = RANDOM_CACHED;
                event.he_destination =
@@ -566,7 +566,7 @@ random_harvest_queue_(const void *entropy, u_int size, enum 
random_entropy_sourc
        if (ring_in != harvest_context.hc_entropy_ring.out) {
                /* The ring is not full */
                event = harvest_context.hc_entropy_ring.ring + ring_in;
-               event->he_somecounter = (uint32_t)get_cyclecount();
+               event->he_somecounter = random_get_cyclecount();
                event->he_source = origin;
                event->he_destination = 
harvest_context.hc_destination[origin]++;
                if (size <= sizeof(event->he_entropy)) {
@@ -595,7 +595,8 @@ random_harvest_fast_(const void *entropy, u_int size)
        u_int pos;
 
        pos = harvest_context.hc_entropy_fast_accumulator.pos;
-       harvest_context.hc_entropy_fast_accumulator.buf[pos] ^= 
jenkins_hash(entropy, size, (uint32_t)get_cyclecount());
+       harvest_context.hc_entropy_fast_accumulator.buf[pos] ^=
+           jenkins_hash(entropy, size, random_get_cyclecount());
        harvest_context.hc_entropy_fast_accumulator.pos = (pos + 
1)%RANDOM_ACCUM_MAX;
 }
 
@@ -612,7 +613,7 @@ random_harvest_direct_(const void *entropy, u_int size, 
enum random_entropy_sour
 
        KASSERT(origin >= RANDOM_START && origin < ENTROPYSOURCE, ("%s: origin 
%d invalid\n", __func__, origin));
        size = MIN(size, sizeof(event.he_entropy));
-       event.he_somecounter = (uint32_t)get_cyclecount();
+       event.he_somecounter = random_get_cyclecount();
        event.he_size = size;
        event.he_source = origin;
        event.he_destination = harvest_context.hc_destination[origin]++;
diff --git a/sys/dev/random/random_harvestq.h b/sys/dev/random/random_harvestq.h
index edeb4ff7a17f..7804bf52aa4f 100644
--- a/sys/dev/random/random_harvestq.h
+++ b/sys/dev/random/random_harvestq.h
@@ -27,6 +27,9 @@
 #ifndef SYS_DEV_RANDOM_RANDOM_HARVESTQ_H_INCLUDED
 #define        SYS_DEV_RANDOM_RANDOM_HARVESTQ_H_INCLUDED
 
+#include <sys/types.h>
+#include <machine/cpu.h>
+
 #define        HARVESTSIZE     2       /* Max length in words of each 
harvested entropy unit */
 
 /* These are used to queue harvested packets of entropy. The entropy
@@ -40,4 +43,10 @@ struct harvest_event {
        uint8_t         he_source;              /* origin of the entropy */
 };
 
+static inline uint32_t
+random_get_cyclecount(void)
+{
+       return ((uint32_t)get_cyclecount());
+}
+
 #endif /* SYS_DEV_RANDOM_RANDOM_HARVESTQ_H_INCLUDED */
diff --git a/sys/dev/random/randomdev.c b/sys/dev/random/randomdev.c
index 6d637ab5a53e..9d1c7b1167c8 100644
--- a/sys/dev/random/randomdev.c
+++ b/sys/dev/random/randomdev.c
@@ -303,14 +303,14 @@ randomdev_accumulate(uint8_t *buf, u_int count)
 
        /* Extra timing here is helpful to scrape scheduler jitter entropy */
        randomdev_hash_init(&hash);
-       timestamp = (uint32_t)get_cyclecount();
+       timestamp = random_get_cyclecount();
        randomdev_hash_iterate(&hash, &timestamp, sizeof(timestamp));
        randomdev_hash_iterate(&hash, buf, count);
-       timestamp = (uint32_t)get_cyclecount();
+       timestamp = random_get_cyclecount();
        randomdev_hash_iterate(&hash, &timestamp, sizeof(timestamp));
        randomdev_hash_finish(&hash, entropy_data);
        for (i = 0; i < RANDOM_KEYSIZE_WORDS; i += 
sizeof(event.he_entropy)/sizeof(event.he_entropy[0])) {
-               event.he_somecounter = (uint32_t)get_cyclecount();
+               event.he_somecounter = random_get_cyclecount();
                event.he_size = sizeof(event.he_entropy);
                event.he_source = RANDOM_CACHED;
                event.he_destination = destination++; /* Harmless cheating */

Reply via email to