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 51920Smcpowers * Common Development and Distribution License (the "License"). 61920Smcpowers * 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 /* 223446Smrj * Copyright 2007 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> 583446Smrj #include <sys/hold_page.h> 590Sstevel@tonic-gate 600Sstevel@tonic-gate #define RNDPOOLSIZE 1024 /* Pool size in bytes */ 610Sstevel@tonic-gate #define HASHBUFSIZE 64 /* Buffer size used for pool mixing */ 620Sstevel@tonic-gate #define MAXMEMBLOCKS 16384 /* Number of memory blocks to scan */ 630Sstevel@tonic-gate #define MEMBLOCKSIZE 4096 /* Size of memory block to read */ 640Sstevel@tonic-gate #define MINEXTRACTBITS 160 /* Min entropy level for extraction */ 650Sstevel@tonic-gate #define TIMEOUT_INTERVAL 5 /* Periodic mixing interval in secs */ 660Sstevel@tonic-gate 670Sstevel@tonic-gate /* Hash-algo generic definitions. For now, they are SHA1's. */ 680Sstevel@tonic-gate #define HASHSIZE 20 690Sstevel@tonic-gate #define HASH_CTX SHA1_CTX 700Sstevel@tonic-gate #define HashInit(ctx) SHA1Init((ctx)) 710Sstevel@tonic-gate #define HashUpdate(ctx, p, s) SHA1Update((ctx), (p), (s)) 720Sstevel@tonic-gate #define HashFinal(d, ctx) SHA1Final((d), (ctx)) 730Sstevel@tonic-gate 740Sstevel@tonic-gate /* Physical memory entropy source */ 750Sstevel@tonic-gate typedef struct physmem_entsrc_s { 760Sstevel@tonic-gate uint8_t *parity; /* parity bit vector */ 770Sstevel@tonic-gate caddr_t pmbuf; /* buffer for memory block */ 780Sstevel@tonic-gate uint32_t nblocks; /* number of memory blocks */ 790Sstevel@tonic-gate int entperblock; /* entropy bits per block read */ 800Sstevel@tonic-gate hrtime_t last_diff; /* previous time to process a block */ 810Sstevel@tonic-gate hrtime_t last_delta; /* previous time delta */ 820Sstevel@tonic-gate hrtime_t last_delta2; /* previous 2nd order time delta */ 830Sstevel@tonic-gate } physmem_entsrc_t; 840Sstevel@tonic-gate 850Sstevel@tonic-gate static uint32_t srndpool[RNDPOOLSIZE/4]; /* Pool of random bits */ 861920Smcpowers static uint32_t buffer[RNDPOOLSIZE/4]; /* entropy mixed in later */ 871920Smcpowers static int buffer_bytes; /* bytes written to buffer */ 880Sstevel@tonic-gate static uint32_t entropy_bits; /* pool's current amount of entropy */ 890Sstevel@tonic-gate static kmutex_t srndpool_lock; /* protects r/w accesses to the pool, */ 900Sstevel@tonic-gate /* and the global variables */ 911920Smcpowers static kmutex_t buffer_lock; /* protects r/w accesses to buffer */ 920Sstevel@tonic-gate static kcondvar_t srndpool_read_cv; /* serializes poll/read syscalls */ 930Sstevel@tonic-gate static int pindex; /* Global index for adding/extracting */ 940Sstevel@tonic-gate /* from the pool */ 951920Smcpowers static int bstart, bindex; /* Global vars for adding/extracting */ 961920Smcpowers /* from the buffer */ 970Sstevel@tonic-gate static uint8_t leftover[HASHSIZE]; /* leftover output */ 980Sstevel@tonic-gate static int leftover_bytes; /* leftover length */ 990Sstevel@tonic-gate 1000Sstevel@tonic-gate static physmem_entsrc_t entsrc; /* Physical mem as an entropy source */ 1010Sstevel@tonic-gate static timeout_id_t rnd_timeout_id; 1020Sstevel@tonic-gate static int snum_waiters; 1030Sstevel@tonic-gate static crypto_kcf_provider_handle_t swrand_prov_handle = NULL; 1040Sstevel@tonic-gate swrand_stats_t swrand_stats; 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate static int physmem_ent_init(physmem_entsrc_t *); 1070Sstevel@tonic-gate static void physmem_ent_fini(physmem_entsrc_t *); 1080Sstevel@tonic-gate static void physmem_ent_gen(physmem_entsrc_t *); 1090Sstevel@tonic-gate static int physmem_parity_update(uint8_t *, uint32_t, int); 1100Sstevel@tonic-gate static void physmem_count_blocks(); 1110Sstevel@tonic-gate static void rnd_dr_callback_post_add(void *, pgcnt_t); 1120Sstevel@tonic-gate static int rnd_dr_callback_pre_del(void *, pgcnt_t); 1130Sstevel@tonic-gate static void rnd_dr_callback_post_del(void *, pgcnt_t, int); 1140Sstevel@tonic-gate static void rnd_handler(void *arg); 1150Sstevel@tonic-gate static void swrand_init(); 1160Sstevel@tonic-gate static void swrand_schedule_timeout(void); 1170Sstevel@tonic-gate static int swrand_get_entropy(uint8_t *ptr, size_t len, boolean_t); 1180Sstevel@tonic-gate static void swrand_add_entropy(uint8_t *ptr, size_t len, uint16_t entropy_est); 1191920Smcpowers static void swrand_add_entropy_later(uint8_t *ptr, size_t len); 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate /* Dynamic Reconfiguration related declarations */ 1220Sstevel@tonic-gate kphysm_setup_vector_t rnd_dr_callback_vec = { 1230Sstevel@tonic-gate KPHYSM_SETUP_VECTOR_VERSION, 1240Sstevel@tonic-gate rnd_dr_callback_post_add, 1250Sstevel@tonic-gate rnd_dr_callback_pre_del, 1260Sstevel@tonic-gate rnd_dr_callback_post_del 1270Sstevel@tonic-gate }; 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate extern struct mod_ops mod_cryptoops; 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate /* 1320Sstevel@tonic-gate * Module linkage information for the kernel. 1330Sstevel@tonic-gate */ 1340Sstevel@tonic-gate static struct modlcrypto modlcrypto = { 1350Sstevel@tonic-gate &mod_cryptoops, 136*5072Smcpowers "Kernel Random number Provider" 1370Sstevel@tonic-gate }; 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate static struct modlinkage modlinkage = { 1400Sstevel@tonic-gate MODREV_1, 1410Sstevel@tonic-gate (void *)&modlcrypto, 1420Sstevel@tonic-gate NULL 1430Sstevel@tonic-gate }; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate /* 1460Sstevel@tonic-gate * CSPI information (entry points, provider info, etc.) 1470Sstevel@tonic-gate */ 1480Sstevel@tonic-gate static void swrand_provider_status(crypto_provider_handle_t, uint_t *); 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate static crypto_control_ops_t swrand_control_ops = { 1510Sstevel@tonic-gate swrand_provider_status 1520Sstevel@tonic-gate }; 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate static int swrand_seed_random(crypto_provider_handle_t, crypto_session_id_t, 1551920Smcpowers uchar_t *, size_t, uint_t, uint32_t, crypto_req_handle_t); 1560Sstevel@tonic-gate static int swrand_generate_random(crypto_provider_handle_t, 1570Sstevel@tonic-gate crypto_session_id_t, uchar_t *, size_t, crypto_req_handle_t); 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate static crypto_random_number_ops_t swrand_random_number_ops = { 1600Sstevel@tonic-gate swrand_seed_random, 1610Sstevel@tonic-gate swrand_generate_random 1620Sstevel@tonic-gate }; 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate static crypto_ops_t swrand_crypto_ops = { 1650Sstevel@tonic-gate &swrand_control_ops, 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 NULL, 1730Sstevel@tonic-gate &swrand_random_number_ops, 1740Sstevel@tonic-gate NULL, 1750Sstevel@tonic-gate NULL, 1760Sstevel@tonic-gate NULL, 1770Sstevel@tonic-gate NULL, 1780Sstevel@tonic-gate NULL 1790Sstevel@tonic-gate }; 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate static crypto_provider_info_t swrand_prov_info = { 1820Sstevel@tonic-gate CRYPTO_SPI_VERSION_1, 1830Sstevel@tonic-gate "Kernel Random Number Provider", 1840Sstevel@tonic-gate CRYPTO_SW_PROVIDER, 1850Sstevel@tonic-gate {&modlinkage}, 1860Sstevel@tonic-gate NULL, 1870Sstevel@tonic-gate &swrand_crypto_ops, 1880Sstevel@tonic-gate 0, 1890Sstevel@tonic-gate NULL 1900Sstevel@tonic-gate }; 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate int 1930Sstevel@tonic-gate _init(void) 1940Sstevel@tonic-gate { 1950Sstevel@tonic-gate int ret; 1960Sstevel@tonic-gate hrtime_t ts; 1970Sstevel@tonic-gate time_t now; 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* 2000Sstevel@tonic-gate * Register with KCF. If the registration fails, return error. 2010Sstevel@tonic-gate */ 2020Sstevel@tonic-gate if ((ret = crypto_register_provider(&swrand_prov_info, 2030Sstevel@tonic-gate &swrand_prov_handle)) != CRYPTO_SUCCESS) { 2040Sstevel@tonic-gate cmn_err(CE_WARN, "swrand : Kernel Random Number Provider " 2050Sstevel@tonic-gate "disabled for /dev/random use"); 2060Sstevel@tonic-gate return (EACCES); 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate mutex_init(&srndpool_lock, NULL, MUTEX_DEFAULT, NULL); 2101920Smcpowers mutex_init(&buffer_lock, NULL, MUTEX_DEFAULT, NULL); 2110Sstevel@tonic-gate cv_init(&srndpool_read_cv, NULL, CV_DEFAULT, NULL); 2120Sstevel@tonic-gate entropy_bits = 0; 2130Sstevel@tonic-gate pindex = 0; 2141920Smcpowers bindex = 0; 2151920Smcpowers bstart = 0; 2160Sstevel@tonic-gate snum_waiters = 0; 2170Sstevel@tonic-gate leftover_bytes = 0; 2181920Smcpowers buffer_bytes = 0; 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate /* 2210Sstevel@tonic-gate * Initialize the pool using 2220Sstevel@tonic-gate * . 2 unpredictable times: high resolution time since the boot-time, 2230Sstevel@tonic-gate * and the current time-of-the day. 2240Sstevel@tonic-gate * . The initial physical memory state. 2250Sstevel@tonic-gate */ 2260Sstevel@tonic-gate ts = gethrtime(); 2270Sstevel@tonic-gate swrand_add_entropy((uint8_t *)&ts, sizeof (ts), 0); 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate (void) drv_getparm(TIME, &now); 2300Sstevel@tonic-gate swrand_add_entropy((uint8_t *)&now, sizeof (now), 0); 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate ret = kphysm_setup_func_register(&rnd_dr_callback_vec, NULL); 2330Sstevel@tonic-gate ASSERT(ret == 0); 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate if (physmem_ent_init(&entsrc) != 0) { 2360Sstevel@tonic-gate mutex_destroy(&srndpool_lock); 2371920Smcpowers mutex_destroy(&buffer_lock); 2380Sstevel@tonic-gate cv_destroy(&srndpool_read_cv); 2390Sstevel@tonic-gate (void) crypto_unregister_provider(swrand_prov_handle); 2400Sstevel@tonic-gate return (ENOMEM); 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate if ((ret = mod_install(&modlinkage)) != 0) { 2440Sstevel@tonic-gate mutex_destroy(&srndpool_lock); 2451920Smcpowers mutex_destroy(&buffer_lock); 2460Sstevel@tonic-gate cv_destroy(&srndpool_read_cv); 2470Sstevel@tonic-gate physmem_ent_fini(&entsrc); 2480Sstevel@tonic-gate (void) crypto_unregister_provider(swrand_prov_handle); 2490Sstevel@tonic-gate return (ret); 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate /* Schedule periodic mixing of the pool. */ 2530Sstevel@tonic-gate mutex_enter(&srndpool_lock); 2540Sstevel@tonic-gate swrand_schedule_timeout(); 2550Sstevel@tonic-gate mutex_exit(&srndpool_lock); 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate return (0); 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate int 2610Sstevel@tonic-gate _info(struct modinfo *modinfop) 2620Sstevel@tonic-gate { 2630Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate /* 2670Sstevel@tonic-gate * Control entry points. 2680Sstevel@tonic-gate */ 2690Sstevel@tonic-gate /* ARGSUSED */ 2700Sstevel@tonic-gate static void 2710Sstevel@tonic-gate swrand_provider_status(crypto_provider_handle_t provider, uint_t *status) 2720Sstevel@tonic-gate { 2730Sstevel@tonic-gate *status = CRYPTO_PROVIDER_READY; 2740Sstevel@tonic-gate } 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate /* 2770Sstevel@tonic-gate * Random number entry points. 2780Sstevel@tonic-gate */ 2790Sstevel@tonic-gate /* ARGSUSED */ 2800Sstevel@tonic-gate static int 2810Sstevel@tonic-gate swrand_seed_random(crypto_provider_handle_t provider, crypto_session_id_t sid, 2821920Smcpowers uchar_t *buf, size_t len, uint_t entropy_est, uint32_t flags, 2831920Smcpowers crypto_req_handle_t req) 2840Sstevel@tonic-gate { 2850Sstevel@tonic-gate /* The entropy estimate is always 0 in this path */ 2861920Smcpowers if (flags & CRYPTO_SEED_NOW) 2871920Smcpowers swrand_add_entropy(buf, len, 0); 2881920Smcpowers else 2891920Smcpowers swrand_add_entropy_later(buf, len); 2900Sstevel@tonic-gate return (CRYPTO_SUCCESS); 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate /* ARGSUSED */ 2940Sstevel@tonic-gate static int 2950Sstevel@tonic-gate swrand_generate_random(crypto_provider_handle_t provider, 2960Sstevel@tonic-gate crypto_session_id_t sid, uchar_t *buf, size_t len, crypto_req_handle_t req) 2970Sstevel@tonic-gate { 2980Sstevel@tonic-gate if (crypto_kmflag(req) == KM_NOSLEEP) 2990Sstevel@tonic-gate (void) swrand_get_entropy(buf, len, B_TRUE); 3000Sstevel@tonic-gate else 3010Sstevel@tonic-gate (void) swrand_get_entropy(buf, len, B_FALSE); 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate return (CRYPTO_SUCCESS); 3040Sstevel@tonic-gate } 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate /* 3080Sstevel@tonic-gate * Extraction of entropy from the pool. 3090Sstevel@tonic-gate * 3100Sstevel@tonic-gate * Returns "len" random bytes in *ptr. 3110Sstevel@tonic-gate * Try to gather some more entropy by calling physmem_ent_gen() when less than 3120Sstevel@tonic-gate * MINEXTRACTBITS are present in the pool. 3130Sstevel@tonic-gate * Will block if not enough entropy was available and the call is blocking. 3140Sstevel@tonic-gate */ 3150Sstevel@tonic-gate static int 3160Sstevel@tonic-gate swrand_get_entropy(uint8_t *ptr, size_t len, boolean_t nonblock) 3170Sstevel@tonic-gate { 3180Sstevel@tonic-gate int i, bytes; 3190Sstevel@tonic-gate HASH_CTX hashctx; 3200Sstevel@tonic-gate uint8_t digest[HASHSIZE], *pool; 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate mutex_enter(&srndpool_lock); 3230Sstevel@tonic-gate if (leftover_bytes > 0) { 3240Sstevel@tonic-gate bytes = min(len, leftover_bytes); 3250Sstevel@tonic-gate bcopy(leftover, ptr, bytes); 3260Sstevel@tonic-gate len -= bytes; 3270Sstevel@tonic-gate ptr += bytes; 3280Sstevel@tonic-gate leftover_bytes -= bytes; 3290Sstevel@tonic-gate if (leftover_bytes > 0) 3300Sstevel@tonic-gate ovbcopy(leftover+bytes, leftover, leftover_bytes); 3310Sstevel@tonic-gate } 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate while (len > 0) { 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate /* Check if there is enough entropy */ 3360Sstevel@tonic-gate while (entropy_bits < MINEXTRACTBITS) { 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate physmem_ent_gen(&entsrc); 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate if (entropy_bits < MINEXTRACTBITS && 3410Sstevel@tonic-gate nonblock == B_TRUE) { 3420Sstevel@tonic-gate mutex_exit(&srndpool_lock); 3430Sstevel@tonic-gate return (EAGAIN); 3440Sstevel@tonic-gate } 3450Sstevel@tonic-gate 3460Sstevel@tonic-gate if (entropy_bits < MINEXTRACTBITS) { 3470Sstevel@tonic-gate ASSERT(nonblock == B_FALSE); 3480Sstevel@tonic-gate snum_waiters++; 3490Sstevel@tonic-gate if (cv_wait_sig(&srndpool_read_cv, 3500Sstevel@tonic-gate &srndpool_lock) == 0) { 3510Sstevel@tonic-gate snum_waiters--; 3520Sstevel@tonic-gate mutex_exit(&srndpool_lock); 3530Sstevel@tonic-gate return (EINTR); 3540Sstevel@tonic-gate } 3550Sstevel@tonic-gate snum_waiters--; 3560Sstevel@tonic-gate } 3570Sstevel@tonic-gate } 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate /* Figure out how many bytes to extract */ 3600Sstevel@tonic-gate bytes = min(HASHSIZE, len); 3610Sstevel@tonic-gate bytes = min(bytes, entropy_bits/8); 3620Sstevel@tonic-gate entropy_bits -= bytes * 8; 3630Sstevel@tonic-gate BUMP_SWRAND_STATS(ss_entOut, bytes * 8); 3640Sstevel@tonic-gate swrand_stats.ss_entEst = entropy_bits; 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate /* Extract entropy by hashing pool content */ 3670Sstevel@tonic-gate HashInit(&hashctx); 3680Sstevel@tonic-gate HashUpdate(&hashctx, (uint8_t *)srndpool, RNDPOOLSIZE); 3690Sstevel@tonic-gate HashFinal(digest, &hashctx); 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate /* 3720Sstevel@tonic-gate * Feed the digest back into the pool so next 3730Sstevel@tonic-gate * extraction produces different result 3740Sstevel@tonic-gate */ 3750Sstevel@tonic-gate pool = (uint8_t *)srndpool; 3760Sstevel@tonic-gate for (i = 0; i < HASHSIZE; i++) { 3770Sstevel@tonic-gate pool[pindex++] ^= digest[i]; 3780Sstevel@tonic-gate /* pindex modulo RNDPOOLSIZE */ 3790Sstevel@tonic-gate pindex &= (RNDPOOLSIZE - 1); 3800Sstevel@tonic-gate } 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate /* 3830Sstevel@tonic-gate * Hash the digest again before output to obscure 3840Sstevel@tonic-gate * what was fed back to the pool. 3850Sstevel@tonic-gate */ 3860Sstevel@tonic-gate HashInit(&hashctx); 3870Sstevel@tonic-gate HashUpdate(&hashctx, digest, HASHSIZE); 3880Sstevel@tonic-gate if (len >= HASHSIZE) 3890Sstevel@tonic-gate HashFinal(ptr, &hashctx); 3900Sstevel@tonic-gate else { 3910Sstevel@tonic-gate HashFinal(digest, &hashctx); 3920Sstevel@tonic-gate bcopy(digest, ptr, bytes); 3930Sstevel@tonic-gate leftover_bytes = HASHSIZE - bytes; 3940Sstevel@tonic-gate bcopy(digest + bytes, leftover, leftover_bytes); 3950Sstevel@tonic-gate } 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate len -= bytes; 3980Sstevel@tonic-gate ptr += bytes; 3990Sstevel@tonic-gate BUMP_SWRAND_STATS(ss_bytesOut, bytes); 4000Sstevel@tonic-gate } 4010Sstevel@tonic-gate mutex_exit(&srndpool_lock); 4020Sstevel@tonic-gate return (0); 4030Sstevel@tonic-gate } 4040Sstevel@tonic-gate 4051920Smcpowers #define SWRAND_ADD_BYTES(ptr, len, i, pool) \ 4061920Smcpowers ASSERT((ptr) != NULL && (len) > 0); \ 4071920Smcpowers BUMP_SWRAND_STATS(ss_bytesIn, (len)); \ 4081920Smcpowers while ((len)--) { \ 4091920Smcpowers (pool)[(i)++] ^= *(ptr); \ 4101920Smcpowers (ptr)++; \ 4111920Smcpowers (i) &= (RNDPOOLSIZE - 1); \ 4121920Smcpowers } 4131920Smcpowers 4140Sstevel@tonic-gate /* Write some more user-provided entropy to the pool */ 4150Sstevel@tonic-gate static void 4160Sstevel@tonic-gate swrand_add_bytes(uint8_t *ptr, size_t len) 4170Sstevel@tonic-gate { 4180Sstevel@tonic-gate uint8_t *pool = (uint8_t *)srndpool; 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate ASSERT(MUTEX_HELD(&srndpool_lock)); 4211920Smcpowers SWRAND_ADD_BYTES(ptr, len, pindex, pool); 4221920Smcpowers } 4230Sstevel@tonic-gate 4241920Smcpowers /* 4251920Smcpowers * Add bytes to buffer. Adding the buffer to the random pool 4261920Smcpowers * is deferred until the random pool is mixed. 4271920Smcpowers */ 4281920Smcpowers static void 4291920Smcpowers swrand_add_bytes_later(uint8_t *ptr, size_t len) 4301920Smcpowers { 4311920Smcpowers uint8_t *pool = (uint8_t *)buffer; 4321920Smcpowers 4331920Smcpowers ASSERT(MUTEX_HELD(&buffer_lock)); 4341920Smcpowers SWRAND_ADD_BYTES(ptr, len, bindex, pool); 4351920Smcpowers buffer_bytes += len; 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate 4381920Smcpowers #undef SWRAND_ADD_BYTES 4391920Smcpowers 4400Sstevel@tonic-gate /* Mix the pool */ 4410Sstevel@tonic-gate static void 4420Sstevel@tonic-gate swrand_mix_pool(uint16_t entropy_est) 4430Sstevel@tonic-gate { 4440Sstevel@tonic-gate int i, j, k, start; 4450Sstevel@tonic-gate HASH_CTX hashctx; 4460Sstevel@tonic-gate uint8_t digest[HASHSIZE]; 4470Sstevel@tonic-gate uint8_t *pool = (uint8_t *)srndpool; 4481920Smcpowers uint8_t *bp = (uint8_t *)buffer; 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate ASSERT(MUTEX_HELD(&srndpool_lock)); 4510Sstevel@tonic-gate 4521920Smcpowers /* add deferred bytes */ 4531920Smcpowers mutex_enter(&buffer_lock); 4541920Smcpowers if (buffer_bytes > 0) { 4551920Smcpowers if (buffer_bytes >= RNDPOOLSIZE) { 4561920Smcpowers for (i = 0; i < RNDPOOLSIZE/4; i++) { 4571920Smcpowers srndpool[i] ^= buffer[i]; 4581920Smcpowers buffer[i] = 0; 4591920Smcpowers } 4601920Smcpowers bstart = bindex = 0; 4611920Smcpowers } else { 4621920Smcpowers for (i = 0; i < buffer_bytes; i++) { 4631920Smcpowers pool[pindex++] ^= bp[bstart]; 4641920Smcpowers bp[bstart++] = 0; 4651920Smcpowers pindex &= (RNDPOOLSIZE - 1); 4661920Smcpowers bstart &= (RNDPOOLSIZE - 1); 4671920Smcpowers } 4681920Smcpowers ASSERT(bstart == bindex); 4691920Smcpowers } 4701920Smcpowers buffer_bytes = 0; 4711920Smcpowers } 4721920Smcpowers mutex_exit(&buffer_lock); 4731920Smcpowers 4740Sstevel@tonic-gate start = 0; 4750Sstevel@tonic-gate for (i = 0; i < RNDPOOLSIZE/HASHSIZE + 1; i++) { 4760Sstevel@tonic-gate HashInit(&hashctx); 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate /* Hash a buffer centered on a block in the pool */ 4790Sstevel@tonic-gate if (start + HASHBUFSIZE <= RNDPOOLSIZE) 4800Sstevel@tonic-gate HashUpdate(&hashctx, &pool[start], HASHBUFSIZE); 4810Sstevel@tonic-gate else { 4820Sstevel@tonic-gate HashUpdate(&hashctx, &pool[start], 4830Sstevel@tonic-gate RNDPOOLSIZE - start); 4840Sstevel@tonic-gate HashUpdate(&hashctx, pool, 4850Sstevel@tonic-gate HASHBUFSIZE - RNDPOOLSIZE + start); 4860Sstevel@tonic-gate } 4870Sstevel@tonic-gate HashFinal(digest, &hashctx); 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate /* XOR the hash result back into the block */ 4900Sstevel@tonic-gate k = (start + HASHSIZE) & (RNDPOOLSIZE - 1); 4910Sstevel@tonic-gate for (j = 0; j < HASHSIZE; j++) { 4920Sstevel@tonic-gate pool[k++] ^= digest[j]; 4930Sstevel@tonic-gate k &= (RNDPOOLSIZE - 1); 4940Sstevel@tonic-gate } 4950Sstevel@tonic-gate 4960Sstevel@tonic-gate /* Slide the hash buffer and repeat with next block */ 4970Sstevel@tonic-gate start = (start + HASHSIZE) & (RNDPOOLSIZE - 1); 4980Sstevel@tonic-gate } 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate entropy_bits += entropy_est; 5010Sstevel@tonic-gate if (entropy_bits > RNDPOOLSIZE * 8) 5020Sstevel@tonic-gate entropy_bits = RNDPOOLSIZE * 8; 5030Sstevel@tonic-gate 5040Sstevel@tonic-gate swrand_stats.ss_entEst = entropy_bits; 5050Sstevel@tonic-gate BUMP_SWRAND_STATS(ss_entIn, entropy_est); 5060Sstevel@tonic-gate } 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate static void 5091920Smcpowers swrand_add_entropy_later(uint8_t *ptr, size_t len) 5101920Smcpowers { 5111920Smcpowers mutex_enter(&buffer_lock); 5121920Smcpowers swrand_add_bytes_later(ptr, len); 5131920Smcpowers mutex_exit(&buffer_lock); 5141920Smcpowers } 5151920Smcpowers 5161920Smcpowers static void 5170Sstevel@tonic-gate swrand_add_entropy(uint8_t *ptr, size_t len, uint16_t entropy_est) 5180Sstevel@tonic-gate { 5190Sstevel@tonic-gate mutex_enter(&srndpool_lock); 5200Sstevel@tonic-gate swrand_add_bytes(ptr, len); 5210Sstevel@tonic-gate swrand_mix_pool(entropy_est); 5220Sstevel@tonic-gate mutex_exit(&srndpool_lock); 5230Sstevel@tonic-gate } 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate /* 5260Sstevel@tonic-gate * The physmem_* routines below generate entropy by reading blocks of 5270Sstevel@tonic-gate * physical memory. Entropy is gathered in a couple of ways: 5280Sstevel@tonic-gate * 5290Sstevel@tonic-gate * - By reading blocks of physical memory and detecting if changes 5300Sstevel@tonic-gate * occurred in the blocks read. 5310Sstevel@tonic-gate * 5320Sstevel@tonic-gate * - By measuring the time it takes to load and hash a block of memory 5330Sstevel@tonic-gate * and computing the differences in the measured time. 5340Sstevel@tonic-gate * 5350Sstevel@tonic-gate * The first method was used in the CryptoRand implementation. Physical 5360Sstevel@tonic-gate * memory is divided into blocks of fixed size. A block of memory is 5370Sstevel@tonic-gate * chosen from the possible blocks and hashed to produce a digest. This 5380Sstevel@tonic-gate * digest is then mixed into the pool. A single bit from the digest is 5390Sstevel@tonic-gate * used as a parity bit or "checksum" and compared against the previous 5400Sstevel@tonic-gate * "checksum" computed for the block. If the single-bit checksum has not 5410Sstevel@tonic-gate * changed, no entropy is credited to the pool. If there is a change, 5420Sstevel@tonic-gate * then the assumption is that at least one bit in the block has changed. 5430Sstevel@tonic-gate * The possible locations within the memory block of where the bit change 5440Sstevel@tonic-gate * occurred is used as a measure of entropy. For example, if a block 5450Sstevel@tonic-gate * size of 4096 bytes is used, about log_2(4096*8)=15 bits worth of 5460Sstevel@tonic-gate * entropy is available. Because the single-bit checksum will miss half 5470Sstevel@tonic-gate * of the changes, the amount of entropy credited to the pool is doubled 5480Sstevel@tonic-gate * when a change is detected. With a 4096 byte block size, a block 5490Sstevel@tonic-gate * change will add a total of 30 bits of entropy to the pool. 5500Sstevel@tonic-gate * 5510Sstevel@tonic-gate * The second method measures the amount of time it takes to read and 5520Sstevel@tonic-gate * hash a physical memory block (as described above). The time measured 5530Sstevel@tonic-gate * can vary depending on system load, scheduling and other factors. 5540Sstevel@tonic-gate * Differences between consecutive measurements are computed to come up 5550Sstevel@tonic-gate * with an entropy estimate. The first, second, and third order delta is 5560Sstevel@tonic-gate * calculated to determine the minimum delta value. The number of bits 5570Sstevel@tonic-gate * present in this minimum delta value is the entropy estimate. This 5580Sstevel@tonic-gate * entropy estimation technique using time deltas is similar to that used 5590Sstevel@tonic-gate * in /dev/random implementations from Linux/BSD. 5600Sstevel@tonic-gate */ 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate static int 5630Sstevel@tonic-gate physmem_ent_init(physmem_entsrc_t *entsrc) 5640Sstevel@tonic-gate { 5650Sstevel@tonic-gate uint8_t *ptr; 5660Sstevel@tonic-gate int i; 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate bzero(entsrc, sizeof (*entsrc)); 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate /* 5710Sstevel@tonic-gate * The maximum entropy amount in bits per block of memory read is 5720Sstevel@tonic-gate * log_2(MEMBLOCKSIZE * 8); 5730Sstevel@tonic-gate */ 5740Sstevel@tonic-gate i = MEMBLOCKSIZE << 3; 5750Sstevel@tonic-gate while (i >>= 1) 5760Sstevel@tonic-gate entsrc->entperblock++; 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate /* Initialize entsrc->nblocks */ 5790Sstevel@tonic-gate physmem_count_blocks(); 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate if (entsrc->nblocks == 0) { 5820Sstevel@tonic-gate cmn_err(CE_WARN, "no memory blocks to scan!"); 5830Sstevel@tonic-gate return (-1); 5840Sstevel@tonic-gate } 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate /* Allocate space for the parity vector and memory page */ 5870Sstevel@tonic-gate entsrc->parity = kmem_alloc(howmany(entsrc->nblocks, 8), 5880Sstevel@tonic-gate KM_SLEEP); 5890Sstevel@tonic-gate entsrc->pmbuf = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP); 5900Sstevel@tonic-gate 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate /* Initialize parity vector with bits from the pool */ 5930Sstevel@tonic-gate i = howmany(entsrc->nblocks, 8); 5940Sstevel@tonic-gate ptr = entsrc->parity; 5950Sstevel@tonic-gate while (i > 0) { 5960Sstevel@tonic-gate if (i > RNDPOOLSIZE) { 5970Sstevel@tonic-gate bcopy(srndpool, ptr, RNDPOOLSIZE); 5980Sstevel@tonic-gate mutex_enter(&srndpool_lock); 5990Sstevel@tonic-gate swrand_mix_pool(0); 6000Sstevel@tonic-gate mutex_exit(&srndpool_lock); 6010Sstevel@tonic-gate ptr += RNDPOOLSIZE; 6020Sstevel@tonic-gate i -= RNDPOOLSIZE; 6030Sstevel@tonic-gate } else { 6040Sstevel@tonic-gate bcopy(srndpool, ptr, i); 6050Sstevel@tonic-gate break; 6060Sstevel@tonic-gate } 6070Sstevel@tonic-gate } 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate /* Generate some entropy to further initialize the pool */ 6100Sstevel@tonic-gate mutex_enter(&srndpool_lock); 6110Sstevel@tonic-gate physmem_ent_gen(entsrc); 6120Sstevel@tonic-gate entropy_bits = 0; 6130Sstevel@tonic-gate mutex_exit(&srndpool_lock); 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate return (0); 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate static void 6190Sstevel@tonic-gate physmem_ent_fini(physmem_entsrc_t *entsrc) 6200Sstevel@tonic-gate { 6210Sstevel@tonic-gate if (entsrc->pmbuf != NULL) 6220Sstevel@tonic-gate vmem_free(heap_arena, entsrc->pmbuf, PAGESIZE); 6230Sstevel@tonic-gate if (entsrc->parity != NULL) 6240Sstevel@tonic-gate kmem_free(entsrc->parity, howmany(entsrc->nblocks, 8)); 6250Sstevel@tonic-gate bzero(entsrc, sizeof (*entsrc)); 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate static void 6290Sstevel@tonic-gate physmem_ent_gen(physmem_entsrc_t *entsrc) 6300Sstevel@tonic-gate { 6310Sstevel@tonic-gate struct memlist *pmem; 6320Sstevel@tonic-gate offset_t offset, poffset; 6330Sstevel@tonic-gate pfn_t pfn; 6340Sstevel@tonic-gate int i, nbytes, len, ent = 0; 6350Sstevel@tonic-gate uint32_t block, oblock; 6360Sstevel@tonic-gate hrtime_t ts1, ts2, diff, delta, delta2, delta3; 6370Sstevel@tonic-gate uint8_t digest[HASHSIZE]; 6380Sstevel@tonic-gate HASH_CTX ctx; 6393446Smrj page_t *pp; 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate /* 6420Sstevel@tonic-gate * Use each 32-bit quantity in the pool to pick a memory 6430Sstevel@tonic-gate * block to read. 6440Sstevel@tonic-gate */ 6450Sstevel@tonic-gate for (i = 0; i < RNDPOOLSIZE/4; i++) { 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate /* If the pool is "full", stop after one block */ 6480Sstevel@tonic-gate if (entropy_bits + ent >= RNDPOOLSIZE * 8) { 6490Sstevel@tonic-gate if (i > 0) 6500Sstevel@tonic-gate break; 6510Sstevel@tonic-gate } 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate /* 6540Sstevel@tonic-gate * This lock protects reading of phys_install. 6550Sstevel@tonic-gate * Any changes to this list, by DR, are done while 6560Sstevel@tonic-gate * holding this lock. So, holding this lock is sufficient 6570Sstevel@tonic-gate * to handle DR also. 6580Sstevel@tonic-gate */ 6590Sstevel@tonic-gate memlist_read_lock(); 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate /* We're left with less than 4K of memory after DR */ 6620Sstevel@tonic-gate ASSERT(entsrc->nblocks > 0); 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate /* Pick a memory block to read */ 6650Sstevel@tonic-gate block = oblock = srndpool[i] % entsrc->nblocks; 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate for (pmem = phys_install; pmem != NULL; pmem = pmem->next) { 6680Sstevel@tonic-gate if (block < pmem->size / MEMBLOCKSIZE) 6690Sstevel@tonic-gate break; 6700Sstevel@tonic-gate block -= pmem->size / MEMBLOCKSIZE; 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate ASSERT(pmem != NULL); 6740Sstevel@tonic-gate 6750Sstevel@tonic-gate offset = pmem->address + block * MEMBLOCKSIZE; 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate if (!address_in_memlist(phys_install, offset, MEMBLOCKSIZE)) { 6780Sstevel@tonic-gate memlist_read_unlock(); 6790Sstevel@tonic-gate continue; 6800Sstevel@tonic-gate } 6810Sstevel@tonic-gate 6820Sstevel@tonic-gate /* 6833446Smrj * Do an initial check to see if the address is safe 6843446Smrj */ 6853446Smrj if (plat_hold_page(offset >> PAGESHIFT, PLAT_HOLD_NO_LOCK, NULL) 6863446Smrj == PLAT_HOLD_FAIL) { 6873446Smrj memlist_read_unlock(); 6883446Smrj continue; 6893446Smrj } 6903446Smrj 6913446Smrj /* 6920Sstevel@tonic-gate * Figure out which page to load to read the 6930Sstevel@tonic-gate * memory block. Load the page and compute the 6940Sstevel@tonic-gate * hash of the memory block. 6950Sstevel@tonic-gate */ 6960Sstevel@tonic-gate len = MEMBLOCKSIZE; 6970Sstevel@tonic-gate ts1 = gethrtime(); 6980Sstevel@tonic-gate HashInit(&ctx); 6990Sstevel@tonic-gate while (len) { 7000Sstevel@tonic-gate pfn = offset >> PAGESHIFT; 7010Sstevel@tonic-gate poffset = offset & PAGEOFFSET; 7020Sstevel@tonic-gate nbytes = PAGESIZE - poffset < len ? 7030Sstevel@tonic-gate PAGESIZE - poffset : len; 7040Sstevel@tonic-gate 7053446Smrj /* 7063446Smrj * Re-check the offset, and lock the frame. If the 7073446Smrj * page was given away after the above check, we'll 7083446Smrj * just bail out. 7093446Smrj */ 7103446Smrj if (plat_hold_page(pfn, PLAT_HOLD_LOCK, &pp) == 7113446Smrj PLAT_HOLD_FAIL) 7123446Smrj break; 7133446Smrj 7140Sstevel@tonic-gate hat_devload(kas.a_hat, entsrc->pmbuf, 7150Sstevel@tonic-gate PAGESIZE, pfn, PROT_READ, 7160Sstevel@tonic-gate HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK); 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate HashUpdate(&ctx, (uint8_t *)entsrc->pmbuf + poffset, 7190Sstevel@tonic-gate nbytes); 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate hat_unload(kas.a_hat, entsrc->pmbuf, PAGESIZE, 7220Sstevel@tonic-gate HAT_UNLOAD_UNLOCK); 7230Sstevel@tonic-gate 7243446Smrj plat_release_page(pp); 7253446Smrj 7260Sstevel@tonic-gate len -= nbytes; 7270Sstevel@tonic-gate offset += nbytes; 7280Sstevel@tonic-gate } 7290Sstevel@tonic-gate /* We got our pages. Let the DR roll */ 7300Sstevel@tonic-gate memlist_read_unlock(); 7310Sstevel@tonic-gate 7323446Smrj /* See if we had to bail out due to a page being given away */ 7333446Smrj if (len) 7343446Smrj continue; 7353446Smrj 7360Sstevel@tonic-gate HashFinal(digest, &ctx); 7370Sstevel@tonic-gate ts2 = gethrtime(); 7380Sstevel@tonic-gate 7390Sstevel@tonic-gate /* 7400Sstevel@tonic-gate * Compute the time it took to load and hash the 7410Sstevel@tonic-gate * block and compare it against the previous 7420Sstevel@tonic-gate * measurement. The delta of the time values 7430Sstevel@tonic-gate * provides a small amount of entropy. The 7440Sstevel@tonic-gate * minimum of the first, second, and third order 7450Sstevel@tonic-gate * delta is used to estimate how much entropy 7460Sstevel@tonic-gate * is present. 7470Sstevel@tonic-gate */ 7480Sstevel@tonic-gate diff = ts2 - ts1; 7490Sstevel@tonic-gate delta = diff - entsrc->last_diff; 7500Sstevel@tonic-gate if (delta < 0) 7510Sstevel@tonic-gate delta = -delta; 7520Sstevel@tonic-gate delta2 = delta - entsrc->last_delta; 7530Sstevel@tonic-gate if (delta2 < 0) 7540Sstevel@tonic-gate delta2 = -delta2; 7550Sstevel@tonic-gate delta3 = delta2 - entsrc->last_delta2; 7560Sstevel@tonic-gate if (delta3 < 0) 7570Sstevel@tonic-gate delta3 = -delta3; 7580Sstevel@tonic-gate entsrc->last_diff = diff; 7590Sstevel@tonic-gate entsrc->last_delta = delta; 7600Sstevel@tonic-gate entsrc->last_delta2 = delta2; 7610Sstevel@tonic-gate 7620Sstevel@tonic-gate if (delta > delta2) 7630Sstevel@tonic-gate delta = delta2; 7640Sstevel@tonic-gate if (delta > delta3) 7650Sstevel@tonic-gate delta = delta3; 7660Sstevel@tonic-gate delta2 = 0; 7670Sstevel@tonic-gate while (delta >>= 1) 7680Sstevel@tonic-gate delta2++; 7690Sstevel@tonic-gate ent += delta2; 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate /* 7720Sstevel@tonic-gate * If the memory block has changed, credit the pool with 7730Sstevel@tonic-gate * the entropy estimate. The entropy estimate is doubled 7740Sstevel@tonic-gate * because the single-bit checksum misses half the change 7750Sstevel@tonic-gate * on average. 7760Sstevel@tonic-gate */ 7770Sstevel@tonic-gate if (physmem_parity_update(entsrc->parity, oblock, 7780Sstevel@tonic-gate digest[0] & 1)) 7790Sstevel@tonic-gate ent += 2 * entsrc->entperblock; 7800Sstevel@tonic-gate 7810Sstevel@tonic-gate /* Add the entropy bytes to the pool */ 7820Sstevel@tonic-gate swrand_add_bytes(digest, HASHSIZE); 7830Sstevel@tonic-gate swrand_add_bytes((uint8_t *)&ts1, sizeof (ts1)); 7840Sstevel@tonic-gate swrand_add_bytes((uint8_t *)&ts2, sizeof (ts2)); 7850Sstevel@tonic-gate } 7860Sstevel@tonic-gate 7870Sstevel@tonic-gate swrand_mix_pool(ent); 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate static int 7910Sstevel@tonic-gate physmem_parity_update(uint8_t *parity_vec, uint32_t block, int parity) 7920Sstevel@tonic-gate { 7930Sstevel@tonic-gate /* Test and set the parity bit, return 1 if changed */ 7940Sstevel@tonic-gate if (parity == ((parity_vec[block >> 3] >> (block & 7)) & 1)) 7950Sstevel@tonic-gate return (0); 7960Sstevel@tonic-gate parity_vec[block >> 3] ^= 1 << (block & 7); 7970Sstevel@tonic-gate return (1); 7980Sstevel@tonic-gate } 7990Sstevel@tonic-gate 8000Sstevel@tonic-gate /* Compute number of memory blocks available to scan */ 8010Sstevel@tonic-gate static void 8020Sstevel@tonic-gate physmem_count_blocks() 8030Sstevel@tonic-gate { 8040Sstevel@tonic-gate struct memlist *pmem; 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate memlist_read_lock(); 8070Sstevel@tonic-gate entsrc.nblocks = 0; 8080Sstevel@tonic-gate for (pmem = phys_install; pmem != NULL; pmem = pmem->next) { 8090Sstevel@tonic-gate entsrc.nblocks += pmem->size / MEMBLOCKSIZE; 8100Sstevel@tonic-gate if (entsrc.nblocks > MAXMEMBLOCKS) { 8110Sstevel@tonic-gate entsrc.nblocks = MAXMEMBLOCKS; 8120Sstevel@tonic-gate break; 8130Sstevel@tonic-gate } 8140Sstevel@tonic-gate } 8150Sstevel@tonic-gate memlist_read_unlock(); 8160Sstevel@tonic-gate } 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate /* 8190Sstevel@tonic-gate * Dynamic Reconfiguration call-back functions 8200Sstevel@tonic-gate */ 8210Sstevel@tonic-gate 8220Sstevel@tonic-gate /* ARGSUSED */ 8230Sstevel@tonic-gate static void 8240Sstevel@tonic-gate rnd_dr_callback_post_add(void *arg, pgcnt_t delta) 8250Sstevel@tonic-gate { 8260Sstevel@tonic-gate /* More memory is available now, so update entsrc->nblocks. */ 8270Sstevel@tonic-gate physmem_count_blocks(); 8280Sstevel@tonic-gate } 8290Sstevel@tonic-gate 8300Sstevel@tonic-gate /* Call-back routine invoked before the DR starts a memory removal. */ 8310Sstevel@tonic-gate /* ARGSUSED */ 8320Sstevel@tonic-gate static int 8330Sstevel@tonic-gate rnd_dr_callback_pre_del(void *arg, pgcnt_t delta) 8340Sstevel@tonic-gate { 8350Sstevel@tonic-gate return (0); 8360Sstevel@tonic-gate } 8370Sstevel@tonic-gate 8380Sstevel@tonic-gate /* Call-back routine invoked after the DR starts a memory removal. */ 8390Sstevel@tonic-gate /* ARGSUSED */ 8400Sstevel@tonic-gate static void 8410Sstevel@tonic-gate rnd_dr_callback_post_del(void *arg, pgcnt_t delta, int cancelled) 8420Sstevel@tonic-gate { 8430Sstevel@tonic-gate /* Memory has shrunk, so update entsrc->nblocks. */ 8440Sstevel@tonic-gate physmem_count_blocks(); 8450Sstevel@tonic-gate } 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate /* Timeout handling to gather entropy from physmem events */ 8480Sstevel@tonic-gate static void 8490Sstevel@tonic-gate swrand_schedule_timeout(void) 8500Sstevel@tonic-gate { 8510Sstevel@tonic-gate clock_t ut; /* time in microseconds */ 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate ASSERT(MUTEX_HELD(&srndpool_lock)); 8540Sstevel@tonic-gate /* 8550Sstevel@tonic-gate * The new timeout value is taken from the pool of random bits. 8560Sstevel@tonic-gate * We're merely reading the first 32 bits from the pool here, not 8570Sstevel@tonic-gate * consuming any entropy. 8580Sstevel@tonic-gate * This routine is usually called right after stirring the pool, so 8590Sstevel@tonic-gate * srndpool[0] will have a *fresh* random value each time. 8600Sstevel@tonic-gate * The timeout multiplier value is a random value between 0.7 sec and 8610Sstevel@tonic-gate * 1.748575 sec (0.7 sec + 0xFFFFF microseconds). 8620Sstevel@tonic-gate * The new timeout is TIMEOUT_INTERVAL times that multiplier. 8630Sstevel@tonic-gate */ 8640Sstevel@tonic-gate ut = 700000 + (clock_t)(srndpool[0] & 0xFFFFF); 8650Sstevel@tonic-gate rnd_timeout_id = timeout(rnd_handler, NULL, 8660Sstevel@tonic-gate TIMEOUT_INTERVAL * drv_usectohz(ut)); 8670Sstevel@tonic-gate } 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate /*ARGSUSED*/ 8700Sstevel@tonic-gate static void 8710Sstevel@tonic-gate rnd_handler(void *arg) 8720Sstevel@tonic-gate { 8730Sstevel@tonic-gate mutex_enter(&srndpool_lock); 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate physmem_ent_gen(&entsrc); 8760Sstevel@tonic-gate if (snum_waiters > 0) 8770Sstevel@tonic-gate cv_broadcast(&srndpool_read_cv); 8780Sstevel@tonic-gate swrand_schedule_timeout(); 8790Sstevel@tonic-gate 8800Sstevel@tonic-gate mutex_exit(&srndpool_lock); 8810Sstevel@tonic-gate } 882