1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <sys/mdb_modapi.h>
28*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_ctf.h>
29*7c478bd9Sstevel@tonic-gate #include <sys/random.h>
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate  * rnd_stats dcmd - Print out the global rnd_stats structure, nicely formatted.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
35*7c478bd9Sstevel@tonic-gate static int
rnd_get_stats(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)36*7c478bd9Sstevel@tonic-gate rnd_get_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
37*7c478bd9Sstevel@tonic-gate {
38*7c478bd9Sstevel@tonic-gate 	rnd_stats_t rnd_stats, rnd_stats_cpu;
39*7c478bd9Sstevel@tonic-gate 	uint32_t random_max_ncpus;
40*7c478bd9Sstevel@tonic-gate 	size_t rndmag_t_size;
41*7c478bd9Sstevel@tonic-gate 	ulong_t rndmag_t_offset;
42*7c478bd9Sstevel@tonic-gate 	uintptr_t rndmag;
43*7c478bd9Sstevel@tonic-gate 	mdb_ctf_id_t rndmag_id;
44*7c478bd9Sstevel@tonic-gate 	int i;
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
47*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 	if (mdb_readvar(&rnd_stats, "rnd_stats") == -1) {
50*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read rnd_stats structure");
51*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
52*7c478bd9Sstevel@tonic-gate 	}
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	if ((mdb_ctf_lookup_by_name("rndmag_t", &rndmag_id) != 0) ||
55*7c478bd9Sstevel@tonic-gate 	    (mdb_ctf_offsetof(rndmag_id, "rm_stats", &rndmag_t_offset) != 0) ||
56*7c478bd9Sstevel@tonic-gate 	    (mdb_readvar(&random_max_ncpus, "random_max_ncpus") == -1) ||
57*7c478bd9Sstevel@tonic-gate 	    (mdb_readvar(&rndmag, "rndmag") == -1) ||
58*7c478bd9Sstevel@tonic-gate 	    ((rndmag_t_size = mdb_ctf_type_size(rndmag_id)) == 0)) {
59*7c478bd9Sstevel@tonic-gate 		/* Can't find per-cpu stats.  Don't add them in. */
60*7c478bd9Sstevel@tonic-gate 		random_max_ncpus = 0;
61*7c478bd9Sstevel@tonic-gate 	}
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	rndmag_t_offset /= 8;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	/*
66*7c478bd9Sstevel@tonic-gate 	 * Read and aggregate per-cpu stats if we have them.
67*7c478bd9Sstevel@tonic-gate 	 */
68*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < random_max_ncpus; i++) {
69*7c478bd9Sstevel@tonic-gate 		mdb_vread(&rnd_stats_cpu, sizeof (rnd_stats_cpu),
70*7c478bd9Sstevel@tonic-gate 		    rndmag + rndmag_t_offset + i * rndmag_t_size);
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 		rnd_stats.rs_rndOut += rnd_stats_cpu.rs_rndOut;
73*7c478bd9Sstevel@tonic-gate 		rnd_stats.rs_rndcOut += rnd_stats_cpu.rs_rndcOut;
74*7c478bd9Sstevel@tonic-gate 		rnd_stats.rs_urndOut += rnd_stats_cpu.rs_urndOut;
75*7c478bd9Sstevel@tonic-gate 	}
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	mdb_printf("Random number device statistics:\n");
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	mdb_printf("%8llu bytes generated for /dev/random\n",
80*7c478bd9Sstevel@tonic-gate 	    rnd_stats.rs_rndOut);
81*7c478bd9Sstevel@tonic-gate 	mdb_printf("%8llu bytes read from /dev/random cache\n",
82*7c478bd9Sstevel@tonic-gate 	    rnd_stats.rs_rndcOut);
83*7c478bd9Sstevel@tonic-gate 	mdb_printf("%8llu bytes generated for /dev/urandom\n",
84*7c478bd9Sstevel@tonic-gate 	    rnd_stats.rs_urndOut);
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate /*
90*7c478bd9Sstevel@tonic-gate  * swrand_stats dcmd - Print out the global swrand_stats structure,
91*7c478bd9Sstevel@tonic-gate  * nicely formatted.
92*7c478bd9Sstevel@tonic-gate  */
93*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
94*7c478bd9Sstevel@tonic-gate static int
swrand_get_stats(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)95*7c478bd9Sstevel@tonic-gate swrand_get_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	swrand_stats_t swrand_stats;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
100*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	if (mdb_readvar(&swrand_stats, "swrand_stats") == -1) {
103*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read swrand_stats structure");
104*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
105*7c478bd9Sstevel@tonic-gate 	}
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	mdb_printf("Software-based Random number generator statistics:\n");
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	mdb_printf("%8u bits of entropy estimate\n", swrand_stats.ss_entEst);
110*7c478bd9Sstevel@tonic-gate 	mdb_printf("%8llu bits of entropy added to the pool\n",
111*7c478bd9Sstevel@tonic-gate 	    swrand_stats.ss_entIn);
112*7c478bd9Sstevel@tonic-gate 	mdb_printf("%8llu bits of entropy extracted from the pool\n",
113*7c478bd9Sstevel@tonic-gate 	    swrand_stats.ss_entOut);
114*7c478bd9Sstevel@tonic-gate 	mdb_printf("%8llu bytes added to the random pool\n",
115*7c478bd9Sstevel@tonic-gate 	    swrand_stats.ss_bytesIn);
116*7c478bd9Sstevel@tonic-gate 	mdb_printf("%8llu bytes extracted from the random pool\n",
117*7c478bd9Sstevel@tonic-gate 	    swrand_stats.ss_bytesOut);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
120*7c478bd9Sstevel@tonic-gate }
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
123*7c478bd9Sstevel@tonic-gate 	{ "rnd_stats",
124*7c478bd9Sstevel@tonic-gate 	    NULL,
125*7c478bd9Sstevel@tonic-gate 	    "print random number device statistics",
126*7c478bd9Sstevel@tonic-gate 	    rnd_get_stats },
127*7c478bd9Sstevel@tonic-gate 	{ "swrand_stats",
128*7c478bd9Sstevel@tonic-gate 	    NULL,
129*7c478bd9Sstevel@tonic-gate 	    "print kernel random number provider statistics",
130*7c478bd9Sstevel@tonic-gate 	    swrand_get_stats },
131*7c478bd9Sstevel@tonic-gate 	{ NULL }
132*7c478bd9Sstevel@tonic-gate };
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate static const mdb_modinfo_t modinfo = {
135*7c478bd9Sstevel@tonic-gate 	MDB_API_VERSION, dcmds, NULL
136*7c478bd9Sstevel@tonic-gate };
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate const mdb_modinfo_t *
_mdb_init(void)139*7c478bd9Sstevel@tonic-gate _mdb_init(void)
140*7c478bd9Sstevel@tonic-gate {
141*7c478bd9Sstevel@tonic-gate 	return (&modinfo);
142*7c478bd9Sstevel@tonic-gate }
143