10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*1920Smcpowers * Common Development and Distribution License (the "License"). 6*1920Smcpowers * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*1920Smcpowers * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * Software based random number provider for the Kernel Cryptographic 300Sstevel@tonic-gate * Framework (KCF). This provider periodically collects unpredictable input 310Sstevel@tonic-gate * from external sources and processes it into a pool of entropy (randomness) 320Sstevel@tonic-gate * in order to satisfy requests for random bits from kCF. It implements 330Sstevel@tonic-gate * software-based mixing, extraction, and generation algorithms. 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * A history note: The software-based algorithms in this file used to be 360Sstevel@tonic-gate * part of the /dev/random driver. 370Sstevel@tonic-gate */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate #include <sys/types.h> 400Sstevel@tonic-gate #include <sys/errno.h> 410Sstevel@tonic-gate #include <sys/debug.h> 420Sstevel@tonic-gate #include <vm/seg_kmem.h> 430Sstevel@tonic-gate #include <vm/hat.h> 440Sstevel@tonic-gate #include <sys/systm.h> 450Sstevel@tonic-gate #include <sys/memlist.h> 460Sstevel@tonic-gate #include <sys/cmn_err.h> 470Sstevel@tonic-gate #include <sys/ksynch.h> 480Sstevel@tonic-gate #include <sys/random.h> 490Sstevel@tonic-gate #include <sys/ddi.h> 500Sstevel@tonic-gate #include <sys/mman.h> 510Sstevel@tonic-gate #include <sys/sysmacros.h> 520Sstevel@tonic-gate #include <sys/mem_config.h> 530Sstevel@tonic-gate #include <sys/time.h> 540Sstevel@tonic-gate #include <sys/crypto/spi.h> 550Sstevel@tonic-gate #include <sys/sha1.h> 560Sstevel@tonic-gate #include <sys/sunddi.h> 570Sstevel@tonic-gate #include <sys/modctl.h> 580Sstevel@tonic-gate 590Sstevel@tonic-gate #define RNDPOOLSIZE 1024 /* Pool size in bytes */ 600Sstevel@tonic-gate #define HASHBUFSIZE 64 /* Buffer size used for pool mixing */ 610Sstevel@tonic-gate #define MAXMEMBLOCKS 16384 /* Number of memory blocks to scan */ 620Sstevel@tonic-gate #define MEMBLOCKSIZE 4096 /* Size of memory block to read */ 630Sstevel@tonic-gate #define MINEXTRACTBITS 160 /* Min entropy level for extraction */ 640Sstevel@tonic-gate #define TIMEOUT_INTERVAL 5 /* Periodic mixing interval in secs */ 650Sstevel@tonic-gate 660Sstevel@tonic-gate /* Hash-algo generic definitions. For now, they are SHA1's. */ 670Sstevel@tonic-gate #define HASHSIZE 20 680Sstevel@tonic-gate #define HASH_CTX SHA1_CTX 690Sstevel@tonic-gate #define HashInit(ctx) SHA1Init((ctx)) 700Sstevel@tonic-gate #define HashUpdate(ctx, p, s) SHA1Update((ctx), (p), (s)) 710Sstevel@tonic-gate #define HashFinal(d, ctx) SHA1Final((d), (ctx)) 720Sstevel@tonic-gate 730Sstevel@tonic-gate /* Physical memory entropy source */ 740Sstevel@tonic-gate typedef struct physmem_entsrc_s { 750Sstevel@tonic-gate uint8_t *parity; /* parity bit vector */ 760Sstevel@tonic-gate caddr_t pmbuf; /* buffer for memory block */ 770Sstevel@tonic-gate uint32_t nblocks; /* number of memory blocks */ 780Sstevel@tonic-gate int entperblock; /* entropy bits per block read */ 790Sstevel@tonic-gate hrtime_t last_diff; /* previous time to process a block */ 800Sstevel@tonic-gate hrtime_t last_delta; /* previous time delta */ 810Sstevel@tonic-gate hrtime_t last_delta2; /* previous 2nd order time delta */ 820Sstevel@tonic-gate } physmem_entsrc_t; 830Sstevel@tonic-gate 840Sstevel@tonic-gate static uint32_t srndpool[RNDPOOLSIZE/4]; /* Pool of random bits */ 85*1920Smcpowers static uint32_t buffer[RNDPOOLSIZE/4]; /* entropy mixed in later */ 86*1920Smcpowers static int buffer_bytes; /* bytes written to buffer */ 870Sstevel@tonic-gate static uint32_t entropy_bits; /* pool's current amount of entropy */ 880Sstevel@tonic-gate static kmutex_t srndpool_lock; /* protects r/w accesses to the pool, */ 890Sstevel@tonic-gate /* and the global variables */ 90*1920Smcpowers static kmutex_t buffer_lock; /* protects r/w accesses to buffer */ 910Sstevel@tonic-gate static kcondvar_t srndpool_read_cv; /* serializes poll/read syscalls */ 920Sstevel@tonic-gate static int pindex; /* Global index for adding/extracting */ 930Sstevel@tonic-gate /* from the pool */ 94*1920Smcpowers static int bstart, bindex; /* Global vars for adding/extracting */ 95*1920Smcpowers /* from the buffer */ 960Sstevel@tonic-gate static uint8_t leftover[HASHSIZE]; /* leftover output */ 970Sstevel@tonic-gate static int leftover_bytes; /* leftover length */ 980Sstevel@tonic-gate 990Sstevel@tonic-gate static physmem_entsrc_t entsrc; /* Physical mem as an entropy source */ 1000Sstevel@tonic-gate static timeout_id_t rnd_timeout_id; 1010Sstevel@tonic-gate static int snum_waiters; 1020Sstevel@tonic-gate static crypto_kcf_provider_handle_t swrand_prov_handle = NULL; 1030Sstevel@tonic-gate swrand_stats_t swrand_stats; 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate static int physmem_ent_init(physmem_entsrc_t *); 1060Sstevel@tonic-gate static void physmem_ent_fini(physmem_entsrc_t *); 1070Sstevel@tonic-gate static void physmem_ent_gen(physmem_entsrc_t *); 1080Sstevel@tonic-gate static int physmem_parity_update(uint8_t *, uint32_t, int); 1090Sstevel@tonic-gate static void physmem_count_blocks(); 1100Sstevel@tonic-gate static void rnd_dr_callback_post_add(void *, pgcnt_t); 1110Sstevel@tonic-gate static int rnd_dr_callback_pre_del(void *, pgcnt_t); 1120Sstevel@tonic-gate static void rnd_dr_callback_post_del(void *, pgcnt_t, int); 1130Sstevel@tonic-gate static void rnd_handler(void *arg); 1140Sstevel@tonic-gate static void swrand_init(); 1150Sstevel@tonic-gate static void swrand_schedule_timeout(void); 1160Sstevel@tonic-gate static int swrand_get_entropy(uint8_t *ptr, size_t len, boolean_t); 1170Sstevel@tonic-gate static void swrand_add_entropy(uint8_t *ptr, size_t len, uint16_t entropy_est); 118*1920Smcpowers static void swrand_add_entropy_later(uint8_t *ptr, size_t len); 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate /* Dynamic Reconfiguration related declarations */ 1210Sstevel@tonic-gate kphysm_setup_vector_t rnd_dr_callback_vec = { 1220Sstevel@tonic-gate KPHYSM_SETUP_VECTOR_VERSION, 1230Sstevel@tonic-gate rnd_dr_callback_post_add, 1240Sstevel@tonic-gate rnd_dr_callback_pre_del, 1250Sstevel@tonic-gate rnd_dr_callback_post_del 1260Sstevel@tonic-gate }; 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate extern struct mod_ops mod_cryptoops; 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate /* 1310Sstevel@tonic-gate * Module linkage information for the kernel. 1320Sstevel@tonic-gate */ 1330Sstevel@tonic-gate static struct modlcrypto modlcrypto = { 1340Sstevel@tonic-gate &mod_cryptoops, 1350Sstevel@tonic-gate "Kernel Random number Provider %I%" 1360Sstevel@tonic-gate }; 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate static struct modlinkage modlinkage = { 1390Sstevel@tonic-gate MODREV_1, 1400Sstevel@tonic-gate (void *)&modlcrypto, 1410Sstevel@tonic-gate NULL 1420Sstevel@tonic-gate }; 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate /* 1450Sstevel@tonic-gate * CSPI information (entry points, provider info, etc.) 1460Sstevel@tonic-gate */ 1470Sstevel@tonic-gate static void swrand_provider_status(crypto_provider_handle_t, uint_t *); 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate static crypto_control_ops_t swrand_control_ops = { 1500Sstevel@tonic-gate swrand_provider_status 1510Sstevel@tonic-gate }; 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate static int swrand_seed_random(crypto_provider_handle_t, crypto_session_id_t, 154*1920Smcpowers uchar_t *, size_t, uint_t, uint32_t, crypto_req_handle_t); 1550Sstevel@tonic-gate static int swrand_generate_random(crypto_provider_handle_t, 1560Sstevel@tonic-gate crypto_session_id_t, uchar_t *, size_t, crypto_req_handle_t); 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate static crypto_random_number_ops_t swrand_random_number_ops = { 1590Sstevel@tonic-gate swrand_seed_random, 1600Sstevel@tonic-gate swrand_generate_random 1610Sstevel@tonic-gate }; 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate static crypto_ops_t swrand_crypto_ops = { 1640Sstevel@tonic-gate &swrand_control_ops, 1650Sstevel@tonic-gate NULL, 1660Sstevel@tonic-gate NULL, 1670Sstevel@tonic-gate NULL, 1680Sstevel@tonic-gate NULL, 1690Sstevel@tonic-gate NULL, 1700Sstevel@tonic-gate NULL, 1710Sstevel@tonic-gate NULL, 1720Sstevel@tonic-gate &swrand_random_number_ops, 1730Sstevel@tonic-gate NULL, 1740Sstevel@tonic-gate NULL, 1750Sstevel@tonic-gate NULL, 1760Sstevel@tonic-gate NULL, 1770Sstevel@tonic-gate NULL 1780Sstevel@tonic-gate }; 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate static crypto_provider_info_t swrand_prov_info = { 1810Sstevel@tonic-gate CRYPTO_SPI_VERSION_1, 1820Sstevel@tonic-gate "Kernel Random Number Provider", 1830Sstevel@tonic-gate CRYPTO_SW_PROVIDER, 1840Sstevel@tonic-gate {&modlinkage}, 1850Sstevel@tonic-gate NULL, 1860Sstevel@tonic-gate &swrand_crypto_ops, 1870Sstevel@tonic-gate 0, 1880Sstevel@tonic-gate NULL 1890Sstevel@tonic-gate }; 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate int 1920Sstevel@tonic-gate _init(void) 1930Sstevel@tonic-gate { 1940Sstevel@tonic-gate int ret; 1950Sstevel@tonic-gate hrtime_t ts; 1960Sstevel@tonic-gate time_t now; 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate /* 1990Sstevel@tonic-gate * Register with KCF. If the registration fails, return error. 2000Sstevel@tonic-gate */ 2010Sstevel@tonic-gate if ((ret = crypto_register_provider(&swrand_prov_info, 2020Sstevel@tonic-gate &swrand_prov_handle)) != CRYPTO_SUCCESS) { 2030Sstevel@tonic-gate cmn_err(CE_WARN, "swrand : Kernel Random Number Provider " 2040Sstevel@tonic-gate "disabled for /dev/random use"); 2050Sstevel@tonic-gate return (EACCES); 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate mutex_init(&srndpool_lock, NULL, MUTEX_DEFAULT, NULL); 209*1920Smcpowers mutex_init(&buffer_lock, NULL, MUTEX_DEFAULT, NULL); 2100Sstevel@tonic-gate cv_init(&srndpool_read_cv, NULL, CV_DEFAULT, NULL); 2110Sstevel@tonic-gate entropy_bits = 0; 2120Sstevel@tonic-gate pindex = 0; 213*1920Smcpowers bindex = 0; 214*1920Smcpowers bstart = 0; 2150Sstevel@tonic-gate snum_waiters = 0; 2160Sstevel@tonic-gate leftover_bytes = 0; 217*1920Smcpowers buffer_bytes = 0; 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate /* 2200Sstevel@tonic-gate * Initialize the pool using 2210Sstevel@tonic-gate * . 2 unpredictable times: high resolution time since the boot-time, 2220Sstevel@tonic-gate * and the current time-of-the day. 2230Sstevel@tonic-gate * . The initial physical memory state. 2240Sstevel@tonic-gate */ 2250Sstevel@tonic-gate ts = gethrtime(); 2260Sstevel@tonic-gate swrand_add_entropy((uint8_t *)&ts, sizeof (ts), 0); 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate (void) drv_getparm(TIME, &now); 2290Sstevel@tonic-gate swrand_add_entropy((uint8_t *)&now, sizeof (now), 0); 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate ret = kphysm_setup_func_register(&rnd_dr_callback_vec, NULL); 2320Sstevel@tonic-gate ASSERT(ret == 0); 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate if (physmem_ent_init(&entsrc) != 0) { 2350Sstevel@tonic-gate mutex_destroy(&srndpool_lock); 236*1920Smcpowers mutex_destroy(&buffer_lock); 2370Sstevel@tonic-gate cv_destroy(&srndpool_read_cv); 2380Sstevel@tonic-gate (void) crypto_unregister_provider(swrand_prov_handle); 2390Sstevel@tonic-gate return (ENOMEM); 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate if ((ret = mod_install(&modlinkage)) != 0) { 2430Sstevel@tonic-gate mutex_destroy(&srndpool_lock); 244*1920Smcpowers mutex_destroy(&buffer_lock); 2450Sstevel@tonic-gate cv_destroy(&srndpool_read_cv); 2460Sstevel@tonic-gate physmem_ent_fini(&entsrc); 2470Sstevel@tonic-gate (void) crypto_unregister_provider(swrand_prov_handle); 2480Sstevel@tonic-gate return (ret); 2490Sstevel@tonic-gate } 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate /* Schedule periodic mixing of the pool. */ 2520Sstevel@tonic-gate mutex_enter(&srndpool_lock); 2530Sstevel@tonic-gate swrand_schedule_timeout(); 2540Sstevel@tonic-gate mutex_exit(&srndpool_lock); 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate return (0); 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate int 2600Sstevel@tonic-gate _info(struct modinfo *modinfop) 2610Sstevel@tonic-gate { 2620Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate /* 2660Sstevel@tonic-gate * Control entry points. 2670Sstevel@tonic-gate */ 2680Sstevel@tonic-gate /* ARGSUSED */ 2690Sstevel@tonic-gate static void 2700Sstevel@tonic-gate swrand_provider_status(crypto_provider_handle_t provider, uint_t *status) 2710Sstevel@tonic-gate { 2720Sstevel@tonic-gate *status = CRYPTO_PROVIDER_READY; 2730Sstevel@tonic-gate } 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate /* 2760Sstevel@tonic-gate * Random number entry points. 2770Sstevel@tonic-gate */ 2780Sstevel@tonic-gate /* ARGSUSED */ 2790Sstevel@tonic-gate static int 2800Sstevel@tonic-gate swrand_seed_random(crypto_provider_handle_t provider, crypto_session_id_t sid, 281*1920Smcpowers uchar_t *buf, size_t len, uint_t entropy_est, uint32_t flags, 282*1920Smcpowers crypto_req_handle_t req) 2830Sstevel@tonic-gate { 2840Sstevel@tonic-gate /* The entropy estimate is always 0 in this path */ 285*1920Smcpowers if (flags & CRYPTO_SEED_NOW) 286*1920Smcpowers swrand_add_entropy(buf, len, 0); 287*1920Smcpowers else 288*1920Smcpowers swrand_add_entropy_later(buf, len); 2890Sstevel@tonic-gate return (CRYPTO_SUCCESS); 2900Sstevel@tonic-gate } 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate /* ARGSUSED */ 2930Sstevel@tonic-gate static int 2940Sstevel@tonic-gate swrand_generate_random(crypto_provider_handle_t provider, 2950Sstevel@tonic-gate crypto_session_id_t sid, uchar_t *buf, size_t len, crypto_req_handle_t req) 2960Sstevel@tonic-gate { 2970Sstevel@tonic-gate if (crypto_kmflag(req) == KM_NOSLEEP) 2980Sstevel@tonic-gate (void) swrand_get_entropy(buf, len, B_TRUE); 2990Sstevel@tonic-gate else 3000Sstevel@tonic-gate (void) swrand_get_entropy(buf, len, B_FALSE); 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate return (CRYPTO_SUCCESS); 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate /* 3070Sstevel@tonic-gate * Extraction of entropy from the pool. 3080Sstevel@tonic-gate * 3090Sstevel@tonic-gate * Returns "len" random bytes in *ptr. 3100Sstevel@tonic-gate * Try to gather some more entropy by calling physmem_ent_gen() when less than 3110Sstevel@tonic-gate * MINEXTRACTBITS are present in the pool. 3120Sstevel@tonic-gate * Will block if not enough entropy was available and the call is blocking. 3130Sstevel@tonic-gate */ 3140Sstevel@tonic-gate static int 3150Sstevel@tonic-gate swrand_get_entropy(uint8_t *ptr, size_t len, boolean_t nonblock) 3160Sstevel@tonic-gate { 3170Sstevel@tonic-gate int i, bytes; 3180Sstevel@tonic-gate HASH_CTX hashctx; 3190Sstevel@tonic-gate uint8_t digest[HASHSIZE], *pool; 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate mutex_enter(&srndpool_lock); 3220Sstevel@tonic-gate if (leftover_bytes > 0) { 3230Sstevel@tonic-gate bytes = min(len, leftover_bytes); 3240Sstevel@tonic-gate bcopy(leftover, ptr, bytes); 3250Sstevel@tonic-gate len -= bytes; 3260Sstevel@tonic-gate ptr += bytes; 3270Sstevel@tonic-gate leftover_bytes -= bytes; 3280Sstevel@tonic-gate if (leftover_bytes > 0) 3290Sstevel@tonic-gate ovbcopy(leftover+bytes, leftover, leftover_bytes); 3300Sstevel@tonic-gate } 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate while (len > 0) { 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate /* Check if there is enough entropy */ 3350Sstevel@tonic-gate while (entropy_bits < MINEXTRACTBITS) { 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate physmem_ent_gen(&entsrc); 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate if (entropy_bits < MINEXTRACTBITS && 3400Sstevel@tonic-gate nonblock == B_TRUE) { 3410Sstevel@tonic-gate mutex_exit(&srndpool_lock); 3420Sstevel@tonic-gate return (EAGAIN); 3430Sstevel@tonic-gate } 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate if (entropy_bits < MINEXTRACTBITS) { 3460Sstevel@tonic-gate ASSERT(nonblock == B_FALSE); 3470Sstevel@tonic-gate snum_waiters++; 3480Sstevel@tonic-gate if (cv_wait_sig(&srndpool_read_cv, 3490Sstevel@tonic-gate &srndpool_lock) == 0) { 3500Sstevel@tonic-gate snum_waiters--; 3510Sstevel@tonic-gate mutex_exit(&srndpool_lock); 3520Sstevel@tonic-gate return (EINTR); 3530Sstevel@tonic-gate } 3540Sstevel@tonic-gate snum_waiters--; 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate } 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate /* Figure out how many bytes to extract */ 3590Sstevel@tonic-gate bytes = min(HASHSIZE, len); 3600Sstevel@tonic-gate bytes = min(bytes, entropy_bits/8); 3610Sstevel@tonic-gate entropy_bits -= bytes * 8; 3620Sstevel@tonic-gate BUMP_SWRAND_STATS(ss_entOut, bytes * 8); 3630Sstevel@tonic-gate swrand_stats.ss_entEst = entropy_bits; 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate /* Extract entropy by hashing pool content */ 3660Sstevel@tonic-gate HashInit(&hashctx); 3670Sstevel@tonic-gate HashUpdate(&hashctx, (uint8_t *)srndpool, RNDPOOLSIZE); 3680Sstevel@tonic-gate HashFinal(digest, &hashctx); 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate /* 3710Sstevel@tonic-gate * Feed the digest back into the pool so next 3720Sstevel@tonic-gate * extraction produces different result 3730Sstevel@tonic-gate */ 3740Sstevel@tonic-gate pool = (uint8_t *)srndpool; 3750Sstevel@tonic-gate for (i = 0; i < HASHSIZE; i++) { 3760Sstevel@tonic-gate pool[pindex++] ^= digest[i]; 3770Sstevel@tonic-gate /* pindex modulo RNDPOOLSIZE */ 3780Sstevel@tonic-gate pindex &= (RNDPOOLSIZE - 1); 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate /* 3820Sstevel@tonic-gate * Hash the digest again before output to obscure 3830Sstevel@tonic-gate * what was fed back to the pool. 3840Sstevel@tonic-gate */ 3850Sstevel@tonic-gate HashInit(&hashctx); 3860Sstevel@tonic-gate HashUpdate(&hashctx, digest, HASHSIZE); 3870Sstevel@tonic-gate if (len >= HASHSIZE) 3880Sstevel@tonic-gate HashFinal(ptr, &hashctx); 3890Sstevel@tonic-gate else { 3900Sstevel@tonic-gate HashFinal(digest, &hashctx); 3910Sstevel@tonic-gate bcopy(digest, ptr, bytes); 3920Sstevel@tonic-gate leftover_bytes = HASHSIZE - bytes; 3930Sstevel@tonic-gate bcopy(digest + bytes, leftover, leftover_bytes); 3940Sstevel@tonic-gate } 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate len -= bytes; 3970Sstevel@tonic-gate ptr += bytes; 3980Sstevel@tonic-gate BUMP_SWRAND_STATS(ss_bytesOut, bytes); 3990Sstevel@tonic-gate } 4000Sstevel@tonic-gate mutex_exit(&srndpool_lock); 4010Sstevel@tonic-gate return (0); 4020Sstevel@tonic-gate } 4030Sstevel@tonic-gate 404*1920Smcpowers #define SWRAND_ADD_BYTES(ptr, len, i, pool) \ 405*1920Smcpowers ASSERT((ptr) != NULL && (len) > 0); \ 406*1920Smcpowers BUMP_SWRAND_STATS(ss_bytesIn, (len)); \ 407*1920Smcpowers while ((len)--) { \ 408*1920Smcpowers (pool)[(i)++] ^= *(ptr); \ 409*1920Smcpowers (ptr)++; \ 410*1920Smcpowers (i) &= (RNDPOOLSIZE - 1); \ 411*1920Smcpowers } 412*1920Smcpowers 4130Sstevel@tonic-gate /* Write some more user-provided entropy to the pool */ 4140Sstevel@tonic-gate static void 4150Sstevel@tonic-gate swrand_add_bytes(uint8_t *ptr, size_t len) 4160Sstevel@tonic-gate { 4170Sstevel@tonic-gate uint8_t *pool = (uint8_t *)srndpool; 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate ASSERT(MUTEX_HELD(&srndpool_lock)); 420*1920Smcpowers SWRAND_ADD_BYTES(ptr, len, pindex, pool); 421*1920Smcpowers } 4220Sstevel@tonic-gate 423*1920Smcpowers /* 424*1920Smcpowers * Add bytes to buffer. Adding the buffer to the random pool 425*1920Smcpowers * is deferred until the random pool is mixed. 426*1920Smcpowers */ 427*1920Smcpowers static void 428*1920Smcpowers swrand_add_bytes_later(uint8_t *ptr, size_t len) 429*1920Smcpowers { 430*1920Smcpowers uint8_t *pool = (uint8_t *)buffer; 431*1920Smcpowers 432*1920Smcpowers ASSERT(MUTEX_HELD(&buffer_lock)); 433*1920Smcpowers SWRAND_ADD_BYTES(ptr, len, bindex, pool); 434*1920Smcpowers buffer_bytes += len; 4350Sstevel@tonic-gate } 4360Sstevel@tonic-gate 437*1920Smcpowers #undef SWRAND_ADD_BYTES 438*1920Smcpowers 4390Sstevel@tonic-gate /* Mix the pool */ 4400Sstevel@tonic-gate static void 4410Sstevel@tonic-gate swrand_mix_pool(uint16_t entropy_est) 4420Sstevel@tonic-gate { 4430Sstevel@tonic-gate int i, j, k, start; 4440Sstevel@tonic-gate HASH_CTX hashctx; 4450Sstevel@tonic-gate uint8_t digest[HASHSIZE]; 4460Sstevel@tonic-gate uint8_t *pool = (uint8_t *)srndpool; 447*1920Smcpowers uint8_t *bp = (uint8_t *)buffer; 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate ASSERT(MUTEX_HELD(&srndpool_lock)); 4500Sstevel@tonic-gate 451*1920Smcpowers /* add deferred bytes */ 452*1920Smcpowers mutex_enter(&buffer_lock); 453*1920Smcpowers if (buffer_bytes > 0) { 454*1920Smcpowers if (buffer_bytes >= RNDPOOLSIZE) { 455*1920Smcpowers for (i = 0; i < RNDPOOLSIZE/4; i++) { 456*1920Smcpowers srndpool[i] ^= buffer[i]; 457*1920Smcpowers buffer[i] = 0; 458*1920Smcpowers } 459*1920Smcpowers bstart = bindex = 0; 460*1920Smcpowers } else { 461*1920Smcpowers for (i = 0; i < buffer_bytes; i++) { 462*1920Smcpowers pool[pindex++] ^= bp[bstart]; 463*1920Smcpowers bp[bstart++] = 0; 464*1920Smcpowers pindex &= (RNDPOOLSIZE - 1); 465*1920Smcpowers bstart &= (RNDPOOLSIZE - 1); 466*1920Smcpowers } 467*1920Smcpowers ASSERT(bstart == bindex); 468*1920Smcpowers } 469*1920Smcpowers buffer_bytes = 0; 470*1920Smcpowers } 471*1920Smcpowers mutex_exit(&buffer_lock); 472*1920Smcpowers 4730Sstevel@tonic-gate start = 0; 4740Sstevel@tonic-gate for (i = 0; i < RNDPOOLSIZE/HASHSIZE + 1; i++) { 4750Sstevel@tonic-gate HashInit(&hashctx); 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate /* Hash a buffer centered on a block in the pool */ 4780Sstevel@tonic-gate if (start + HASHBUFSIZE <= RNDPOOLSIZE) 4790Sstevel@tonic-gate HashUpdate(&hashctx, &pool[start], HASHBUFSIZE); 4800Sstevel@tonic-gate else { 4810Sstevel@tonic-gate HashUpdate(&hashctx, &pool[start], 4820Sstevel@tonic-gate RNDPOOLSIZE - start); 4830Sstevel@tonic-gate HashUpdate(&hashctx, pool, 4840Sstevel@tonic-gate HASHBUFSIZE - RNDPOOLSIZE + start); 4850Sstevel@tonic-gate } 4860Sstevel@tonic-gate HashFinal(digest, &hashctx); 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate /* XOR the hash result back into the block */ 4890Sstevel@tonic-gate k = (start + HASHSIZE) & (RNDPOOLSIZE - 1); 4900Sstevel@tonic-gate for (j = 0; j < HASHSIZE; j++) { 4910Sstevel@tonic-gate pool[k++] ^= digest[j]; 4920Sstevel@tonic-gate k &= (RNDPOOLSIZE - 1); 4930Sstevel@tonic-gate } 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate /* Slide the hash buffer and repeat with next block */ 4960Sstevel@tonic-gate start = (start + HASHSIZE) & (RNDPOOLSIZE - 1); 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate entropy_bits += entropy_est; 5000Sstevel@tonic-gate if (entropy_bits > RNDPOOLSIZE * 8) 5010Sstevel@tonic-gate entropy_bits = RNDPOOLSIZE * 8; 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate swrand_stats.ss_entEst = entropy_bits; 5040Sstevel@tonic-gate BUMP_SWRAND_STATS(ss_entIn, entropy_est); 5050Sstevel@tonic-gate } 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate static void 508*1920Smcpowers swrand_add_entropy_later(uint8_t *ptr, size_t len) 509*1920Smcpowers { 510*1920Smcpowers mutex_enter(&buffer_lock); 511*1920Smcpowers swrand_add_bytes_later(ptr, len); 512*1920Smcpowers mutex_exit(&buffer_lock); 513*1920Smcpowers } 514*1920Smcpowers 515*1920Smcpowers static void 5160Sstevel@tonic-gate swrand_add_entropy(uint8_t *ptr, size_t len, uint16_t entropy_est) 5170Sstevel@tonic-gate { 5180Sstevel@tonic-gate mutex_enter(&srndpool_lock); 5190Sstevel@tonic-gate swrand_add_bytes(ptr, len); 5200Sstevel@tonic-gate swrand_mix_pool(entropy_est); 5210Sstevel@tonic-gate mutex_exit(&srndpool_lock); 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate /* 5250Sstevel@tonic-gate * The physmem_* routines below generate entropy by reading blocks of 5260Sstevel@tonic-gate * physical memory. Entropy is gathered in a couple of ways: 5270Sstevel@tonic-gate * 5280Sstevel@tonic-gate * - By reading blocks of physical memory and detecting if changes 5290Sstevel@tonic-gate * occurred in the blocks read. 5300Sstevel@tonic-gate * 5310Sstevel@tonic-gate * - By measuring the time it takes to load and hash a block of memory 5320Sstevel@tonic-gate * and computing the differences in the measured time. 5330Sstevel@tonic-gate * 5340Sstevel@tonic-gate * The first method was used in the CryptoRand implementation. Physical 5350Sstevel@tonic-gate * memory is divided into blocks of fixed size. A block of memory is 5360Sstevel@tonic-gate * chosen from the possible blocks and hashed to produce a digest. This 5370Sstevel@tonic-gate * digest is then mixed into the pool. A single bit from the digest is 5380Sstevel@tonic-gate * used as a parity bit or "checksum" and compared against the previous 5390Sstevel@tonic-gate * "checksum" computed for the block. If the single-bit checksum has not 5400Sstevel@tonic-gate * changed, no entropy is credited to the pool. If there is a change, 5410Sstevel@tonic-gate * then the assumption is that at least one bit in the block has changed. 5420Sstevel@tonic-gate * The possible locations within the memory block of where the bit change 5430Sstevel@tonic-gate * occurred is used as a measure of entropy. For example, if a block 5440Sstevel@tonic-gate * size of 4096 bytes is used, about log_2(4096*8)=15 bits worth of 5450Sstevel@tonic-gate * entropy is available. Because the single-bit checksum will miss half 5460Sstevel@tonic-gate * of the changes, the amount of entropy credited to the pool is doubled 5470Sstevel@tonic-gate * when a change is detected. With a 4096 byte block size, a block 5480Sstevel@tonic-gate * change will add a total of 30 bits of entropy to the pool. 5490Sstevel@tonic-gate * 5500Sstevel@tonic-gate * The second method measures the amount of time it takes to read and 5510Sstevel@tonic-gate * hash a physical memory block (as described above). The time measured 5520Sstevel@tonic-gate * can vary depending on system load, scheduling and other factors. 5530Sstevel@tonic-gate * Differences between consecutive measurements are computed to come up 5540Sstevel@tonic-gate * with an entropy estimate. The first, second, and third order delta is 5550Sstevel@tonic-gate * calculated to determine the minimum delta value. The number of bits 5560Sstevel@tonic-gate * present in this minimum delta value is the entropy estimate. This 5570Sstevel@tonic-gate * entropy estimation technique using time deltas is similar to that used 5580Sstevel@tonic-gate * in /dev/random implementations from Linux/BSD. 5590Sstevel@tonic-gate */ 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate static int 5620Sstevel@tonic-gate physmem_ent_init(physmem_entsrc_t *entsrc) 5630Sstevel@tonic-gate { 5640Sstevel@tonic-gate uint8_t *ptr; 5650Sstevel@tonic-gate int i; 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate bzero(entsrc, sizeof (*entsrc)); 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate /* 5700Sstevel@tonic-gate * The maximum entropy amount in bits per block of memory read is 5710Sstevel@tonic-gate * log_2(MEMBLOCKSIZE * 8); 5720Sstevel@tonic-gate */ 5730Sstevel@tonic-gate i = MEMBLOCKSIZE << 3; 5740Sstevel@tonic-gate while (i >>= 1) 5750Sstevel@tonic-gate entsrc->entperblock++; 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate /* Initialize entsrc->nblocks */ 5780Sstevel@tonic-gate physmem_count_blocks(); 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate if (entsrc->nblocks == 0) { 5810Sstevel@tonic-gate cmn_err(CE_WARN, "no memory blocks to scan!"); 5820Sstevel@tonic-gate return (-1); 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate 5850Sstevel@tonic-gate /* Allocate space for the parity vector and memory page */ 5860Sstevel@tonic-gate entsrc->parity = kmem_alloc(howmany(entsrc->nblocks, 8), 5870Sstevel@tonic-gate KM_SLEEP); 5880Sstevel@tonic-gate entsrc->pmbuf = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP); 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate 5910Sstevel@tonic-gate /* Initialize parity vector with bits from the pool */ 5920Sstevel@tonic-gate i = howmany(entsrc->nblocks, 8); 5930Sstevel@tonic-gate ptr = entsrc->parity; 5940Sstevel@tonic-gate while (i > 0) { 5950Sstevel@tonic-gate if (i > RNDPOOLSIZE) { 5960Sstevel@tonic-gate bcopy(srndpool, ptr, RNDPOOLSIZE); 5970Sstevel@tonic-gate mutex_enter(&srndpool_lock); 5980Sstevel@tonic-gate swrand_mix_pool(0); 5990Sstevel@tonic-gate mutex_exit(&srndpool_lock); 6000Sstevel@tonic-gate ptr += RNDPOOLSIZE; 6010Sstevel@tonic-gate i -= RNDPOOLSIZE; 6020Sstevel@tonic-gate } else { 6030Sstevel@tonic-gate bcopy(srndpool, ptr, i); 6040Sstevel@tonic-gate break; 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate } 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate /* Generate some entropy to further initialize the pool */ 6090Sstevel@tonic-gate mutex_enter(&srndpool_lock); 6100Sstevel@tonic-gate physmem_ent_gen(entsrc); 6110Sstevel@tonic-gate entropy_bits = 0; 6120Sstevel@tonic-gate mutex_exit(&srndpool_lock); 6130Sstevel@tonic-gate 6140Sstevel@tonic-gate return (0); 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate static void 6180Sstevel@tonic-gate physmem_ent_fini(physmem_entsrc_t *entsrc) 6190Sstevel@tonic-gate { 6200Sstevel@tonic-gate if (entsrc->pmbuf != NULL) 6210Sstevel@tonic-gate vmem_free(heap_arena, entsrc->pmbuf, PAGESIZE); 6220Sstevel@tonic-gate if (entsrc->parity != NULL) 6230Sstevel@tonic-gate kmem_free(entsrc->parity, howmany(entsrc->nblocks, 8)); 6240Sstevel@tonic-gate bzero(entsrc, sizeof (*entsrc)); 6250Sstevel@tonic-gate } 6260Sstevel@tonic-gate 6270Sstevel@tonic-gate static void 6280Sstevel@tonic-gate physmem_ent_gen(physmem_entsrc_t *entsrc) 6290Sstevel@tonic-gate { 6300Sstevel@tonic-gate struct memlist *pmem; 6310Sstevel@tonic-gate offset_t offset, poffset; 6320Sstevel@tonic-gate pfn_t pfn; 6330Sstevel@tonic-gate int i, nbytes, len, ent = 0; 6340Sstevel@tonic-gate uint32_t block, oblock; 6350Sstevel@tonic-gate hrtime_t ts1, ts2, diff, delta, delta2, delta3; 6360Sstevel@tonic-gate uint8_t digest[HASHSIZE]; 6370Sstevel@tonic-gate HASH_CTX ctx; 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate /* 6400Sstevel@tonic-gate * Use each 32-bit quantity in the pool to pick a memory 6410Sstevel@tonic-gate * block to read. 6420Sstevel@tonic-gate */ 6430Sstevel@tonic-gate for (i = 0; i < RNDPOOLSIZE/4; i++) { 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate /* If the pool is "full", stop after one block */ 6460Sstevel@tonic-gate if (entropy_bits + ent >= RNDPOOLSIZE * 8) { 6470Sstevel@tonic-gate if (i > 0) 6480Sstevel@tonic-gate break; 6490Sstevel@tonic-gate } 6500Sstevel@tonic-gate 6510Sstevel@tonic-gate /* 6520Sstevel@tonic-gate * This lock protects reading of phys_install. 6530Sstevel@tonic-gate * Any changes to this list, by DR, are done while 6540Sstevel@tonic-gate * holding this lock. So, holding this lock is sufficient 6550Sstevel@tonic-gate * to handle DR also. 6560Sstevel@tonic-gate */ 6570Sstevel@tonic-gate memlist_read_lock(); 6580Sstevel@tonic-gate 6590Sstevel@tonic-gate /* We're left with less than 4K of memory after DR */ 6600Sstevel@tonic-gate ASSERT(entsrc->nblocks > 0); 6610Sstevel@tonic-gate 6620Sstevel@tonic-gate /* Pick a memory block to read */ 6630Sstevel@tonic-gate block = oblock = srndpool[i] % entsrc->nblocks; 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate for (pmem = phys_install; pmem != NULL; pmem = pmem->next) { 6660Sstevel@tonic-gate if (block < pmem->size / MEMBLOCKSIZE) 6670Sstevel@tonic-gate break; 6680Sstevel@tonic-gate block -= pmem->size / MEMBLOCKSIZE; 6690Sstevel@tonic-gate } 6700Sstevel@tonic-gate 6710Sstevel@tonic-gate ASSERT(pmem != NULL); 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate offset = pmem->address + block * MEMBLOCKSIZE; 6740Sstevel@tonic-gate 6750Sstevel@tonic-gate if (!address_in_memlist(phys_install, offset, MEMBLOCKSIZE)) { 6760Sstevel@tonic-gate memlist_read_unlock(); 6770Sstevel@tonic-gate continue; 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate /* 6810Sstevel@tonic-gate * Figure out which page to load to read the 6820Sstevel@tonic-gate * memory block. Load the page and compute the 6830Sstevel@tonic-gate * hash of the memory block. 6840Sstevel@tonic-gate */ 6850Sstevel@tonic-gate len = MEMBLOCKSIZE; 6860Sstevel@tonic-gate ts1 = gethrtime(); 6870Sstevel@tonic-gate HashInit(&ctx); 6880Sstevel@tonic-gate while (len) { 6890Sstevel@tonic-gate pfn = offset >> PAGESHIFT; 6900Sstevel@tonic-gate poffset = offset & PAGEOFFSET; 6910Sstevel@tonic-gate nbytes = PAGESIZE - poffset < len ? 6920Sstevel@tonic-gate PAGESIZE - poffset : len; 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate hat_devload(kas.a_hat, entsrc->pmbuf, 6950Sstevel@tonic-gate PAGESIZE, pfn, PROT_READ, 6960Sstevel@tonic-gate HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK); 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate HashUpdate(&ctx, (uint8_t *)entsrc->pmbuf + poffset, 6990Sstevel@tonic-gate nbytes); 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate hat_unload(kas.a_hat, entsrc->pmbuf, PAGESIZE, 7020Sstevel@tonic-gate HAT_UNLOAD_UNLOCK); 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate len -= nbytes; 7050Sstevel@tonic-gate offset += nbytes; 7060Sstevel@tonic-gate } 7070Sstevel@tonic-gate /* We got our pages. Let the DR roll */ 7080Sstevel@tonic-gate memlist_read_unlock(); 7090Sstevel@tonic-gate 7100Sstevel@tonic-gate HashFinal(digest, &ctx); 7110Sstevel@tonic-gate ts2 = gethrtime(); 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate /* 7140Sstevel@tonic-gate * Compute the time it took to load and hash the 7150Sstevel@tonic-gate * block and compare it against the previous 7160Sstevel@tonic-gate * measurement. The delta of the time values 7170Sstevel@tonic-gate * provides a small amount of entropy. The 7180Sstevel@tonic-gate * minimum of the first, second, and third order 7190Sstevel@tonic-gate * delta is used to estimate how much entropy 7200Sstevel@tonic-gate * is present. 7210Sstevel@tonic-gate */ 7220Sstevel@tonic-gate diff = ts2 - ts1; 7230Sstevel@tonic-gate delta = diff - entsrc->last_diff; 7240Sstevel@tonic-gate if (delta < 0) 7250Sstevel@tonic-gate delta = -delta; 7260Sstevel@tonic-gate delta2 = delta - entsrc->last_delta; 7270Sstevel@tonic-gate if (delta2 < 0) 7280Sstevel@tonic-gate delta2 = -delta2; 7290Sstevel@tonic-gate delta3 = delta2 - entsrc->last_delta2; 7300Sstevel@tonic-gate if (delta3 < 0) 7310Sstevel@tonic-gate delta3 = -delta3; 7320Sstevel@tonic-gate entsrc->last_diff = diff; 7330Sstevel@tonic-gate entsrc->last_delta = delta; 7340Sstevel@tonic-gate entsrc->last_delta2 = delta2; 7350Sstevel@tonic-gate 7360Sstevel@tonic-gate if (delta > delta2) 7370Sstevel@tonic-gate delta = delta2; 7380Sstevel@tonic-gate if (delta > delta3) 7390Sstevel@tonic-gate delta = delta3; 7400Sstevel@tonic-gate delta2 = 0; 7410Sstevel@tonic-gate while (delta >>= 1) 7420Sstevel@tonic-gate delta2++; 7430Sstevel@tonic-gate ent += delta2; 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate /* 7460Sstevel@tonic-gate * If the memory block has changed, credit the pool with 7470Sstevel@tonic-gate * the entropy estimate. The entropy estimate is doubled 7480Sstevel@tonic-gate * because the single-bit checksum misses half the change 7490Sstevel@tonic-gate * on average. 7500Sstevel@tonic-gate */ 7510Sstevel@tonic-gate if (physmem_parity_update(entsrc->parity, oblock, 7520Sstevel@tonic-gate digest[0] & 1)) 7530Sstevel@tonic-gate ent += 2 * entsrc->entperblock; 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate /* Add the entropy bytes to the pool */ 7560Sstevel@tonic-gate swrand_add_bytes(digest, HASHSIZE); 7570Sstevel@tonic-gate swrand_add_bytes((uint8_t *)&ts1, sizeof (ts1)); 7580Sstevel@tonic-gate swrand_add_bytes((uint8_t *)&ts2, sizeof (ts2)); 7590Sstevel@tonic-gate } 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate swrand_mix_pool(ent); 7620Sstevel@tonic-gate } 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate static int 7650Sstevel@tonic-gate physmem_parity_update(uint8_t *parity_vec, uint32_t block, int parity) 7660Sstevel@tonic-gate { 7670Sstevel@tonic-gate /* Test and set the parity bit, return 1 if changed */ 7680Sstevel@tonic-gate if (parity == ((parity_vec[block >> 3] >> (block & 7)) & 1)) 7690Sstevel@tonic-gate return (0); 7700Sstevel@tonic-gate parity_vec[block >> 3] ^= 1 << (block & 7); 7710Sstevel@tonic-gate return (1); 7720Sstevel@tonic-gate } 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate /* Compute number of memory blocks available to scan */ 7750Sstevel@tonic-gate static void 7760Sstevel@tonic-gate physmem_count_blocks() 7770Sstevel@tonic-gate { 7780Sstevel@tonic-gate struct memlist *pmem; 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate memlist_read_lock(); 7810Sstevel@tonic-gate entsrc.nblocks = 0; 7820Sstevel@tonic-gate for (pmem = phys_install; pmem != NULL; pmem = pmem->next) { 7830Sstevel@tonic-gate entsrc.nblocks += pmem->size / MEMBLOCKSIZE; 7840Sstevel@tonic-gate if (entsrc.nblocks > MAXMEMBLOCKS) { 7850Sstevel@tonic-gate entsrc.nblocks = MAXMEMBLOCKS; 7860Sstevel@tonic-gate break; 7870Sstevel@tonic-gate } 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate memlist_read_unlock(); 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate /* 7930Sstevel@tonic-gate * Dynamic Reconfiguration call-back functions 7940Sstevel@tonic-gate */ 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate /* ARGSUSED */ 7970Sstevel@tonic-gate static void 7980Sstevel@tonic-gate rnd_dr_callback_post_add(void *arg, pgcnt_t delta) 7990Sstevel@tonic-gate { 8000Sstevel@tonic-gate /* More memory is available now, so update entsrc->nblocks. */ 8010Sstevel@tonic-gate physmem_count_blocks(); 8020Sstevel@tonic-gate } 8030Sstevel@tonic-gate 8040Sstevel@tonic-gate /* Call-back routine invoked before the DR starts a memory removal. */ 8050Sstevel@tonic-gate /* ARGSUSED */ 8060Sstevel@tonic-gate static int 8070Sstevel@tonic-gate rnd_dr_callback_pre_del(void *arg, pgcnt_t delta) 8080Sstevel@tonic-gate { 8090Sstevel@tonic-gate return (0); 8100Sstevel@tonic-gate } 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate /* Call-back routine invoked after the DR starts a memory removal. */ 8130Sstevel@tonic-gate /* ARGSUSED */ 8140Sstevel@tonic-gate static void 8150Sstevel@tonic-gate rnd_dr_callback_post_del(void *arg, pgcnt_t delta, int cancelled) 8160Sstevel@tonic-gate { 8170Sstevel@tonic-gate /* Memory has shrunk, so update entsrc->nblocks. */ 8180Sstevel@tonic-gate physmem_count_blocks(); 8190Sstevel@tonic-gate } 8200Sstevel@tonic-gate 8210Sstevel@tonic-gate /* Timeout handling to gather entropy from physmem events */ 8220Sstevel@tonic-gate static void 8230Sstevel@tonic-gate swrand_schedule_timeout(void) 8240Sstevel@tonic-gate { 8250Sstevel@tonic-gate clock_t ut; /* time in microseconds */ 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate ASSERT(MUTEX_HELD(&srndpool_lock)); 8280Sstevel@tonic-gate /* 8290Sstevel@tonic-gate * The new timeout value is taken from the pool of random bits. 8300Sstevel@tonic-gate * We're merely reading the first 32 bits from the pool here, not 8310Sstevel@tonic-gate * consuming any entropy. 8320Sstevel@tonic-gate * This routine is usually called right after stirring the pool, so 8330Sstevel@tonic-gate * srndpool[0] will have a *fresh* random value each time. 8340Sstevel@tonic-gate * The timeout multiplier value is a random value between 0.7 sec and 8350Sstevel@tonic-gate * 1.748575 sec (0.7 sec + 0xFFFFF microseconds). 8360Sstevel@tonic-gate * The new timeout is TIMEOUT_INTERVAL times that multiplier. 8370Sstevel@tonic-gate */ 8380Sstevel@tonic-gate ut = 700000 + (clock_t)(srndpool[0] & 0xFFFFF); 8390Sstevel@tonic-gate rnd_timeout_id = timeout(rnd_handler, NULL, 8400Sstevel@tonic-gate TIMEOUT_INTERVAL * drv_usectohz(ut)); 8410Sstevel@tonic-gate } 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate /*ARGSUSED*/ 8440Sstevel@tonic-gate static void 8450Sstevel@tonic-gate rnd_handler(void *arg) 8460Sstevel@tonic-gate { 8470Sstevel@tonic-gate mutex_enter(&srndpool_lock); 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate physmem_ent_gen(&entsrc); 8500Sstevel@tonic-gate if (snum_waiters > 0) 8510Sstevel@tonic-gate cv_broadcast(&srndpool_read_cv); 8520Sstevel@tonic-gate swrand_schedule_timeout(); 8530Sstevel@tonic-gate 8540Sstevel@tonic-gate mutex_exit(&srndpool_lock); 8550Sstevel@tonic-gate } 856