Changeset View
Changeset View
Standalone View
Standalone View
sys/dev/random/random_harvestq.c
Show First 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | |||||
* 'empty' queue. | * 'empty' queue. | ||||
*/ | */ | ||||
#define RANDOM_RING_MAX 1024 | #define RANDOM_RING_MAX 1024 | ||||
#define RANDOM_ACCUM_MAX 8 | #define RANDOM_ACCUM_MAX 8 | ||||
/* 1 to let the kernel thread run, 0 to terminate, -1 to mark completion */ | /* 1 to let the kernel thread run, 0 to terminate, -1 to mark completion */ | ||||
volatile int random_kthread_control; | volatile int random_kthread_control; | ||||
/* Allow the sysadmin to select the broad category of | |||||
* entropy types to harvest. | |||||
*/ | |||||
__read_frequently u_int hc_source_mask; | |||||
/* | /* | ||||
* Put all the harvest queue context stuff in one place. | * Put all the harvest queue context stuff in one place. | ||||
* this make is a bit easier to lock and protect. | * this make is a bit easier to lock and protect. | ||||
*/ | */ | ||||
static struct harvest_context { | static struct harvest_context { | ||||
/* The harvest mutex protects all of harvest_context and | /* The harvest mutex protects all of harvest_context and | ||||
* the related data. | * the related data. | ||||
*/ | */ | ||||
struct mtx hc_mtx; | struct mtx hc_mtx; | ||||
/* Round-robin destination cache. */ | /* Round-robin destination cache. */ | ||||
u_int hc_destination[ENTROPYSOURCE]; | u_int hc_destination[ENTROPYSOURCE]; | ||||
/* The context of the kernel thread processing harvested entropy */ | /* The context of the kernel thread processing harvested entropy */ | ||||
struct proc *hc_kthread_proc; | struct proc *hc_kthread_proc; | ||||
/* Allow the sysadmin to select the broad category of | |||||
* entropy types to harvest. | |||||
*/ | |||||
u_int hc_source_mask; | |||||
/* | /* | ||||
* Lockless ring buffer holding entropy events | * Lockless ring buffer holding entropy events | ||||
* If ring.in == ring.out, | * If ring.in == ring.out, | ||||
* the buffer is empty. | * the buffer is empty. | ||||
* If ring.in != ring.out, | * If ring.in != ring.out, | ||||
* the buffer contains harvested entropy. | * the buffer contains harvested entropy. | ||||
* If (ring.in + 1) == ring.out (mod RANDOM_RING_MAX), | * If (ring.in + 1) == ring.out (mod RANDOM_RING_MAX), | ||||
* the buffer is full. | * the buffer is full. | ||||
▲ Show 20 Lines • Show All 135 Lines • ▼ Show 20 Lines | |||||
/* ARGSUSED */ | /* ARGSUSED */ | ||||
static int | static int | ||||
random_check_uint_harvestmask(SYSCTL_HANDLER_ARGS) | random_check_uint_harvestmask(SYSCTL_HANDLER_ARGS) | ||||
{ | { | ||||
int error; | int error; | ||||
u_int value, orig_value; | u_int value, orig_value; | ||||
orig_value = value = harvest_context.hc_source_mask; | orig_value = value = hc_source_mask; | ||||
error = sysctl_handle_int(oidp, &value, 0, req); | error = sysctl_handle_int(oidp, &value, 0, req); | ||||
if (error != 0 || req->newptr == NULL) | if (error != 0 || req->newptr == NULL) | ||||
return (error); | return (error); | ||||
if (flsl(value) > ENTROPYSOURCE) | if (flsl(value) > ENTROPYSOURCE) | ||||
return (EINVAL); | return (EINVAL); | ||||
/* | /* | ||||
* Disallow userspace modification of pure entropy sources. | * Disallow userspace modification of pure entropy sources. | ||||
*/ | */ | ||||
harvest_context.hc_source_mask = (value & ~RANDOM_HARVEST_PURE_MASK) | | hc_source_mask = (value & ~RANDOM_HARVEST_PURE_MASK) | | ||||
(orig_value & RANDOM_HARVEST_PURE_MASK); | (orig_value & RANDOM_HARVEST_PURE_MASK); | ||||
return (0); | return (0); | ||||
} | } | ||||
/* ARGSUSED */ | /* ARGSUSED */ | ||||
static int | static int | ||||
random_print_harvestmask(SYSCTL_HANDLER_ARGS) | random_print_harvestmask(SYSCTL_HANDLER_ARGS) | ||||
{ | { | ||||
struct sbuf sbuf; | struct sbuf sbuf; | ||||
int error, i; | int error, i; | ||||
error = sysctl_wire_old_buffer(req, 0); | error = sysctl_wire_old_buffer(req, 0); | ||||
if (error == 0) { | if (error == 0) { | ||||
sbuf_new_for_sysctl(&sbuf, NULL, 128, req); | sbuf_new_for_sysctl(&sbuf, NULL, 128, req); | ||||
for (i = ENTROPYSOURCE - 1; i >= 0; i--) | for (i = ENTROPYSOURCE - 1; i >= 0; i--) | ||||
sbuf_cat(&sbuf, (harvest_context.hc_source_mask & (1 << i)) ? "1" : "0"); | sbuf_cat(&sbuf, (hc_source_mask & (1 << i)) ? "1" : "0"); | ||||
error = sbuf_finish(&sbuf); | error = sbuf_finish(&sbuf); | ||||
sbuf_delete(&sbuf); | sbuf_delete(&sbuf); | ||||
} | } | ||||
return (error); | return (error); | ||||
} | } | ||||
static const char *random_source_descr[ENTROPYSOURCE] = { | static const char *random_source_descr[ENTROPYSOURCE] = { | ||||
[RANDOM_CACHED] = "CACHED", | [RANDOM_CACHED] = "CACHED", | ||||
Show All 30 Lines | random_print_harvestmask_symbolic(SYSCTL_HANDLER_ARGS) | ||||
bool first; | bool first; | ||||
first = true; | first = true; | ||||
error = sysctl_wire_old_buffer(req, 0); | error = sysctl_wire_old_buffer(req, 0); | ||||
if (error == 0) { | if (error == 0) { | ||||
sbuf_new_for_sysctl(&sbuf, NULL, 128, req); | sbuf_new_for_sysctl(&sbuf, NULL, 128, req); | ||||
for (i = ENTROPYSOURCE - 1; i >= 0; i--) { | for (i = ENTROPYSOURCE - 1; i >= 0; i--) { | ||||
if (i >= RANDOM_PURE_START && | if (i >= RANDOM_PURE_START && | ||||
(harvest_context.hc_source_mask & (1 << i)) == 0) | (hc_source_mask & (1 << i)) == 0) | ||||
continue; | continue; | ||||
if (!first) | if (!first) | ||||
sbuf_cat(&sbuf, ","); | sbuf_cat(&sbuf, ","); | ||||
sbuf_cat(&sbuf, !(harvest_context.hc_source_mask & (1 << i)) ? "[" : ""); | sbuf_cat(&sbuf, !(hc_source_mask & (1 << i)) ? "[" : ""); | ||||
sbuf_cat(&sbuf, random_source_descr[i]); | sbuf_cat(&sbuf, random_source_descr[i]); | ||||
sbuf_cat(&sbuf, !(harvest_context.hc_source_mask & (1 << i)) ? "]" : ""); | sbuf_cat(&sbuf, !(hc_source_mask & (1 << i)) ? "]" : ""); | ||||
first = false; | first = false; | ||||
} | } | ||||
error = sbuf_finish(&sbuf); | error = sbuf_finish(&sbuf); | ||||
sbuf_delete(&sbuf); | sbuf_delete(&sbuf); | ||||
} | } | ||||
return (error); | return (error); | ||||
} | } | ||||
/* ARGSUSED */ | /* ARGSUSED */ | ||||
static void | static void | ||||
random_harvestq_init(void *unused __unused) | random_harvestq_init(void *unused __unused) | ||||
{ | { | ||||
struct sysctl_oid *random_sys_o; | struct sysctl_oid *random_sys_o; | ||||
random_sys_o = SYSCTL_ADD_NODE(&random_clist, | random_sys_o = SYSCTL_ADD_NODE(&random_clist, | ||||
SYSCTL_STATIC_CHILDREN(_kern_random), | SYSCTL_STATIC_CHILDREN(_kern_random), | ||||
OID_AUTO, "harvest", CTLFLAG_RW, 0, | OID_AUTO, "harvest", CTLFLAG_RW, 0, | ||||
"Entropy Device Parameters"); | "Entropy Device Parameters"); | ||||
harvest_context.hc_source_mask = RANDOM_HARVEST_EVERYTHING_MASK; | hc_source_mask = RANDOM_HARVEST_EVERYTHING_MASK; | ||||
SYSCTL_ADD_PROC(&random_clist, | SYSCTL_ADD_PROC(&random_clist, | ||||
SYSCTL_CHILDREN(random_sys_o), | SYSCTL_CHILDREN(random_sys_o), | ||||
OID_AUTO, "mask", CTLTYPE_UINT | CTLFLAG_RW, | OID_AUTO, "mask", CTLTYPE_UINT | CTLFLAG_RW, | ||||
NULL, 0, random_check_uint_harvestmask, "IU", | NULL, 0, random_check_uint_harvestmask, "IU", | ||||
"Entropy harvesting mask"); | "Entropy harvesting mask"); | ||||
SYSCTL_ADD_PROC(&random_clist, | SYSCTL_ADD_PROC(&random_clist, | ||||
SYSCTL_CHILDREN(random_sys_o), | SYSCTL_CHILDREN(random_sys_o), | ||||
OID_AUTO, "mask_bin", CTLTYPE_STRING | CTLFLAG_RD, | OID_AUTO, "mask_bin", CTLTYPE_STRING | CTLFLAG_RD, | ||||
▲ Show 20 Lines • Show All 84 Lines • ▼ Show 20 Lines | |||||
* check a few lines below. This includes the "always-on" sources | * check a few lines below. This includes the "always-on" sources | ||||
* like the Intel "rdrand" or the VIA Nehamiah "xstore" sources. | * like the Intel "rdrand" or the VIA Nehamiah "xstore" sources. | ||||
*/ | */ | ||||
/* XXXRW: get_cyclecount() is cheap on most modern hardware, where cycle | /* XXXRW: get_cyclecount() is cheap on most modern hardware, where cycle | ||||
* counters are built in, but on older hardware it will do a real time clock | * counters are built in, but on older hardware it will do a real time clock | ||||
* read which can be quite expensive. | * read which can be quite expensive. | ||||
*/ | */ | ||||
void | void | ||||
random_harvest_queue(const void *entropy, u_int size, u_int bits, enum random_entropy_source origin) | random_harvest_queue_(const void *entropy, u_int size, u_int bits, | ||||
enum random_entropy_source origin) | |||||
{ | { | ||||
struct harvest_event *event; | struct harvest_event *event; | ||||
u_int ring_in; | u_int ring_in; | ||||
KASSERT(origin >= RANDOM_START && origin < ENTROPYSOURCE, ("%s: origin %d invalid\n", __func__, origin)); | KASSERT(origin >= RANDOM_START && origin < ENTROPYSOURCE, ("%s: origin %d invalid\n", __func__, origin)); | ||||
if (!(harvest_context.hc_source_mask & (1 << origin))) | |||||
return; | |||||
RANDOM_HARVEST_LOCK(); | RANDOM_HARVEST_LOCK(); | ||||
ring_in = (harvest_context.hc_entropy_ring.in + 1)%RANDOM_RING_MAX; | ring_in = (harvest_context.hc_entropy_ring.in + 1)%RANDOM_RING_MAX; | ||||
if (ring_in != harvest_context.hc_entropy_ring.out) { | if (ring_in != harvest_context.hc_entropy_ring.out) { | ||||
/* The ring is not full */ | /* The ring is not full */ | ||||
event = harvest_context.hc_entropy_ring.ring + ring_in; | event = harvest_context.hc_entropy_ring.ring + ring_in; | ||||
event->he_somecounter = (uint32_t)get_cyclecount(); | event->he_somecounter = (uint32_t)get_cyclecount(); | ||||
event->he_source = origin; | event->he_source = origin; | ||||
event->he_destination = harvest_context.hc_destination[origin]++; | event->he_destination = harvest_context.hc_destination[origin]++; | ||||
Show All 14 Lines | |||||
/*- | /*- | ||||
* Entropy harvesting fast routine. | * Entropy harvesting fast routine. | ||||
* | * | ||||
* This is supposed to be very fast; do not do anything slow in here! | * This is supposed to be very fast; do not do anything slow in here! | ||||
* This is the right place for high-rate harvested data. | * This is the right place for high-rate harvested data. | ||||
*/ | */ | ||||
void | void | ||||
random_harvest_fast(const void *entropy, u_int size, u_int bits, enum random_entropy_source origin) | random_harvest_fast_(const void *entropy, u_int size, u_int bits) | ||||
{ | { | ||||
u_int pos; | u_int pos; | ||||
KASSERT(origin >= RANDOM_START && origin < ENTROPYSOURCE, ("%s: origin %d invalid\n", __func__, origin)); | |||||
/* XXX: FIX!! The above KASSERT is BS. Right now we ignore most structure and just accumulate the supplied data */ | |||||
if (!(harvest_context.hc_source_mask & (1 << origin))) | |||||
return; | |||||
pos = harvest_context.hc_entropy_fast_accumulator.pos; | pos = harvest_context.hc_entropy_fast_accumulator.pos; | ||||
cem: KASSERT references removed parameter | |||||
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, (uint32_t)get_cyclecount()); | ||||
harvest_context.hc_entropy_fast_accumulator.pos = (pos + 1)%RANDOM_ACCUM_MAX; | harvest_context.hc_entropy_fast_accumulator.pos = (pos + 1)%RANDOM_ACCUM_MAX; | ||||
} | } | ||||
/*- | /*- | ||||
* Entropy harvesting direct routine. | * Entropy harvesting direct routine. | ||||
* | * | ||||
* This is not supposed to be fast, but will only be used during | * This is not supposed to be fast, but will only be used during | ||||
* (e.g.) booting when initial entropy is being gathered. | * (e.g.) booting when initial entropy is being gathered. | ||||
*/ | */ | ||||
void | void | ||||
random_harvest_direct(const void *entropy, u_int size, u_int bits, enum random_entropy_source origin) | random_harvest_direct_(const void *entropy, u_int size, u_int bits, enum random_entropy_source origin) | ||||
{ | { | ||||
struct harvest_event event; | struct harvest_event event; | ||||
KASSERT(origin >= RANDOM_START && origin < ENTROPYSOURCE, ("%s: origin %d invalid\n", __func__, origin)); | KASSERT(origin >= RANDOM_START && origin < ENTROPYSOURCE, ("%s: origin %d invalid\n", __func__, origin)); | ||||
if (!(harvest_context.hc_source_mask & (1 << origin))) | |||||
return; | |||||
size = MIN(size, sizeof(event.he_entropy)); | size = MIN(size, sizeof(event.he_entropy)); | ||||
event.he_somecounter = (uint32_t)get_cyclecount(); | event.he_somecounter = (uint32_t)get_cyclecount(); | ||||
event.he_size = size; | event.he_size = size; | ||||
event.he_bits = bits; | event.he_bits = bits; | ||||
event.he_source = origin; | event.he_source = origin; | ||||
event.he_destination = harvest_context.hc_destination[origin]++; | event.he_destination = harvest_context.hc_destination[origin]++; | ||||
memcpy(event.he_entropy, entropy, size); | memcpy(event.he_entropy, entropy, size); | ||||
random_harvestq_fast_process_event(&event); | random_harvestq_fast_process_event(&event); | ||||
explicit_bzero(&event, sizeof(event)); | explicit_bzero(&event, sizeof(event)); | ||||
} | } | ||||
void | void | ||||
random_harvest_register_source(enum random_entropy_source source) | random_harvest_register_source(enum random_entropy_source source) | ||||
{ | { | ||||
harvest_context.hc_source_mask |= (1 << source); | hc_source_mask |= (1 << source); | ||||
} | } | ||||
void | void | ||||
random_harvest_deregister_source(enum random_entropy_source source) | random_harvest_deregister_source(enum random_entropy_source source) | ||||
{ | { | ||||
harvest_context.hc_source_mask &= ~(1 << source); | hc_source_mask &= ~(1 << source); | ||||
} | } | ||||
MODULE_VERSION(random_harvestq, 1); | MODULE_VERSION(random_harvestq, 1); |
KASSERT references removed parameter